Python每日练习之图形用户界面和游戏开发(day~10)

from random import randint
from math import sqrt
from enum import Enum,unique
import pygame

# @unique装饰器可以帮助我们检查保证没有重复值
@unique
class Color(Enum):
    # 颜色
    BLACK = (0,0,0)
    GREEN = (0,255,0)
    BLUE = (0,0,255)
    RED = (255,0,0)
    WHITE = (255,255,255)
    GRAY = (242,242,242)
# 定义静态获取随机颜色方法
    @staticmethod
    def random_color():
        r = randint(0,255)
        g = randint(0,255)
        b = randint(0,255)
        return (r,g,b)

# 定义球类
class Ball(object):

    def __init__(self,x,y,radius,sx,sy,color=Color.RED):      # 初始化
        self.x = x
        self.y = y
        self.radius = radius
        self.sx = sx
        self.sy = sy
        self.color = color
        self.alive = True
    def draw(self,screen):          # 在窗口上绘制球
        pygame.draw.circle(screen,self.color,(self.x,self.y),self.radius,0)
        
    def eat(self,other):   # 定义吃方法
        if self.alive and other.alive and self != other:
            dx,dy = self.x - other.x,self.y - other.y
            distance = sqrt(dx ** 2 + dy ** 2)
            if distance < self.radius + other.radius \
                and self.radius > other.radius:
                other.alive = False
                self.radius = self.radius + int(other.radius * 0.146)

    def move(self,screen):          # 定义移动方法
        self.x += self.sx
        self.y += self.sy
        if self.x - self.radius <= 0 or \
                self.x + self.radius >= screen.get_width():
            self.sx = - self.sx
        if self.y - self.radius <= 0 or \
                self.y + self.radius >= screen.get_height():
            self.sy = -self.sy

def main():
    balls = []      # 定义用来装所有球的容器
    pygame.init()    # 初始化导入的pygame中的模块
    screen = pygame.display.set_mode((800,800))      # 初始化用于显示的窗口并设置窗口尺寸
    pygame.display.set_caption('达尔文进化')      # 设置当前窗口的标题
    running = True
    while running:      # 开启一个事件循环处理发生的事件
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
            if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1:
                # 获得点击鼠标的位置
                x,y = event.pos
                radius = randint(10,100)
                sx,sy = randint(-10,10),randint(-10,10)
                color = Color.random_color()
                ball = Ball(x,y,radius,sx,sy,color)   # 在点击鼠标的位置创建一个球(大小、速度和颜色随机)
                # 将球添加到列表容器中
                balls.append(ball)
        screen.fill((255,255,255))
        for ball in balls:          # 取出容器中的球 如果没被吃掉就绘制 被吃掉了就移除
            if ball.alive:
                ball.draw(screen)
            else:
                balls.remove(ball)
        pygame.display.flip()
        pygame.time.delay(50)          # 每隔50毫秒就改变球的位置再刷新窗口
        for ball in balls:
            ball.move(screen)
            for other in balls:              # 检查球有没有吃到其他的球
                ball.eat(other) 

if __name__ == '__main__':
    main()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值