import pygame
import sys
from pygame.sprite import Group
from pygame.sprite import Sprite
import random
# 小球个数
number = int(input("请输入小球的个数:"))
move_speed = int(input("请输入小球的移动速度:"))
up_left = 7
up_right = 9
down_left = 3
down_right = 1
black = (0, 0, 0)
blue = (0, 0, 255)
windows_width = 1200
windows_height = 600
r = 10
# 小球类
class Bound(Sprite):
def __init__(self, direction, color, circle_x, circle_y):
super(Bound, self).__init__()
self.circle_x = circle_x
self.circle_y = circle_y
self.color = color
self.start_direction = direction
# 检查边界,并设置反弹
def check_border(self):
if self.circle_y <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = down_left
# 右上
elif self.start_direction == up_right:
self.start_direction = down_right
elif self.circle_y >= windows_height:
# 右下
if self.start_direction == down_right:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = up_left
elif self.circle_x <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = down_right
elif self.circle_x >= windows_width:
# 右上
if self.start_direction == up_right:
self.start_direction = up_left
# 右下
elif self.start_direction == down_right:
self.start_direction = down_left
# 判断走的方向,并更新数据
def type_judge(self):
if self.start_direction == up_right:
self.circle_y -= move_speed
self.circle_x += move_speed
elif self.start_direction == up_left:
self.circle_y -= move_speed
self.circle_x -= move_speed
elif self.start_direction == down_left:
self.circle_y += move_speed
self.circle_x -= move_speed
elif self.start_direction == down_right:
self.circle_y += move_speed
self.circle_x += move_speed
def draw_buf(self, windows):
pygame.draw.circle(windows, self.color, (self.circle_x, self.circle_y), r)
class Sq(Sprite):
def __init__(self, direction, start_x, start_y, color):
super(Sq, self).__init__()
self.color = color
self.start_direction = direction
self.rect = pygame.Rect(start_x, start_y, 20, 20)
# 检查边界,并设置反弹
def check_border(self):
if self.rect.top <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = down_left
# 右上
elif self.start_direction == up_right:
self.start_direction = down_right
elif self.rect.bottom >= windows_height:
# 右下
if self.start_direction == down_right:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = up_left
elif self.rect.left <= 0:
# 左上
if self.start_direction == up_left:
self.start_direction = up_right
# 左下
elif self.start_direction == down_left:
self.start_direction = down_right
elif self.rect.right >= windows_width:
# 右上
if self.start_direction == up_right:
self.start_direction = up_left
# 右下
elif self.start_direction == down_right:
self.start_direction = down_left
# 判断走的方向,并更新数据
def type_judge(self):
if self.start_direction == up_right:
self.rect.top -= move_speed
self.rect.right += move_speed
elif self.start_direction == up_left:
self.rect.top -= move_speed
self.rect.left -= move_speed
elif self.start_direction == down_left:
self.rect.bottom += move_speed
self.rect.left -= move_speed
elif self.start_direction == down_right:
self.rect.bottom += move_speed
self.rect.right += move_speed
def draw_buf(self, windows):
pygame.draw.rect(windows, self.color, self.rect)
# 主程序
def run():
pygame.init()
windows = pygame.display.set_mode((windows_width, windows_height))
pygame.display.set_caption('弹球游戏')
# 小球组
bufs = Group()
for i in range(number//2):
direction = random.choice([1, 3, 7, 9])
color1 = random.randint(0, 255)
color2 = random.randint(0, 255)
color3 = random.randint(0, 255)
color = [color1, color2, color3]
circle_x = random.randint(0, windows_width)
circle_y = random.randint(0, windows_height)
new_buf = Bound(direction, color, circle_x, circle_y)
bufs.add(new_buf)
for i in range(number//2):
direction = random.choice([1, 3, 7, 9])
color1 = random.randint(0, 255)
color2 = random.randint(0, 255)
color3 = random.randint(0, 255)
color = [color1, color2, color3]
start_x = random.randint(0, windows_width)
start_y = random.randint(0, windows_height)
new_buf = Sq(direction, start_x, start_y, color)
bufs.add(new_buf)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
for buf in bufs:
buf.check_border()
buf.type_judge()
buf.draw_buf(windows)
font1 = pygame.font.Font('resoures/myfont.ttf', 100)
tip1 = font1.render("PYTHON 弹小球", True, (255, 0, 0))
windows.blit(tip1, (250, 0))
pygame.display.flip()
pygame.time.delay(1)
windows.fill(black)
if __name__ == '__main__':
run()