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

import pygame

@unique
class Color(Enum):
"""颜色"""

RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
BLACK = (0, 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 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 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 draw(self, screen):
"""在窗口上绘制球"""
pygame.draw.circle(screen, self.color,
(self.x, self.y), self.radius, 0)

def main():
# 定义用来装所有球的容器
balls = []
# 初始化导入的pygame中的模块
pygame.init()
# 初始化用于显示的窗口并设置窗口尺寸
screen = pygame.display.set_mode((800, 600))
print(screen.get_width())
print(screen.get_height())
# 设置当前窗口的标题
pygame.display.set_caption('大球吃小球')
# 定义变量来表示小球在屏幕上的位置
x, y = 50, 50
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()
# 每隔50毫秒就改变小球的位置再刷新窗口
pygame.time.delay(50)
for ball in balls:
ball.move(screen)
for other in balls:
ball.eat(other)

if __name__ == '__main__':
main()

awsl的更多相关文章

  1. superset的安装(win10)踩踩坑!AWSL

    基本安装参考https://www.jianshu.com/p/8b27ff71429f 按此方案装的时候会遇到各种flask版本不兼容的问题,所以 第一步:装好anaconda 第二部:保证好高于V ...

  2. buaaoo_first_assignment

    四周之前,我不懂面向对象是什么:四周之后,我依然不懂面向对象是什么. 一.第一次作业 (1)实现 说起来,本次作业是最惨烈的一次. 虽然它很简单,但由于未熟悉正则表达式的应用,导致判断wrong fo ...

  3. luogu P5286 [HNOI2019]鱼

    传送门 这题真的牛皮,还好考场没去刚( 这题口胡起来真的简单 首先枚举D点,然后对其他所有点按极角排序,同时记录到D的距离.然后按照极角序枚举A,那么鱼尾的两个点的极角范围就是A关于D对称的那个向量, ...

  4. luogu P5287 [HNOI2019]JOJO

    传送门 神™这题暴力能A,这出题人都没造那种我考场就想到的数据,难怪我的垃圾做法有分 先考虑没有撤销操作怎么做,因为每次插入一段一样的字符,所以我们可以把\(x\)个字符\(c\)定义为\(cx\), ...

  5. 洛谷P4456 交错序列[CQOI2018] dp+数论

    正解:dp 解题报告: 传送门 首先可以先拆下这个贡献式,为了方便之后设状态什么的,把式子转成和ny有关,就成了 \(\sum \left ( n-i \right )^{a}\cdot i^{b}\ ...

  6. WC2019 划水记

    写在前面: 本篇是擅长咕咕咕的\(\text{BLUESKY007}\)同学难得不咕写的游记,将会记录\(WC2019(2019.1.24(Day\ 0)\sim2019.1.30(Day\ 6))\ ...

  7. HBSX2019 3月训练

    Day 1 3月有31天废话 今天先颓过了就只剩30天了 初步计划 每天一道字符串/数据结构题 图论学习 根据<若干图论模型探讨>(lyd)复习 二分图与网络流学习 <算法竞赛进阶指 ...

  8. gym101808 E

    提问:我是什么品种的傻逼? 哇看到积水兴高采烈啊.然后就走上了一条不归路. 为什么不归呢,因为我这个法子就是不对的,我总是在想很多很多点围成的一块区域,然后求这一块区域的面积. 然后尝试了各种扫描方法 ...

  9. gym 101657 D

    理论1A.  //没删debug的文件读入.. 傻逼题. 先求出来每条边两侧的三角形,然后枚举边,根据叉积判断三角形位置,建图,拓扑排序. #include <bits/stdc++.h> ...

随机推荐

  1. OO第四单元博客作业

    OO第四单元博客作业 BUAA_1706_HugeGun 目录 第四单元作业架构设计 四个单元架构设计及OO方法理解 四个单元测试理解与实践演进 课程收获 一点建议 第四单元作业架构设计 ### 第十 ...

  2. H3C 主机单播IP包发送

  3. phpstorm 里能做git的命令行操作吗?

    在VCS菜单下面有 GIT -> Branches 然后会弹出branch菜单,后面怎么操作应该不需要解释吧,所有的branch都列出来自己选 在Tools菜单下面有Open Terminal. ...

  4. iptablesIP规则的保存与恢复

    iptables-save把规则保存到文件中,再由目录rc.d下的脚本(/etc/rc.d/init.d/iptables)自动装载 使用命令iptables-save来保存规则.一般用 iptabl ...

  5. Xshell如何修改字体大小和颜色

    https://jingyan.baidu.com/article/db55b609aac41e4ba30a2f86.html 打开Xshell,点击菜单栏的“文件”->“属性”,或者也可以使用 ...

  6. JS正则验证两位小数,验证数字最简单正则表达式大全

    <h3>输入完按回车后即可验证!</h3> 正整数: <input type="text" size="20" onkeydown ...

  7. 洛谷P2719 搞笑世界杯 题解 概率DP入门

    作者:zifeiy 标签:概率DP 题目链接:https://www.luogu.org/problem/P2719 我们设 f[n][m] 用于表示还剩下n张A类票m张B类票时最后两张票相同的概率, ...

  8. vue 组件的强制刷新

    组件 <vue-component v-if="hackReset"></vue-component> <button @click="a& ...

  9. Talk is cheap. Show me the code.

    Talk is cheap. Show me the code. -- Linux创始人 Linus Torvalds 2000-08-25 Stay hungry Stay foolish -- 乔 ...

  10. java IO的概述和File方法

    IO流用来处理设备之间的数据传输        Java对数据的操作是通过流的方式        Java用于操作流的对象都在IO包中 File类在整个IO包中与文件本身有关的操作类,所有的与文件本身 ...