[Python] Dooble pygame Jump

Discussão em 'Python' iniciado por Stack, Setembro 3, 2025.

  1. Stack

    Stack Membro Participativo

    So one day i've decided to start making a simple Pygame remake of old Dooble Jump (the idea was to make a Toby Fox as a player :p) and i had working prototype, with camera following player and sections generation. But i found a problem, lags. I knew python is slow, but it could handle infinity platform generations (i also made a function that removes platforms that are under the screen), but after all, ive got broken code. Sections dont generate, and idk what to do with that. Here is my code:

    from random import *
    pygame.init()


    # comments with parts of code are unused and left for seeing difference between versions of functions
    WIDTH = 500
    HEIGHT = 700
    FPS = 120
    cam_Y = HEIGHT // 2
    world_top = 0

    #colors
    sky = (104, 205, 254)
    green = (0, 255, 0)
    yellow = (219, 215, 0)
    black = (0, 0, 0)
    grass = (84, 208, 78)
    dirt = (121, 85, 58)
    platform_color = (172, 107, 104)

    #window
    win = pygame.display.set_mode((WIDTH, HEIGHT))
    pygame.display.set_caption('Dooble TOBY FOX')
    clock = pygame.time.Clock()
    win.fill(sky)

    #classes
    class Platform:
    def __init__(self, x, y, w=80, h=20, color=grass):
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.color = color
    def draw(self):
    pygame.draw.rect(win, self.color, (self.x, self.y, self.w, self.h))



    class Player:
    def __init__(self, x, y, w=30, h=30, r=20, color=yellow):
    self.x = x
    self.y = y
    self.w = w
    self.h = h
    self.r = r
    self.speed = 200
    self.color = color
    self.gravity = 800
    self.jump_power = 600
    self.vy = 0
    self.on_ground = False
    def draw(self):
    # pygame.draw.circle(win, self.color, (self.x, self.y), self.w)
    # pygame.draw.circle(win, black, (self.x, self.y), self.w, 3)
    pygame.draw.circle(win, self.color, (int(self.x), int(self.y)), self.r)
    pygame.draw.circle(win, black, (int(self.x), int(self.y)), self.r, 3)

    def move(self, dt):
    keys = pygame.key.get_pressed()
    if keys[pygame.K_a]:
    self.x -= self.speed * dt
    if keys[pygame.K_d]:
    self.x += self.speed * dt

    def looping_map(self):
    # if self.x >= WIDTH:
    # self.x = WIDTH - WIDTH
    # elif self.x <= WIDTH - WIDTH:
    # self.x = WIDTH
    if self.x - self.r > WIDTH:
    self.x = -self.r
    elif self.x + self.r < 0:
    self.x = WIDTH + self.r


    def jump(self):
    self.vy = -self.jump_power

    def gravity_effect(self, dt):
    self.vy += self.gravity * dt
    self.y += self.vy * dt

    def lose(self):
    global playing
    if self.y - self.r > HEIGHT:
    print('You lose!')
    playing = False

    def physics(self, dt):
    for p in platforms:
    if (self.y + self.r >= p.y and
    self.y + self.r <= p.y + 20 and
    self.x >= p.x and self.x <= p.x + p.w and
    self.vy > 0):
    self.jump()

    def update(self, dt):
    global world_top
    self.move(dt)
    self.looping_map()
    self.gravity_effect(dt)
    self.physics(dt)
    if self.y < HEIGHT // 2:
    shift = self.vy * dt
    self.y = HEIGHT // 2
    for p in platforms:
    p.y += -shift
    platforms[:] = [p for p in platforms if p.y > -50]
    if world_top >= 0:
    world_top = 0

    generate_section(world_top - HEIGHT, world_top)
    world_top -= HEIGHT
    self.lose()
    self.draw()

    # def generate_section(y_end, y_start):
    # # if platforms:
    # # last_y = max(p.y for p in platforms)
    # # else:
    # # last_y = HEIGHT
    # # section_platforms = []
    # min_dist = 30
    # for _ in range(10):
    # while True:
    # x = randint(50, WIDTH - 130)
    # y = randint(y_end, y_start)
    # too_close = False
    # for p in platforms:
    # if abs(p.y - y) < min_dist:
    # too_close = True
    # break
    # if not too_close:
    # platforms.append(Platform(x, y, 100, 10, grass))
    # break

    # def generate_section(y_start, y_end):
    # step = 60 # расстояние между платформами по высоте
    # for y in range(y_end, y_start, step):
    # x = randint(50, WIDTH - 130) # случайный X
    # platforms.append(Platform(x, y, 100, 10, grass))

    def generate_section(y_start, y_end):
    step = 60
    if y_start < y_end:
    rng = range(y_end, y_start, -step)
    else:
    rng = range(y_start, y_end, -step)
    for y in rng:
    x = randint(50, WIDTH - 130)
    platforms.append(Platform(x, y, 100, 10, grass))

    # ball = Player(WIDTH // 2, 650, 20, 0, yellow)
    ball = Player(WIDTH // 2, 650, r=20, color=yellow)
    platforms = [Platform(WIDTH//2 - 50, 670, 100, 10, grass)]


    #functions
    def create_random_platforms(n):
    min_dist = 70
    for i in range(n):
    while True:
    min_dist = 30
    x = randint(50, WIDTH - 130)
    y = randint(100, HEIGHT - 60)
    # Проверяем расстояние до всех платформ
    too_close = False
    for p in platforms:
    if abs(p.y - y) < min_dist:
    too_close = True
    break
    if not too_close:
    platforms.append(Platform(x, y, 100, 10, grass))
    break

    create_random_platforms(10)
    playing = True
    while playing:
    dt = clock.tick(FPS) / 1000 # время в секундах с последнего кадра
    win.fill(sky)

    #шар
    ball.update(dt)
    #платформа
    for p in platforms:
    p.draw()


    for event in pygame.event.get():
    if event.type == pygame.QUIT:
    playing = False
    pygame.display.update()```

    Continue reading...

Compartilhe esta Página