首页 > python爬虫

python pygame实现球球大作战

时间:2021-02-01 python爬虫 查看: 1405

本文实例为大家分享了python pygame球球大作战的具体代码,供大家参考,具体内容如下

球球大作战:(大球吃小球,代码如下:)

from random import randint,randrange
import pygame
from math import sqrt,pi


class Ball(object):
  def __init__(self, center, color, radius, sx, sy):
    self._center = center
    self._color = color
    self._radius = radius
    self._sx = sx
    self._sy = sy

  @property
  def center(self):
    return self._center

  @property
  def radius(self):
    return self._radius

  @radius.setter
  def radius(self,radius):
    self._radius = radius

  def move(self):
    x, y = self._center[0], self._center[1]
    x += self._sx
    y += self._sy
    self._center = (x, y)
    # if x + self._radius > 800:
    #   self._sx = -abs(self._sx)
    # elif x + self._radius < 0:
    #   self._sx = abs(self._sx)
    # elif y +self._radius > 800:
    #   self._sy = -abs(self._sy)
    # elif y +self._radius < 0:
    #   self._sy = abs(self._sy)
    if x + self._radius >= 800 or x - self._radius <= 0 or x <= 0:
      self._sx = -self._sx
    if y +self._radius >= 800 or y - self._radius <= 0 or y <= 0:
      self._sy = -self._sy

  def draw(self,screen):
    pygame.draw.circle(screen, self._color, self._center, self._radius, 0)

  def eat(self, other):
    a = sqrt((self._center[0] - other.center[0]) ** 2 + (self._center[1] - other.center[1]) ** 2)
    if a < self._radius + other.radius and self._radius < other.radius:
      other.radius = self._radius + other.radius
      self.radius = 0
    elif a < self._radius + other.radius and self._radius > other.radius:
      self._radius = self._radius + other.radius
      other.radius = 0


def main():
  balls = []
  pygame.init()
  screen = pygame.display.set_mode([800,800])
  pygame.display.set_caption('大球吃小球')
  c = pygame.time.Clock()
  running = True
  while running:
    for event in pygame.event.get():
      if event.type == pygame.QUIT:
        running = False
      elif event.type == pygame.MOUSEBUTTONDOWN and \
         event.button == 1:
        color = random_color()
        radius = randint(10,100)
        sx, sy = randint(-10,10), randint(-10,10)
        ball = Ball(event.pos, color, radius, sx, sy)
        balls.append(ball)
    refresh(screen,balls)
    c.tick(20) # 50帧
    for ball in balls:
      ball.move()
    balls_len = len(balls)
    for i in range(balls_len):
      for x in range(balls_len):
        balls[i].eat(balls[x])
    for ball in balls:
      if ball.radius == 0:
        balls.remove(ball)


  pygame.quit()


def refresh(screen,balls):
  bg_color = [255, 255, 255]
  screen.fill(bg_color)
  for ball in balls:
    ball.draw(screen)
  pygame.display.flip()


def random_color():
  return [randint(1,255), randint(1,255), randint(1,255)]


if __name__ == '__main__':
  main()

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持python博客。

展开全文
上一篇:python对Excel按条件进行内容补充(推荐)
下一篇:python实现差分隐私Laplace机制详解
输入字:
相关知识
Python爬虫基础之爬虫的分类知识总结

来给大家讲python爬虫的基础啦,首先我们从爬虫的分类开始讲起,下文有非常详细的知识总结,对正在学习python的小伙伴们很有帮助,需要的朋友可以参考下

Python爬虫基础讲解之请求

今天带大家了解一下python爬虫的基础知识,文中有非常详细的解释说明,对正在学习python爬虫的小伙伴们有很好地帮助,需要的朋友可以参考下

PyQt5爬取12306车票信息程序的实现

12306是学习爬虫的比较好的一个练手网站。本文主要实现了PyQt5爬取12306车票信息程序,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

Python爬虫之m3u8文件里提取小视频的正确姿势

本文给大家分享如何正确提取m3u8文件里的.ts视频,并合成完整的.mp4格式视频,通过图文实例代码的形式给大家介绍的非常详细,对Python提取m3u8文件小视频感兴趣的朋友一起看看吧