吃球:
'''__author__==fubo'''
'''
'''
import pygame
from random import randint
from math import sqrt
import math
def rand_color():
return randint(0,255),randint(0,255),randint(0,255)
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,400))
screen.fill((255,255,255))
pygame.display.flip()
# all_balls中保存多个信息
# 每个球要保存:半径、圆心坐标、颜色、x速度、y速度
all_balls = [
{'r': randint(10, 25),
'pos':(200,300),
'color': (255,0,0),
'x_speed': 3,
'y_speed': 0},
{'r': randint(10, 25),
'pos': (300, 140),
'color': (0, 255, 0),
'x_speed': 1,
'y_speed': 0},
{'r': randint(10, 25),
'pos': (130, 240),
'color': (0,0, 255),
'x_speed': 0,
'y_speed': 2}
]
ball_dict = {}
while True:
screen.fill((255, 255, 255))
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
if event.type==pygame.MOUSEBUTTONDOWN:
print(event.pos)
random_color=rand_color()
ball_dict={'r':randint(10,25),
'pos':event.pos,
'color':random_color,
'x_speed':randint(-3,3),
'y_speed': randint(-3, 3)}
all_balls.append(ball_dict)
pygame.draw.circle(screen,ball_dict['color'],ball_dict['pos'],ball_dict['r'])
for item in all_balls:
x, y = item['pos']
x_speed = item['x_speed']
y_speed = item['y_speed']
x += x_speed
y += y_speed
if x+item['r']>=600:
x=600-item['r']
x_speed=-x_speed
item['x_speed']=x_speed
if x - item['r']<=0:
x = 0 +item['r']
x_speed = -x_speed
item['x_speed'] = x_speed
if y+item['r']>=400:
y=400-item['r']
y_speed=-y_speed
item['y_speed'] = y_speed
if y - item['r']<=0:
y = 0 +item['r']
y_speed = -y_speed
item['y_speed'] = y_speed
pygame.time.delay(10)
pygame.draw.circle(screen, item['color'], (x, y), item['r'])
item['pos'] = x, y
num=0
for item1 in all_balls[:]:
if item1['r']>=150:
item1['r']=0
print('sfg')
all_balls.remove(item1)
break
for item1 in all_balls[:]:
dict2=all_balls.copy()
num+=1
x1,y1=item1['pos']
r1=item1['r']
for mul in range(num,len(dict2)):
x2,y2=dict2[mul]['pos']
r2=all_balls[mul]['r']
if (r1+r2)>=int(sqrt((x1-x2)**2+(y1-y2)**2)):
if r1>r2:
item1['r']= r1+r2
all_balls.pop(mul)
break
else:
all_balls[mul]['r'] = r1 + r2
all_balls.remove(item1)
break
pygame.display.update()
结果:
吃2.JPG
作业2
'''__author__==fubo'''
'''
'''
import pygame
from random import randint
def draw_button(address):
#
font = pygame.font.SysFont('Times', 60)
title = font.render('gameover', True, (255,0,0))
screen.blit(title, (250, 300))
def get_rect(address,pos):
# 画反弹板
x1, y1 = event.pos
point_x1=x1-int(width/2)
point_y1=y1-int(hight/2)
pygame.draw.rect(address,(0,255,0), (point_x1,point_y1,width,hight))
return point_x1,point_y1
def is_in_rect(pos,rect):
# 判断鼠标是否在反弹板上
x1,y1=event.pos
point_x1,point_y1,width,hight=rect
if (point_x1<x1<point_x1+width) and (point_y1<y1<point_y1+hight):
return True
return False
def set_ball(address,point_x,point_y,ridius):
# 画圆
pygame.draw.circle(address,(randint(0,255),randint(0,255),randint(0,255)),(point_x,point_y),ridius)
if __name__ == '__main__':
pygame.init()
screen=pygame.display.set_mode((600,650))
screen.fill((250,250,250))
# 圆的圆心
point_x = randint(25, 575)
point_y = randint(25, 625)
# 圆的半径
ridius = randint(15, 25)
# 板的坐标
point_x1 = randint(0, 540)
point_y1 = randint(0, 610)
# 板的长宽
hight=randint(20, 40)
width = randint(50, 100)
set_ball(screen,point_x,point_y,ridius)
pygame.draw.rect(screen,(0,255,0),(point_x1,point_y1,width,hight),0)
pygame.display.flip()
# 球的速度
x_speed=randint(-4,4)
y_speed=randint(-4,4)
mul = 0
flage=False
if point_y1>=325:
flage=True
while True:
num1=0
num2= 0
num3= 0
num4=0
screen.fill((255,255,255))
point_y+=y_speed
point_x+=x_speed
# 宽的出界判断
if point_x+ridius>600:
point_x=600-ridius
x_speed=-x_speed
if point_x - ridius <0:
point_x = 0 +ridius
x_speed = -x_speed
# 高的出界判断,并判断保护方向,及木板反弹方向
if point_y+ridius>650 and not flage:
point_y=650-ridius
y_speed=-y_speed
elif point_y+ridius>650 and flage:
print('你输了')
break
if point_y-ridius<=0 and flage:
point_y=0+ridius
y_speed=-y_speed
elif point_y-ridius<=0 and not flage:
print('你输了')
break
set_ball(screen, point_x, point_y, ridius)
pygame.draw.rect(screen, (0, 255, 0), (point_x1, point_y1, width, hight), 0)
for event in pygame.event.get():
if event.type==pygame.QUIT:
exit()
if event.type==pygame.MOUSEBUTTONDOWN: #点击木板
if is_in_rect(event.pos,(point_x1, point_y1, width, hight)):
num1,num2=event.pos
mul=1
if event.type==pygame.MOUSEMOTION and mul==1:#移动木板
point_x1,point_y1=get_rect(screen,event.pos)
if event.type==pygame.MOUSEBUTTONUP and mul==1:#放开木板
mul=0
if flage: #反弹球
if point_y < point_y1 and abs(point_y1 - point_y) == ridius:
if point_x1 <= point_x <= point_x1 + width:
y_speed = -y_speed
else:
if point_y > point_y1 and abs(point_y1 + hight - point_y) == ridius:
if point_x1 <= point_x <= point_x1 + width:
y_speed = -y_speed
pygame.time.wait(40)
pygame.display.update()
draw_button(screen)
结果:
作业2.JPG
作业22.JPG
网友评论