""" 盒子里的弹球小世界 """ import pygame from random import randint from pygame.locals import * RED = (255,0,0) BLUE = (0,0,255) class Ball(pygame.sprite.Sprite): """ 球类,继承自角色类,它会在一个矩形范围内受重力移动。 """ def __init__(self,radius,box,color): """ radius:半径,box:所在的盒子,color:颜色 """ def set_boundary(self): """设置反弹的边界""" self.left_boundary = self.box.rect.left # 左边界 self.right_boundary = self.box.rect.right # 右边界 self.top_boundary = self.box.rect.top # 上边界 self.bottom_boundary = self.box.rect.bottom # 下边界 def update(self): """更新坐标,碰到边缘就反弹。""" class Box(pygame.sprite.Sprite): """ 盒子类,继承自角色类 """ def __init__(self,width,height,pos): """ width:宽度,height:高度 """ size = width,height= 640,380 screen = pygame.display.set_mode(size) pygame.display.set_caption("盒子里的弹珠世界by lixingqiu") running = True while running: for event in pygame.event.get(): if event.type == QUIT:running=False balls1.update() balls2.update() screen.fill((20,30,0)) screen.blit(box1.image,box1.rect) screen.blit(box2.image,box2.rect) balls1.draw(screen) balls2.draw(screen) pygame.display.update() clock.tick(30) pygame.quit()
下载完整源代码与素材,请
需要浏览更多吗?
成为会员后,登陆才能继续浏览!联系微信scratch8即可办理会员。
(会员专属:能浏览所有文章,下载所有带链接的Python资源。)