实现鼠标点击屏幕产生小球,小球自动移动,与屏幕碰撞会反弹。小球之间碰撞,会随机吃掉。
import pygame
import yt_color
from random import randint
"""
游戏功能:点击屏幕在点击的地方产生一个球,球可以自由的移动,撞到边界会弹回,撞到其他的球会吃掉
第一步:搭建游戏窗口,
第二步:点击屏幕
第三步:让球动起来(需要用列表来保存所有的球,需要用字典来保存每个球的信息)
"""
#全局变量
WINDOW_WIDTH = 600
WINDOW_HIGHT = 400
key_ball_color = 'ball_color'
key_ball_center = 'ball_center'
key_ball_radius = 'ball_radius'
key_ball_xspeed = 'ball_xspeed'
key_ball_yspeed = 'ball_yspeed'
key_ball_alive = 'ball_alive'
all_balls = [] # 保存所有的球
def ball_crash():
"""
检测碰撞
:看屏幕上的每个球是否和其他的球的圆心距<=半径和
:return:
"""
# 拿第一个球
for ball in all_balls:
# 拿第二个球
for other in all_balls:
# 是同一个球或者是已经消失的球都不需要再判断
if ball == other or not ball[key_ball_alive] or not other[key_ball_alive]:
continue
# 判断两次拿到的球
x1,y1 = ball[key_ball_center]
x2,y2 = other[key_ball_center]
# 计算两个球的圆心距
distance = ((x1-x2)**2 + (y1-y2)**2)**0.5
if distance <= ball[key_ball_radius] + other[key_ball_radius]:
# 相撞后:
if randint(0,1):
ball[key_ball_radius] += int(other[key_ball_radius]*0.5)
other[key_ball_alive] = False
else:
other[key_ball_radius] += int(ball[key_ball_radius] * 0.5)
ball[key_ball_alive] = False
def draw_all_ball(window):# 重新画所有的球
window.fill(yt_color.white)
for ball in all_balls:
if ball[key_ball_alive]:
pygame.draw.circle(window, ball[key_ball_color], ball[key_ball_center], ball[key_ball_radius])
else:
all_balls.remove(ball)
pygame.display.update()
def ball_move():
"""
球动起来
:return:
"""
for ball in all_balls:
# 获取新的原点
ball_x,ball_y = ball[key_ball_center]
new_x,new_y = ball_x + ball[key_ball_xspeed],ball_y + ball[key_ball_yspeed]
# 做边界检测
# x方向的边界
if new_x < ball[key_ball_radius]:
new_x = ball[key_ball_radius]
ball[key_ball_xspeed] *= -1
elif new_x > WINDOW_WIDTH:
new_x = WINDOW_WIDTH - ball[key_ball_radius]
ball[key_ball_xspeed] *= -1
# y方向的边界
if new_y < ball[key_ball_radius]:
new_y = ball[key_ball_radius]
ball[key_ball_yspeed] *= -1
elif new_y > WINDOW_HIGHT:
new_y = WINDOW_HIGHT - ball[key_ball_radius]
ball[key_ball_yspeed] *= -1
# 修改圆心坐标
ball[key_ball_center] = new_x,new_y
def creat_ball(window, pos):
"""
在指定的位置产生一个随机颜色的球。
:param window:
:param pos:
:return:
"""
ball_color = yt_color.rand_color()
ball_center = pos
ball_radius = randint(10,30)
ball = {key_ball_color:ball_color,
key_ball_center:ball_center,
key_ball_radius:ball_radius,
key_ball_xspeed:randint(-5,5),
key_ball_yspeed:randint(-5,5),
key_ball_alive:True
}
all_balls.append(ball)
pygame.draw.circle(window, ball_color, ball_center, ball_radius)
pygame.display.update()
def main_game():
# 初始化游戏
pygame.init()
window = pygame.display.set_mode((WINDOW_WIDTH,WINDOW_HIGHT))
window.fill(yt_color.white)
# 进入游戏界面默认显示的内容和要执行的操作。
pygame.display.flip()
# 游戏循环
while True:
# 游戏循环执行的代码
ball_move() # 修改球的位置
pygame.time.delay(10)
ball_crash() #检测求的碰撞
draw_all_ball(window) # 重新画所有的球
# 事件检测
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
# 事件发生要执行的操作写在这个下面。。。
# 1、鼠标按下
elif event.type == pygame.MOUSEBUTTONDOWN:
creat_ball(window, event.pos)
if __name__ == '__main__':
main_game()
定义部分颜色的模块
import random
# 白色
white = (255, 255, 255)
# 黑色
black = (0, 0 , 0)
# 红色
red = (255, 0, 0)
# 绿色
green = (0, 255, 0)
# 蓝色
blue = (0, 0, 255)
# 灰色
gray = (120, 120 ,120)
def rand_color():
"""
随机颜色
:return:
"""
return (random.randint(0,255), random.randint(0,255), random.randint(0,255))
网友评论