我试图在pygame中进行箭头键移动,但是screen.blit还没有工作:/ 它一直出现错误说
>###AttributeError: 'builtin_function_or_method' object has no attribute 'blit'
这是我的代码:
import sys, pygame
from pygame.locals import *
pygame.init()
pygame.display.set_caption(" Arrow keys to move ")
#Colour Defintions
white = pygame.Color(255,255,255)
black = pygame.Color( 0, 0 ,0)
gray = pygame.Color(90, 90, 90)
silver = pygame.Color( 200, 200, 200)
red = pygame.Color( 200, 0 , 0)
blue = pygame.Color(0, 255, 0)
#screen size
size = width, height = 1024, 640
velocity = [5, -20]
screen = pygame.display.set_mode
sprite = pygame.image.load("sprite01.png")
spriterect = sprite.get_rect()
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pressed_keys = pygame.key.get_pressed()
if pressed_keys[pygame.K_RIGHT]:
spriterect = spriterect.move(5, 0)
if pressed_keys[pygame.K_LEFT]:
spriterect = spriterect.move(-5, 0)
if pressed_keys[pygame.K_UP]:
spriterect = spriterect.move(0, 5)
if pressed_keys[pygame.K_UP]:
spriterect = spriterect.move(0, -5)
if spriterect.top < 0:
spriterect.top = 0
if spriterect.bottom > height:
spriterect.bottom = height
screen.blit(sprite(0, 0))
screen.blit(spriterect(0, 0))
pygame.display.flip()
clock.tick(120)}
答案 0 :(得分:2)
你错过了set_mode调用的括号,所以引用一个函数而不是调用该函数的返回值。
screen = pygame.display.set_mode
应该是:
screen = pygame.display.set_mode(size)