python pygame实现球球大作战

yipeiwu_com5年前Python基础

本文实例为大家分享了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设计】。

相关文章

100行Python代码实现每天不同时间段定时给女友发消息

100行Python代码实现每天不同时间段定时给女友发消息

每天不同时间段通过微信发消息提醒女友 简介 有时候,你很想关心她,但是你太忙了,以至于她一直抱怨,觉得你不够关心她。你暗自下决心,下次一定要准时发消息给她,哪怕是几句话,可是你又忘记了。...

详解Python中open()函数指定文件打开方式的用法

文件打开方式 当我们用open()函数去打开文件的时候,有好几种打开的模式。 'r'->只读 'w'->只写,文件已存在则清空,不存在则创建。 'a'->追加,写到文件...

python错误:AttributeError: 'module' object has no attribute 'setdefaultencoding'问题的解决方法

Python的字符集处理实在蛋疼,目前使用UTF-8居多,然后默认使用的字符集是ascii,所以我们需要改成utf-8 查看目前系统字符集 复制代码 代码如下: import sys p...

python将多个文本文件合并为一个文本的代码(便于搜索)

但是,当一本书学过之后,对一般的技术和函数都有了印象,突然想要查找某个函数的实例代码时,却感到很困难,因为一本书的源代码目录很长,往往有几十甚至上百个源代码文件,想要找到自己想要的函数实...

Mac安装python3的方法步骤

Mac安装python3的方法步骤

Python有两个版本,一个是2.x版,一个是3.x版,这两个版本是不兼容的。 现在 Mac 上默认安装的 python 版本为 2.7 版本,若 安装 新版本需要 通过 该地址进行下载...