#cartoon
import pygame
import sys #退出程序时要用
from pygame.locals import *
from random import *
import math
#继承本身自带的类即可,本身定义了精灵
class Ball(pygame.sprite.Sprite):
#实例化球对象
def __init__(self,grayball_image,greenball_image,position,speed,bg_size,target):
pygame.sprite.Sprite.__init__(self)#调用该类
self.grayball_image = pygame.image.load(grayball_image).convert_alpha()
self.greenball_image = pygame.image.load(greenball_image).convert_alpha()
self.rect = self.greenball_image.get_rect()#获取球的大小
self.rect.left,self.rect.top = position #希望球在position的位置上显示
self.speed = speed #赋值速度
self.target = target
self.control = False
self.width,self.height = bg_size[0],bg_size[1]
def move(self):
self.rect = self.rect.move(self.speed)
#实现小球从左边出去,从右边进来,从上边出去,从下面进来
if self.rect.right <0: #球出右边
self.rect.left = self.width
if self.rect.left >self.width:
self.rect.right = 0
if self.rect.bottom <0:
self.rect.top = self.height
if self.rect.top > self.height:
self.rect.bottom = 0
#用于判断鼠标在1秒内产生的事件数量是否匹配此目标
def check(self,motion):
if self.target < motion <self.target +5 :
return True
else:
return False
#设定玻璃板,在玻璃板内移动鼠标,给每个小球设定不同的频率(响应事件数量),鼠标移动,
#当鼠标移动的频率(产生一定的事件数量)和某个小球的频率一致时,小球产生响应
class Glass(pygame.sprite.Sprite):
def __init__(self,glass_image,mouse_image,bg_size):
pygame.sprite.Sprite.__init__(self)#调用该类
self.glass_image = pygame.image.load(glass_image).convert_alpha()
self.glass_rect = self.glass_image.get_rect()
#摆放玻璃面板的位置在屏幕下方的正中间,x=(屏幕的宽度-面板宽度)/2
self.glass_rect.left,self.glass_rect.top = \
(bg_size[0] - self.glass_rect.width) // 2 ,\
bg_size[1] - self.glass_rect.height
self.mouse_image = pygame.image.load(mouse_image).convert_alpha()
self.mouse_rect = self.mouse_image.get_rect()
#给鼠标一个初始位置
self.mouse_rect.left,self.mouse_rect.top = \
self.glass_rect.left,self.glass_rect.top
#把真实的鼠标设置为不可见,然后显示自己的鼠标图片
pygame.mouse.set_visible(False)
#target是除了Item(1个球),的四个球
def collide_check(item,target):
collide_ball = []
for each in target:
distance = math.sqrt(\
math.pow((item.rect.center[0] - each.rect.center[0]), 2) + \
math.pow((item.rect.center[1] - each.rect.center[1]), 2))
if distance <= (item.rect.width + each.rect.width) / 2:
collide_ball.append(each)
return collide_ball
def main():
pygame.init()
grayball_image = "gray_ball.png"
greenball_image = "green_ball.png"
bg_image = "background_ball.png"
glass_image = "glass.png"
mouse_image = "hand.png"
running = True
bg_size = width,height = 1024,681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("play the ball")
#背景音乐
pygame.mixer.music.load("bg_music.ogg")
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play()
#添加音效
loser_sound = pygame.mixer.Sound("loser.wav")
laugh_sound = pygame.mixer.Sound("laugh.wav")
winner_sound = pygame.mixer.Sound("winner.wav")
hole_sound = pygame.mixer.Sound("hole.wav")
#音乐播放结束时,游戏结束,USEEVENT,可以自定义事件,USEEVENT+1,USEEVENT+2,,,,,
GAMEOVER = USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER) #音乐播放结束时,就会发送自定义事件game_over到事件队列
#添加自定义事件,每隔1秒,触发一次此事件,1000毫秒 = 1秒
MYTIMER = USEREVENT +1
pygame.time.set_timer(MYTIMER,1*1000)
balls= []
#产生五个随机大小相同的球
for i in range(5):
position = randint(0,width-100),randint(0,height-100) #球宽是100,避免球越界
speed = [randint(-10,10),randint(-10,10)]
#5*(i+1),产生事件数量,从0—4
ball = Ball(grayball_image,greenball_image,position,speed,bg_size,6*(i+1))
#如果产生的球发生碰撞,重新再设置球的位置
while collide_check(ball,balls):
ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
balls.append(ball)
clock = pygame.time.Clock()
background =pygame.image.load(bg_image).convert_alpha()
#调用类
glass = Glass(glass_image,mouse_image,bg_size)
#记录每秒钟产生的事件数量
motion = 0
while running:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == GAMEOVER:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running = False #结束循环
if event.type == MYTIMER:
if motion: #如果产生事件
for each in balls:
if each.check(motion):
each.speed = [0,0]
each.control = True
motion = 0
if event.type == MOUSEMOTION: #鼠标移动,计数事件
motion +=1
#如果按下W,A,S,D,表示绿色小球受玩家控制
if event.type == KEYDOWN:
if event.type == K_w:
for each in balls:
if each.control:
each.speed[1] -= 1 #向上垂直加速运动
if event.type == K_s:
for each in balls:
if each.control:
each.speed[1] += 1 #向下垂直加速运动
if event.type == K_a:
for each in balls:
if each.control:
each.speed[0] -= 1 #向左垂直加速运动
if event.type == K_d:
for each in balls:
if each.control:
each.speed[0] += 1 #向右垂直加速运动
screen.blit(background,(0,0))
screen.blit(glass.glass_image,glass.glass_rect)
glass.mouse_rect.left,glass.mouse_rect.top = pygame.mouse.get_pos() #获取鼠标当前的位置
if glass.mouse_rect.left <glass.glass_rect.left : #出了玻璃面板的左侧
glass.mouse_rect.left = glass.glass_rect.left
if glass.mouse_rect.left >glass.glass_rect.right - glass.mouse_rect.width:
glass.mouse_rect.left = glass.glass_rect.right - glass.mouse_rect.width
if glass.mouse_rect.top <glass.glass_rect.top:
glass.mouse_rect.top = glass.glass_rect.top
if glass.mouse_rect.top >glass.glass_rect.bottom -glass.mouse_rect.height :
glass.mouse_rect.top = glass.glass_rect.bottom -glass.mouse_rect.height
screen.blit(glass.mouse_image,glass.mouse_rect)
for each in balls:
each.move()
#如果球的频率和鼠标移动的频率相同,表表示小球被控制,显示绿色
#灰色表示没有被控制
if each.control:
screen.blit(each.greenball_image,each.rect)
else:
screen.blit(each.grayball_image,each.rect)
#移动后开始检测小球的碰撞情况
for i in range(5):
item = balls.pop(i)
if collide_check(item,balls): #collide_ball不为空
item.speed[0] = -item.speed[0]
item.speed[1] = -item.speed[1]
balls.insert(i,item)
pygame.display.flip()
clock.tick(30)
if __name__ =="__main__":
main()