大家好,我是查理。今天教大家制作一个简化版球球大作战
话不不多说,上代码
# -*- coding: utf-8 -*-
# @Time : 2018/7/30 16:19
# @Author : G.Hope
# @Email : [email protected]
# @File : 吃球.py
# @Software: PyCharm
import pygame
import random
import math
# 生成随机颜色
def random_color():
return random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
# 判断是否碰撞,并使大球吃掉小球(小球消失,大球变大)
def eat(ball1, ball2):
x1, y1 = ball1['pos']
x2, y2 = ball2['pos']
x_distance = x1 - x2
y_distance = y1 - y2
distance = math.sqrt(x_distance ** 2 + y_distance ** 2)
if distance < ball1['r'] + ball2['r']:
if ball1['r'] > ball2['r']:
ball1['r'] = ball2['r'] + ball1['r']
all_balls.remove(ball2)
else:
ball2['r'] = ball2['r'] + ball1['r']