|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 齐鲁骏马 于 2020-4-6 18:03 编辑
import pygame
import sys
from pygame.locals import *
from random import *
class Ball(pygame.sprite.Sprite):
def __init__(self,image,position,speed):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left,self.rect.top = position
def main():
pygame.init()
ball_image = 'gray_ball.png'
bg_image = 'background.png'
running = True
bg_size = width, height = 1024, 681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption('Play the ball - FishC Demo')
background = pygame.image.load(bg_image).convert_alpha()
balls = []
for i in range(5):
position = randint(0, width-100),randint(0, height-100)
speed = [randint(-10, 10), randint(-10, 10)]
ball = Ball(ball_image, position, speed)
balls.append(ball)
clock = pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
screen.blit(background, (0, 0))
for each in balls:
screen.blit(each.image, each.rect)
pygame.display.flip()
clock.tick(30)
if __name__ == '__main__':
main()
以上代码是书上的,第2版18.7.1章节的,在我的电脑运行后出现黑框,没有出现画面和精灵,求解决方案
这是一个自定义的图片,你可以将以下的压缩包解压后运行main.py,可以得到你要的效果
p18_19.zip
(182.88 KB, 下载次数: 5)
|
-
-
|