import pygame
import random
import math
# random_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800, 450))
screen.fill((255, 255, 255))
pygame.display.set_caption('bll game 2')
pygame.display.flip()
# all_balls中保存多个球
# 每个球要保存:半径、圆心坐标、颜色、x速度、y速度
all_balls = [
{
'r': random.randint(10,30),
'pos':(100, 100),
'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
'x_speed': random.randint(-3, 3),
'y_speed': random.randint(-3, 3)
},
{
'r': random.randint(10, 30),
'pos': (200, 200),
'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
'x_speed': random.randint(-3, 3),
'y_speed': random.randint(-3, 3)
},
{
'r': random.randint(10, 30),
'pos': (300, 300),
'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
'x_speed': random.randint(-3, 3),
'y_speed': random.randint(-3, 3)
}
]
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
if event.type == pygame.MOUSEBUTTONDOWN:
ball_new = {
'r': random.randint(10, 30),
'pos': event.pos,
'color': (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
'x_speed': random.randint(-3, 3),
'y_speed': random.randint(-3, 3)
}
all_balls.append(ball_new)
# 刷新界面
screen.fill((255, 255, 255))
pygame.time.delay(10)
for ball in all_balls:
x, y = ball['pos']
x_speed = ball['x_speed']
y_speed = ball['y_speed']
x += x_speed
y += y_speed
# 边界检测
if x + ball['r'] >= 800:
ball['x_speed'] *= -1
if x <= ball['r']:
ball['x_speed'] *= -1
if y + ball['r'] >= 450:
ball['y_speed'] *= -1
if y <= ball['r']:
ball['y_speed'] *= -1
for ball1 in all_balls:
for ball2 in all_balls:
if ball1 == ball2:
continue
distance = ball1['r'] + ball2['r']
x_edge1, y_edge1 = ball1['pos']
x_edge2, y_edge2 = ball2['pos']
if distance >= math.sqrt((x_edge1 - x_edge2) * (x_edge1 - x_edge2) +
(y_edge1 - y_edge2) * (y_edge1 - y_edge2)):
index_random = random.randint(0, 1)
if index_random:
ball_del = ball1
ball2['r'] += ball1['r']
all_balls.remove(ball_del)
break
else:
ball_del = ball2
ball1['r'] += ball2['r']
all_balls.remove(ball_del)
pygame.draw.circle(screen, ball['color'], (x,y), ball['r'])
# 更新球对应的坐标
ball['pos'] = x, y
pygame.display.update()
ball_game3.gif
简单的碰撞
import pygame
def draw_ball(local, color, pos):
"""
画球
:param local:
:return:
"""
pygame.draw.circle(local, color, pos, 20)
# 方向对应的key值
up = 273
down = 274
left = 276
right = 275
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((800, 450))
screen.fill((255, 255, 255))
pygame.display.set_caption('ball game')
pygame.display.flip()
# 保存初始坐标
ball_x = 100
ball_y = 100
x_speed = 10
y_speed = 0
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == up:
y_speed = -10
x_speed = 0
elif event.key == down:
y_speed = 10
x_speed = 0
elif event.key == right:
x_speed = 10
y_speed = 0
elif event.key == left:
x_speed = -10
y_speed = 0
# 刷新屏幕
screen.fill((255, 255, 255))
# 设置初始方向
ball_x += x_speed
ball_y += y_speed
# 边界检测
if ball_x + 20 >= 800:
ball_x = 800 - 20
x_speed *= -1
if ball_x <= 20:
ball_x = 20
x_speed *= -1
if ball_y + 20 >= 450:
ball_y = 450 - 20
y_speed *= -1
if ball_y <= 20:
ball_y = 20
y_speed *= -1
# 设置障碍
pygame.draw.rect(screen, (0, 0, 0), (300, 300, 100,10))
if ball_x - 20 <= 300 + 100 and ball_x + 20 >= 300 and ball_y - 20 <= 310 and ball_y + 20 >= 300:
x_speed = 0
y_speed = 0
font = pygame.font.SysFont('SimSun', 50)
title = font.render('Game Over', True, (255, 0, 0))
screen.blit(title,(300, 180))
pygame.time.delay(17)
draw_ball(screen, (255, 255, 0), (ball_x, ball_y))
pygame.display.update()
ball_game4.gif
网友评论