圆月山庄资源网 Design By www.vgjia.com

本文实例为大家分享了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,pygame,球

圆月山庄资源网 Design By www.vgjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
圆月山庄资源网 Design By www.vgjia.com

稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!

昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。

这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。

而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?