使用pygame在python屏幕上移动对象时遇到麻烦

河马

我总体上是pygame和python中的nubie。我昨天开始了一个简单的直升机游戏项目,但我不知道为什么我的直升机不动。

我遵循与我所观看的youtube教程完全相同的模式,但是仍然无法移动图像。

import pygame

#GLOBAL CONSTANTS_______________________________________________
pygame.init()

screen_height = 600
screen_width = 1200

black = (0,0,0)
white = (255,255,255)

screen = pygame.display.set_mode((screen_width,screen_height))
title = pygame.display.set_caption("Helicpter game")
clock = pygame.time.Clock()
FPS = 10

heli = pygame.image.load(r"C:\Users\rahul\Downloads\heli1.jpg")
heli = pygame.transform.scale(heli, (150, 150))
def helicopter(x, y):
    screen.blit(heli, (x, y))

x = (0 * screen_width)
y = (0.5 * screen_height)

change_y = 0

gameExit = False
while not gameExit:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            gameExit = True

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_UP:
            change_y = -2


        elif event.key == pygame.K_DOWN:
            change_y = 2

    if event.type == pygame.KEYUP:
        if event.key ==  pygame.K_DOWN or event.key == pygame.K_UP:
           change_y = 0

    change_y += y

    screen.fill(white)
    helicopter(x, y)

    pygame.display.flip()

clock.tick(FPS)
pygame.quit 

它没有给我任何错误消息,但是图像完全没有移动。

拉比德76

您必须更改变量,y而不是change_y

change_y += y

y += change_y

注意,定义图像位置的坐标为ychange_y用于修改此位置。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章