- 帖子
- 11
- 精华
- 0
- 积分
- 64
- 阅读权限
- 20
- 注册时间
- 2018-4-30
- 最后登录
- 2018-7-14
|
crossin先生你好,请教一个代码:
import sys,pygame
pygame.init()
screen=pygame.display.set_mode([640,480])
background=pygame.Surface(screen.get_size())
background.fill([255,255,255])
clock=pygame.time.Clock()
class Ball(pygame.sprite.Sprite):
def __init__(self,image_file,speed,location):
pygame.sprite.Sprite. __init__(self)
self.image=pygame.image.load(image_file)
self.rect=self.image.get_rect()
self.rect.left,self.rect.top=location
self.speed =speed
def move(self):
if self.rect.left<=screen.get_rect().left or \
self.rect.right>=screen.get_rect().right:
self.speed[0]=-self.speed[0]
newpos=self.rect.move(self.speed)
self.rect=newpos
my_ball = Ball('beach_ball.png',[10,0],[20,20])
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
sys.exit()
clock.tick(30)
screen.blit(background,(0,0))
my_ball.move()
screen.blit(my_ball.image,my_ball.rect)
pygame.display.flip()
-----------------------------------------
运行后显示
Traceback (most recent call last):
File "/Users/Max/PycharmProjects/untitled1/hi.py", line 7, in <module>
class Ball(pygame.sprite.Sprite):
File "/Users/Max/PycharmProjects/untitled1/hi.py", line 22, in Ball
my_ball = Ball('beach_ball.png',[10,0],[20,20])
NameError: name 'Ball' is not defined
第7行定义Ball(),为何第22行调用的时候又显示没定义呢?谢谢! |
|