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设计】。

相关文章

利用python修改json文件的value方法

做工程时遇到需要监听json文件,根据json文件中的key-value值作出相应处理的情形。为此写了修改json文件的python脚本供工程后续调用。 代码如下: # coding...

python实现查询IP地址所在地

python实现查询IP地址所在地

使方法一、用IP138数据库查询域名或IP地址对应的地理位置。 #-*- coding:gbk -*- import urllib2 import re try: while...

numpy ndarray 按条件筛选数组,关联筛选的例子

最近的项目中大量涉及数据的预处理工作,对于ndarray的使用非常频繁。其中ndarray如何进行数值筛选,总结了几种方法。 1.按某些固定值筛选 如下面这段代码从,ndarray中可...

Python原始字符串与Unicode字符串操作符用法实例分析

Python原始字符串与Unicode字符串操作符用法实例分析

本文实例讲述了Python原始字符串与Unicode字符串操作符用法。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 在原始字符串里,所有的字符串都是直接按照...

Python赋值语句后逗号的作用分析

本文实例讲述了Python赋值语句后逗号的作用。分享给大家供大家参考。具体分析如下: IDLE 2.6.2 >>> a = 1 >>> b =...