我如何在pygame的按键事件中使图像变白 [英] How do I blit an image on a keypressed event in pygame
本文介绍了我如何在pygame的按键事件中使图像变白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
触发事件 K_SPACE
时,我试图在屏幕上显示图像。
但是,当我按空格键时什么也没发生。我尝试移动屏幕更新或翻转的位置,但是碰到了砖墙。
I'm trying to blit an image to the screen when the event K_SPACE
is triggered.
However, when I press space nothing happens. I've tried moving where the screen is updated or flipped, but i've hit a brick wall.
这是我的代码:
def Play():
background = pygame.image.load(bifPlay).convert()
char = pygame.image.load(mif).convert_alpha()
bullet = pygame.image.load(bulletLoad).convert()
zombie = pygame.image.load(zombieLoad).convert_alpha()
#converting images needed so pygame can use them
x,y = 115,350
movex, movey = 0,0
zombieX = 1100
zombieY = random.randint(0,675)
bulletX = x+90
bulletY = y+37.5
clock = pygame.time.Clock()
zombieSpeed = 100
bulletSpeed = 200
while True:
milli = clock.tick()
seconds = milli/1000.
zomMovement = seconds*zombieSpeed
bulletMovement = seconds*bulletSpeed
zombieX-=zomMovement
bulletX+=bulletMovement
x += movex
y += movey
screen.blit(background,(0,0))
screen.blit(char,(x,y))
screen.blit(zombie,(zombieX,zombieY))
spacePressed = pygame.key.get_pressed()
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
#code above allows the user to exit - while program is running if event is exit
#allows user to exit program.
if event.type == KEYDOWN:
if event.key == K_UP:
movey =-1
elif event.key == K_DOWN:
movey =+1
elif event.key == K_SPACE:
screen.blit(bullet,(bulletX,bulletY))
if event.type == KEYUP:
if event.key == K_UP:
movey =0
elif event.key == K_DOWN:
movey =0
if y == 675:
y -= 1
if y == 0:
y +=1
pygame.display.update()
推荐答案
由于您已按下spacePressed,因此请使用它来绘制项目符号:
Since you have spacePressed, use that to draw the bullet like this:
if spacePressed[K_SPACE]:
screen.blit(bullet, (bulletx, bullety))
您也可以对任何键使用spacePressed。
You can use spacePressed for any key as well.
这篇关于我如何在pygame的按键事件中使图像变白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文