[Python] How do I make objects in pygame scroll while focusing on a single object? [duplicate]

Discussão em 'Python' iniciado por Stack, Junho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm creating a game in pygame, in which you have to stop the ball from falling out of the screen and keep it up for as long as possible by creating line segments from which it bounces off. I have done most of the physics and logic stuff, but I want to make the lines move down instead of the ball moving up (which creates a similar effect). The ball should stay stationary in one place and all the other objects should "scroll" downwards.

    main.py

    import pygame
    import colorspy
    from line import Line, THICKNESS
    import random

    pygame.init()

    WIDTH, HEIGHT = 500, 800
    win = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption("Linez")

    last_pos = (192, 500)
    circles = [last_pos]
    lines = []
    current_line = None

    ball = pygame.Vector2(200, 80)
    radius = 10
    touching = False
    vel = pygame.Vector2()
    G = 440

    offset = pygame.Vector2(0, 0)

    clock = pygame.time.Clock()
    run = True
    while run:
    dt = clock.tick(60)/1000
    touching = False

    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    run = False

    if event.type == pygame.MOUSEBUTTONDOWN:
    current_pos = pygame.mouse.get_pos()
    if current_pos != last_pos:
    circles.append(current_pos)
    lines.append(Line(last_pos, current_pos))
    last_pos = current_pos
    current_line = lines[-1]

    win.fill(colorspy.midnight_blue)

    for circle in circles:
    pygame.draw.circle(win, colorspy.white, circle, 8)

    for line in lines:
    pygame.draw.line(win, colorspy.white, line.p1, line.p2, THICKNESS)

    pygame.draw.circle(win, colorspy.dark_orange, ball, radius)

    touching = False
    if current_line != None:
    if current_line.collide(ball, radius):
    normal = pygame.Vector2(-current_line.normal.y, current_line.normal.x)

    dot = vel.dot(normal)
    vel = vel - 2 * dot * normal

    # Nudge the ball out to prevent sticking
    #ball += normal * 2

    touching = True

    if not touching:
    vel.y += G * dt
    ball += vel * dt

    offset.y += vel.y * dt

    if ball.x - radius > WIDTH:
    ball.x = -radius
    elif ball.x + radius < 0:
    ball.x = WIDTH + radius

    pygame.display.update()

    pygame.quit()


    line.py

    import pygame
    import math


    THICKNESS = 5
    ACC = 0.2


    class Line:
    def __init__(self, start, end):
    self.p1 = pygame.Vector2(start)
    self.p2 = pygame.Vector2(end)
    self.res = self.p2 - self.p1
    self.length = self.p1.distance_to(self.p2)
    self.angle = math.atan2(self.res.y, self.res.x)
    self.normal = self.res.normalize()

    def point_circle(self, px, py, ball, r):
    return ball.distance_to(pygame.Vector2(px, py)) <= r

    def line_point(self, ball):
    dist1 = ball.distance_to(self.p1)
    dist2 = ball.distance_to(self.p2)

    return (dist1 + dist2) > self.length - ACC and (dist1 + dist2) < self.length + ACC

    def collide(self, ball, radius):
    inside1 = self.point_circle(self.p1.x, self.p1.y, ball, radius)
    inside2 = self.point_circle(self.p2.x, self.p2.y, ball, radius)

    if inside1 or inside2:
    return True

    dot = ( ((ball.x-self.p1.x)*(self.p2.x-self.p1.x)) + ((ball.y-self.p1.y)*(self.p2.y-self.p1.y)) ) / (self.length * self.length)

    x = self.p1.x + (dot * (self.p2.x - self.p1.x))
    y = self.p1.y + (dot * (self.p2.y - self.p1.y))

    on_segment = self.line_point(pygame.Vector2(x, y))
    if not on_segment:
    return False

    dist = ball.distance_to(pygame.Vector2(x, y))

    return dist <= radius


    I tried creating an offset vector with the velocity of the ball and adding it to all the objects, but it didn't seem to work properly. Is there a way I could implement this without redoing my entire code?

    Continue reading...

Compartilhe esta Página