在pygam上使用“blit”时出现意外语法错误

2024-04-25 22:17:13 发布

您现在位置:Python中文网/ 问答频道 /正文

import pygame
import sys
from pygame.locals import *

pygame.init()

white = (255,255,255)
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
yellow = (255,255,0)
cyan = (0,255,255)
purple = (255,0,255)

bg = black

fps = 30
dispWidth = 800
dispHeight = 600
pixMove = 10

UP = 'up'
DOWN = 'down'
LEFT = 'left'
RIGHT = 'right'

def runGame():
    imgx = 3
    imgy = 3
    direction = RIGHT

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()

            elif event.type == KEYDOWN:
                if event.key == (K_LEFT or K_a):
                    direction = LEFT
                elif event.key == (K_RIGHT or K_d):
                    direction = RIGHT
                elif event.key == (K_DOWN or K_s):
                    direction = DOWN
                elif event.key == (K_UP or K_w):
                    direction = UP

            if direction == UP:
                imgy -= pixMove
            elif direction == DOWN:
                imgy += pixMove
            elif direction == LEFT:
                imgx -=pixMove
            elif direction == RIGHT:
                imgx += pixMove

            setDisplay.fill(bg)
            img = pygame.image.load('kirby.png')
            img = pygame.transform.scale(img, (20,20)
            setDisplay.blit(img,(imgx,imgy))
            pygame.display.update()

while True:
    global fpsTime
    global setDisplay

    fpsTime = pygame.time.Clock()
    setDisplay = pygame.display.set_mode((dispWidth,dispHeight))
    pygame.display.set_caption('kirby\'s controlled adventure')
    runGame()

好的,所以当我运行这个代码时,我在设置显示.blit(img,(imgx,imgy))“(更具体地说,在setDisplay之后。你知道吗

我不知道这段代码到底出了什么问题,有人能帮我吗?你知道吗

(我使用的是Python3.4 btw)


Tags: orkeyrighteventimgleftpygamedown