这是我要制作的游戏代码(翻拍一只飞扬的小鸟)
def move():
global bird_x
global bird_y
pygame.draw.rect(win,(0,0,0), (0,0,1000,1000))
pygame.draw.rect(win,(255,0,0),(bird_x, bird_y, 30, 30))
pygame.display.update()
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
bird_y += 60
fb.move()
time.sleep(0.005)
当我尝试运行此代码时,“ bird”(红色方块)不会移动,我已更改了所使用的键,已将动作移入和移出了功能,但没有任何作用。 (这是经过修剪的代码,因此变量可能不存在)
完整代码:
import time
import random
pygame.init()
win = pygame.display.set_mode((400,600))
pygame.display.set_caption('Flappy Bird')
pipex = 345
pipe1y = -270
pipe2y = 420
width = 65
height1 = 400
height2 = 3000
vel = 5
bird_x = 20
bird_y = 300
isJump = False
jumpCount = 10
class fb:
def move():
global pipex
global yy
global pipe1y
global pipe2y
global bird_x
global bird_y
pipex -= 1
if pipex < -60:
pipex = 345
yy = random.randint(-350,0)
pipe1y = yy
pipe2y = pipe1y + 555
pygame.draw.rect(win,(0,0,0), (0,0,1000,1000))
pygame.draw.rect(win,(0, 255, 0), (pipex, pipe1y, width, height1))
pygame.draw.rect(win,(0, 255, 100), (pipex, pipe2y, width, height2))
pygame.draw.rect(win,(255,0,0),(bird_x, bird_y, 30, 30))
pygame.display.update()
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
bird_y += 60
fb.move()
time.sleep(0.005)
答案 0 :(得分:3)
将主循环更改为此:
while True:
keys = pygame.key.get_pressed()
if keys[pygame.K_UP]:
bird_y -= 1
if keys[pygame.K_DOWN]:
bird_y += 1
fb.move()
time.sleep(0.005)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
break
您没有测试事件 您需要处理事件
PS: 我加了下移动作