如何使用pygame在python中移动图片?

时间:2019-02-02 15:54:30

标签: python pygame pygame-surface

我在python初学者,我努力学习,以使一个简单的游戏pygame中的功能。我想用WASD键移动图像,但它不工作。请帮忙。

我上窗10使用Python(3.7.0)。 以下是代码:

import pygame
from pygame.locals import *
pygame.init()

pygame.display.init()
keys = [False, False, False, False]
screen=pygame.display.set_mode((0, 0), pygame.FULLSCREEN)
background=pygame.Surface(screen.get_size())
background.fill((255,255,255))
playerpos=[100,100]
player=pygame.image.load("Copper.png")

while 1:
    pygame.display.init()
    screen.fill(0)
    screen.blit(background,(0,0))
    screen.blit(player,playerpos)
    for event in pygame.event.get():
        pygame.display.init()
        pygame.display.flip()
        if event.type == pygame.KEYDOWN:
            if event.key==pygame.K_RETURN:
                    pygame.display.quit()
        pygame.display.flip()
        # 8 - loop through events
        for event in pygame.event.get():
            # 9 - check if event is X button 
            if event.type==pygame.QUIT:
                # 10 - quit the game
                pygame.quit() 
                exit(0)
            if event.type == pygame.KEYDOWN:
                if event.key==K_w:
                    keys[0]=True
                elif event.key==K_a:
                    keys[1]=True
                elif event.key==K_s:
                    keys[2]=True
                elif event.key==K_d:
                    keys[3]=True
            if event.type == pygame.KEYUP:
                if event.key==pygame.K_w:
                    keys[0]=False
                elif event.key==pygame.K_a:
                    keys[1]=False
                elif event.key==pygame.K_s:
                    keys[2]=False
                elif event.key==pygame.K_d:
                    keys[3]=False
            # 9 - Move player
            if keys[0]:
                playerpos[1]-=500
            elif keys[2]:
                playerpos[1]+=500
            if keys[1]:
                playerpos[0]-=500
            elif keys[3]:
                playerpos[0]+=500

我希望当我按w,a,s,d时图像“ Copper.png”会移动,但是图像不会移动。图像不刷新每次我按瓦特,A,S,d但不移动。

1 个答案:

答案 0 :(得分:2)

摆脱多个偶数处理循环。使用一个循环来处理所有事件。
进一步就足够了初始化显示一次(pygame.display.init()

创建变量speed,该变量定义像素数,图像的位置每步更改一次

首先评估pygame.QUIT事件,并在事件发生时停止运行主循环:

done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

然后处理其他事件,例如pygame.KEYDOWNpygame.KEYUP

要连续运动,必须在事件循环之外进行玩家位置的操纵。如果在循环中是唐,则玩家的位置将仅在发生事件时改变。请注意使用小的“速度”(speed = 1),否则播放器会迅速移出窗口。

for event in pygame.event.get():
    # event handling

if keys[0]:
    playerpos[1]-=speed
elif keys[2]:
    playerpos[1]+=speed
if keys[1]:
    playerpos[0]-=speed
elif keys[3]:
    playerpos[0]+=speed

执行场景的图,在主循环的末尾:

speed = 1
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key==pygame.K_RETURN:
                done = True
            for i in range(4):
                if event.key == (K_w, K_a, K_s, K_d)[i]:
                    keys[i]=True
        elif event.type == pygame.KEYUP:
            for i in range(4):
                if event.key == (K_w, K_a, K_s, K_d)[i]:
                    keys[i]=False
    if keys[0]:
        playerpos[1]-=speed
    elif keys[2]:
        playerpos[1]+=speed
    if keys[1]:
        playerpos[0]-=speed
    elif keys[3]:
        playerpos[0]+=speed

    screen.fill(0)
    screen.blit(background,(0,0))
    screen.blit(player,playerpos)    
    pygame.display.flip()

请注意,您也可以使用pygame.key.get_pressed()一次获取所有键盘按钮的所有状态。因此,您无需分别评估关键事件:

speed = 1
done = False
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_RETURN:
                done = True

    allKeys = pygame.key.get_pressed()
    playerpos[0] += -speed if allKeys[K_a] else speed if allKeys[K_d] else 0
    playerpos[1] += -speed if allKeys[K_w] else speed if allKeys[K_s] else 0

    screen.fill(0)
    screen.blit(background,(0,0))
    screen.blit(player,playerpos)    
    pygame.display.flip()