day11 作业 2018-07-30
2018-07-30 本文已影响0人
LPP27149
游戏截图1
游戏截图1
# -*- coding: utf-8 -*-
"""
-------------------------------------------------
File Name: 06-多个球一起动
Description :
Author : LPP
date: 2018/7/30
-------------------------------------------------
Change Activity:
2018/7/30:
-------------------------------------------------
"""
import pygame
from random import randint, randrange
from math import fabs
rand_color = (randint(0, 255), randint(0, 255), randint(0, 255))
print(fabs(-4))
if __name__ == '__main__':
pygame.init()
screen = pygame.display.set_mode((1600, 900))
screen.fill((255, 255, 255))
pygame.display.flip()
# all_balls 中保存多个球
# 每个球保存: 半径, 圆心坐标, 颜色, x速度, y速度
all_balls = [
{
'r':randint(10, 20),
'pos': (randint(0, 100), randint(0, 100)),
'color': rand_color,
'x_speed': randrange(-5, 5, 2),
'y_speed': randrange(-5, 5, 2)
},
{
'r': randint(10, 20),
'pos': (100, 100),
'color': (randint(0, 255), randint(0, 255), randint(0, 255)),
'x_speed': randrange(-5, 5, 2),
'y_speed': randrange(-5, 5, 2)
},
{
'r': randint(10, 20),
'pos': (1408, randint(0, 100)),
'color': (randint(0, 255), randint(0, 255), randint(0, 255)),
'x_speed': randrange(-5, 5, 2),
'y_speed': randrange(-5, 5, 2)
}
]
font = pygame.font.Font('./aa.ttf', 30)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
ball = {
'r': randint(10, 25),
'color': (randint(0, 255), randint(0, 255), randint(0, 255)),
'pos': event.pos,
'x_speed': randrange(-5, 5, 2),
'y_speed': randrange(-5, 5, 2)
}
all_balls.append(ball)
if len(all_balls) == 1:
screen.fill((255, 255, 255))
font = pygame.font.Font('./aa.ttf', 150)
tips_font = pygame.font.Font('./aa.ttf', 30)
text = font.render('GAME OVER', True, (0, 0, 0))
tips = tips_font.render('点击任意位置关闭游戏', True, (0, 0, 0))
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
exit()
screen.blit(tips, (700, 440))
screen.blit(text, (500, 300))
pygame.display.update()
if event.type == pygame.MOUSEBUTTONDOWN:
exit()
else:
# 刷新界面
screen.fill((255, 255, 255))
for ball_dict in all_balls:
# 取出速度及坐标
x, y = ball_dict['pos']
x_speed = ball_dict['x_speed']
y_speed = ball_dict['y_speed']
x += x_speed
y += y_speed
pygame.draw.circle(screen, ball_dict['color'], (x, y), ball_dict['r'])
ball_dict['pos'] = x, y
x, y = ball_dict['pos']
if x + ball_dict['r'] >= 1600:
x = 1600
ball_dict['x_speed'] *= -1
if x - ball_dict['r'] <= 0:
x = 0
ball_dict['x_speed'] *= -1
if y + ball_dict['r'] >= 900:
y = 900
ball_dict['y_speed'] *= -1
if y - ball_dict['r'] <= 0:
y = 0
ball_dict['y_speed'] *= -1
for balls1 in all_balls:
for balls2 in all_balls:
if not (balls1 == balls2):
x1, y1 = balls1['pos']
x2, y2 = balls2['pos']
r1 = balls1['r']
r2 = balls2['r']
if (fabs(x2-x1) <= fabs(r2+r1)) and (fabs(y2-y1) <= fabs(r2+r1)):
if r1 >= r2:
balls1['r'] += int(r2/7)
all_balls.remove(balls2)
else:
balls2['r'] += int(r1 / 7)
all_balls.remove(balls1)
text = font.render('剩余数量: %d' % len(all_balls), True, (0, 0, 0))
for x in range(0, 1600, 50):
pygame.draw.line(screen, (randint(0, 255), randint(0, 255), randint(0, 255)),(x, 0), (x, 900))
for y in range(0, 900, 50):
pygame.draw.line(screen, (randint(0, 255), randint(0, 255), randint(0, 255)),(0, y), (1600, y))
screen.blit(text, (20, 20))
pygame.display.update()