pygame问题
import pygameimport sys
from pygame.locals import *
import 金币
pygame.init()
size = width, height = 600, 400
bg = (255, 255, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
fanxiang = 'right'
left = 300
top = 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
pygame.time.set_timer(move, 1 * 1000)
money = USEREVENT + 1
pygame.time.set_timer(money, 5 * 1000)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == move:
if fanxiang == 'left':
left -= 11
elif fanxiang == 'top':
top -= 11
elif fanxiang == 'right':
left += 11
elif fanxiang == 'bottom':
top += 11
#生成金币
elif event.type == money:
Money = pygame.sprite.Group()
m = 金币.money()
Money.add(m)
key_pressed = pygame.key.get_pressed()
#控制方向
if key_pressed or key_pressed:
fanxiang = 'top'
if key_pressed or key_pressed:
fanxiang = 'bottom'
if key_pressed or key_pressed:
fanxiang = 'right'
if key_pressed or key_pressed:
fanxiang = 'left'
screen.fill(bg)
#金币机制
for i in Money:
pygame.draw.rect(screen, snake, (left, top, 10, 10), 0)
#if 金币.money
#绘制贪吃蛇
pygame.draw.rect(screen, YELLOW, (金币.money.width, 金币.money.height, 10, 10), 0)
pygame.display.flip()
为什么pygame说Money未定义?谢谢 Money的定义应该在while循环外部,而不是在事件处理中。在while循环外部定义Money变量并将其初始化为pygame.sprite.Group(),然后在事件处理中添加金币对象到Money中。另外,金币对象的绘制也需要在循环中进行。另外,金币对象的绘制应该使用m.rect属性而不是直接使用金币.money.width和金币.money.height属性。
修正后的代码应该如下所示:
import pygame
import sys
from pygame.locals import *
import 金币
pygame.init()
size = width, height = 600, 400
bg = (255, 255, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
fanxiang = 'right'
left = 300
top = 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
pygame.time.set_timer(move, 1 * 1000)
money = USEREVENT + 1
pygame.time.set_timer(money, 5 * 1000)
Money = pygame.sprite.Group()# 在while循环外部定义Money变量
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == move:
if fanxiang == 'left':
left -= 11
elif fanxiang == 'top':
top -= 11
elif fanxiang == 'right':
left += 11
elif fanxiang == 'bottom':
top += 11
elif event.type == money:
m = 金币.money()
Money.add(m)# 在事件处理中添加金币对象到Money中
key_pressed = pygame.key.get_pressed()
if key_pressed or key_pressed:
fanxiang = 'top'
if key_pressed or key_pressed:
fanxiang = 'bottom'
if key_pressed or key_pressed:
fanxiang = 'right'
if key_pressed or key_pressed:
fanxiang = 'left'
screen.fill(bg)
for i in Money:
pygame.draw.rect(screen, snake, i.rect)# 绘制金币对象
pygame.draw.rect(screen, YELLOW, (left, top, 10, 10), 0)# 绘制贪吃蛇
pygame.display.flip()
通过将Money的定义移至while循环外部,并在事件处理中添加金币对象到Money中,可以解决Money未定义的问题。同时,修正了金币对象的绘制方式。 在你的代码中,你在事件循环内部定义了 Money,在循环外部并没有定义。因此,在绘制金币的部分无法识别 Money。此外,在绘制贪吃蛇和金币时出现了一些错误。
以下是修改后的代码:
import pygame
import sys
from pygame.locals import *
import 金币
pygame.init()
size = width, height = 600, 400
bg = (255, 255, 255)
YELLOW = (255, 255, 0)
snake = (0, 255, 0)
fanxiang = 'right'
left = 300
top = 200
screen = pygame.display.set_mode(size)
pygame.display.set_caption('贪吃蛇')
move = USEREVENT
pygame.time.set_timer(move, 1 * 1000)
money = USEREVENT + 1
pygame.time.set_timer(money, 5 * 1000)
# 将金币定义移至事件循环外部
Money = pygame.sprite.Group()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == move:
if fanxiang == 'left':
left -= 11
elif fanxiang == 'top':
top -= 11
elif fanxiang == 'right':
left += 11
elif fanxiang == 'bottom':
top += 11
# 生成金币
elif event.type == money:
m = 金币.money()
Money.add(m)
key_pressed = pygame.key.get_pressed()
# 控制方向
if key_pressed or key_pressed:
fanxiang = 'top'
if key_pressed or key_pressed:
fanxiang = 'bottom'
if key_pressed or key_pressed:
fanxiang = 'right'
if key_pressed or key_pressed:
fanxiang = 'left'
screen.fill(bg)
# 绘制金币与贪吃蛇
for i in Money:
pygame.draw.rect(screen, snake, i.rect)# 绘制金币
pygame.draw.rect(screen, YELLOW, (left, top, 10, 10), 0)# 绘制贪吃蛇
pygame.display.flip()
希望对你有所帮助!
页:
[1]