- 帖子
- 1
- 精华
- 0
- 积分
- 7
- 阅读权限
- 10
- 注册时间
- 2019-7-7
- 最后登录
- 2019-7-8
|
代码如下:
这是《与孩子一起学编程》这本书里面的17-2代码清单,在SPE里面运行就显示黑框,是为什么呀?
import sys, pygame
from random import *
class MyBallClass(pygame.sprite.Sprite):
def __init__(self, image_file, location, speed):
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):
self.rect = self.rect.move(self.speed)
if self.rect.left < 0 or self.rect.right > width:
self.speed[0] = - self.speed[0]
if self.rect.top < 0 or self.rect.bottom > height:
self.speed[1] = -self.speed[1]
size = width, height = 640, 480
screen = pygame.display.set_mode(size)
screen.fill([255,255,255])
img_file = "beach_ball.png"
balls = []
for row in range (0, 3):
for column in range (0, 3):
location = [column*180 + 10, row*180 + 10]
speed = [choice([-2, 2]), choice([-2, 2])]
ball = MyBallClass(img_file, location, speed)
balls.append(ball)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
pygame.time.delay(20)
screen.fill([255,255,255])
for ball in balls:
ball.move()
screen.blit(ball.image, ball.rect)
pygame.display.flip()
|
|