0
當我嘗試運行這個簡單的遊戲。它說:我在哪裏添加鍵盤控件到pygame中的精靈?
screen.blit(plumbers,(50 + M ,337 + N))
UnboundLocalError: local variable 'M' referenced before assignment
這是來自婁代碼:
import pygame
import os, sys
import itertools
import pygame
from pygame.sprite import Sprite
cloud_background = pygame.image.load('clouds.bmp')
brick_tile = pygame.image.load('brick_tile.png')
plumbers = pygame.image.load('Mario_sideways_sprite_2xL.png')
pink = (255, 64, 64)
w = 640
h = 480
screen = pygame.display.set_mode((w, h))
running = 1
def setup_background():
screen.fill((pink))
screen.blit(cloud_background,(0,0))
brick_width, brick_height = brick_tile.get_width(), brick_tile.get_height()
for x,y in itertools.product(range(0,640,brick_width),
range(390,480,brick_height)):
# print(x,y)
screen.blit(brick_tile, (x,y))
def show_sprites():
screen.blit(plumbers,(50 + M ,337 + N))
if event.key == pygame.K_UP:
M = 1
N = 0
while running:
setup_background()
show_sprites()
move()
event = pygame.event.poll()
if event.type == pygame.QUIT: sys.exit()
但是,當我加入上述 「screen.blit」 變量,所以它看起來是這樣的:
def show_sprites():
if event.key == pygame.K_UP:
M = 1
N = 0
screen.blit(plumbers,(50 + M ,337 + N))
我得到這個錯誤:
Traceback (most recent call last):
File "C:\Users\Enrique\Dropbox\GAMEZ_PYGAME\gamez.py", line 36, in <module>
show_sprites()
File "C:\Users\Enrique\Dropbox\GAMEZ_PYGAME\gamez.py", line 28, in show_sprites
if event.key == pygame.K_UP:
NameError: global name 'event' is not defined
我該怎麼做必須添加/更改才能使這些簡單的控件起作用?
我想你的建議,我得到了錯誤:回溯(最近通話最後一個): 文件 「C:\用戶\恩裏克\ Dropbox的\ GAMEZ_PYGAME \ gamez.py」,行41,在 show_sprites(事件) 文件 「C:\用戶\恩裏克\收存箱\ GAMEZ_PYGAME \ gamez.py」,第28行,在show_sprites 如果event.key == pygame.K_UP: AttributeError的: '事件'對象有沒有屬性「鍵」 @joaquin –
enrique2334
2012-01-15 17:22:36
這是什麼意思是「事件對象有沒有屬性關鍵@joaquin – enrique2334 2012-01-15 17:29:43
@ enrique2334看版它 – joaquin 2012-01-15 18:45:21