import pygame,sys,math
from pygame.locals import *
from random import *
import pygame.mixer
class Ball(pygame.sprite.Sprite):
def __init__(self,grayball_img,greenball_img,position,speed,target):
super().__init__()
self.grayball_image=pygame.image.load(grayball_img).convert_alpha()
self.greenball_image=pygame.image.load(greenball_img).convert()#convert才能用后面的透明度方法!!!!!!!!!
self.greenball_image.set_colorkey((0, 0, 0))
self.greenball_image.set_alpha(200)
self.rect=self.grayball_image.get_rect()
self.rect.left,self.rect.top=position
self.speed=speed
self.control=False
self.target=target
self.radius=self.rect.width//2
def move(self):
self.rect=self.rect.move(self.speed)
width=1024
height=681
if self.rect.right<0:
self.rect.left=width
elif self.rect.left>width:
self.rect.right=0
if self.rect.top>height:
self.rect.bottom=0
elif self.rect.bottom<0:
self.rect.top=height
def check(self,motion):
if self.target
return True
else:
return False
class Glass(pygame.sprite.Sprite):
def __init__(self,glass_img,mouse_img):
self.glass_img=pygame.image.load(glass_img).convert()
self.glass_img.set_alpha(40)
self.rect=self.glass_img.get_rect()
self.rect.left=(1024-self.rect.width)//2
self.rect.bottom=681
self.mouse_img=pygame.image.load(mouse_img).convert_alpha()
self.mouse_rect=self.mouse_img.get_rect()
self.mouse_rect.center=self.rect.center
pygame.mouse.set_visible(False)
def main():
clock = pygame.time.Clock()
pygame.init()
#BGM
#pygame.mixer.music.load('Windows 电话拨出.wav')
#pygame.mixer.music.play()
#pygame.mixer.music.set_endevent(Gameover)自定义gameover,可以判断even.type=gameover
#音效则
#sound=pygame.mixer.Sound('')
#sound.play()
grayball_img='20190412174532211.png'
greenball_img='173303tm7zwkjmjmkfpp7h.png'
bg_img='20190412174635626.png'
glass_img='173303pkoonwoqjojz5oh5.png'
mouse_img='173303gc7ftmcm48ccutcf.png'
hole=[(115,125,195,207),(229,252,390,398),(501,507,320,328),(681,705,192,196),(902,912,416,426)]
screen=pygame.display.set_mode((1024,681))
width=1024
height=681
background=pygame.image.load(bg_img).convert_alpha()
pygame.display.set_caption('play ball')
screen.blit(background,(0,0))
glass=Glass(glass_img,mouse_img)
Balls=[]
group=pygame.sprite.Group()
for i in range(5):
position=(randint(0,width),randint(0,height))
speed=[randint(-10,10),randint(-10,10)]
ball=Ball(grayball_img,greenball_img,position,speed,5*(i+1))
Balls.append(ball)
group.add(ball)
for each in group:
group.remove(each)
while pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
each.rect.left,each.rect.top=(randint(0,width),randint(0,height))
group.add(each)
motion=0
ATIMER=pygame.USEREVENT +1
pygame.time.set_timer(ATIMER,1000)#触发事件可以用这个!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!永久有效!@!!!!!!!!!!!!!!!!
pygame.key.set_repeat(100,100)
while 1:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
elif event.type==ATIMER:
if motion:
for each in group:
if each.check(motion):
each.speed=[0,0]
each.control=True
motion=0
elif event.type==MOUSEMOTION:
motion+=1
elif event.type==KEYDOWN:
if event.key==K_w:
for each in group:
if each.control:
each.speed[1]-=1
if event.key==K_s:
for each in group:
if each.control:
each.speed[1]+=1
if event.key==K_a:
for each in group:
if each.control:
each.speed[0]-=1
if event.key==K_d:
for each in group:
if each.control:
each.speed[0]+=1
if event.key==K_SPACE:
for each in Balls:
if each.control:
for i in hole:
if i[0]<=each.rect.left<=i[1] and i[2]<=each.rect.top<=i[3]:
hole.remove(i)
group.remove(each)
each.speed=[0,0]
temp=Balls.pop(Balls.index(each))
Balls.insert(0,temp)
if not hole:
sys.exit()
screen.blit(background,(0,0))
screen.blit(glass.glass_img,glass.rect)
glass.mouse_rect.left,glass.mouse_rect.top=pygame.mouse.get_pos()#!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!不能直接glass.mouse_rect=pygame.mouse.get_pos()
if glass.mouse_rect.left
glass.mouse_rect.left=glass.rect.left
if glass.mouse_rect.right>glass.rect.right:
glass.mouse_rect.right=glass.rect.right
if glass.mouse_rect.top
glass.mouse_rect.top=glass.rect.top
screen.blit(glass.mouse_img,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 each in group:
group.remove(each)
item=pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle)
if item:
each.control=False
item[0].control=False
while 1:
each.speed=[randint(-10,10),randint(-10,10)]
item[0].speed=[randint(-10,10),randint(-10,10)]
each.move()
item[0].move()
if pow((pow((each.rect.left-item[0].rect.left),2)+pow((each.rect.top-item[0].rect.top),2)),0.5)>each.rect.width:#这儿有个小瑕疵!!!!!!!!!!!!!!!!!!
break
group.add(each)
pygame.display.flip()
clock.tick(30)
if __name__=='__main__':
main()