Вы можете вызывать метод move
каждый раз, когда запускаете игру l oop вместе с draw
, а затем вызывать метод jump
только один раз, когда нажимается пробел.
Результатом будет (с некоторыми другими незначительными изменениями, такими как вызов pygame.init
, выход с quit
вместо pygame.quit
и удаление избыточных членов класса):
import pygame
from pygame import Color
class Bird:
def __init__(self):
self.x = 10
self.y = 300
self.jumpCount = 10
self.isJump = False
def draw(self):
pygame.draw.rect(surface, Color("yellow"), (self.x, self.y, 10, 10))
pygame.display.flip()
def jump(self):
self.jumpCount = 10
self.isJump = True
def move(self):
if self.isJump:
if self.jumpCount >= -10:
self.y += (self.jumpCount * abs(self.jumpCount)) * -0.5
self.jumpCount -= 1
else:
self.isJump = False
def run():
bird = Bird()
while True:
pygame.time.delay(100)
surface.fill(Color("blue"))
bird.move()
bird.draw()
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == 32:
bird.jump()
if event.type == pygame.QUIT:
quit()
pygame.display.flip()
pygame.init()
HEIGHT = 500
WIDTH = 400
surface = pygame.display.set_mode((HEIGHT, WIDTH))
run()