Commit 870cb975 by BellCodeEditor

save project

parent 740bcb90
File added
glass.png

32.4 KB

hand.png

1.63 KB

File added
File added
File added
......@@ -5,12 +5,15 @@ from random import *
import math
class Ball(pygame.sprite.Sprite):
def __init__(self,image,position,speed,bg_size):
#初始化动画精灵
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load(image).convert_alpha()
self.rect=self.image.get_rect()
#将小球放到指定位置
self.rect.left,self.rect.top=position
self.speed=speed
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:
......@@ -21,6 +24,7 @@ class Ball(pygame.sprite.Sprite):
self.rect.top=self.height
elif self.rect.top>self.height:
self.rect.bottom=0
def collide_check(item,target):
col_balls=[]
for each in target:
......@@ -28,18 +32,42 @@ def collide_check(item,target):
if distance<=(item.rect.width+each.rect.width)/2:
col_balls.append(each)
return col_balls
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()
#玻璃鼠标的位置
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)#隐藏鼠标
def main():
pygame.init()
#导入素材
pygame.mixer.music.load('bg_music.ogg')
pygame.mixer.music.play()
loser_sound=pygame.mixer.Sound('loser.wav')
hole_sound=pygame.mixer.Sound('hole.wav')
laugh_sound=pygame.mixer.Sound('laugh.wav')
winner_sound=pygame.mixer.Sound('winner.wav')
GAMEOVER=USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER)
ball_image=r'C:\Users\Administrator\Documents\level3-lesson24-diy3\q\gray_ball.png'
bg_image=r'C:\Users\Administrator\Documents\level3-lesson24-diy3\q\background.png'
glass_image=r'C:\Users\Administrator\Documents\level3-lesson24-diy3\glass.png'
mouse_image=r'C:\Users\Administrator\Documents\level3-lesson24-diy3\hand.png'
running=True
bg_size=width,height=1048,681
screen=pygame.display.set_mode(bg_size)
pygame.display.set_caption('Play the ball demo')
background=pygame.image.load(bg_image).convert_alpha()
balls=[]
group=pygame.sprite.Group()
ballnum=5
#小球位置
for i in range(ballnum):
position=randint(0,width-100),randint(0,height-100)
speed=[randint(-10,10),randint(-10,10)]
......@@ -48,27 +76,46 @@ def main():
ball.rect.left,ball.rect.top=randint(0,width-100),randint(0,height-100)
balls.append(ball)
group.add(ball)
glass=Glass(glass_image,mouse_image,bg_size)
clock=pygame.time.Clock()
while running:
for event in pygame.event.get():
if event.type==QUIT:
sys.exit()
elif event.type==GAMEOVER:
loser_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running=False
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.right>glass.glass_rect.right:
glass.mouse_rect.right=glass.glass_rect.right
if glass.mouse_rect.top<glass.glass_rect.top:
glass.mouse_rect.top=glass.glass_rect.top
if glass.mouse_rect.bottom>glass.glass_rect.bottom:
glass.mouse_rect.bottom=glass.glass_rect.bottom
screen.blit(glass.mouse_image,glass.mouse_rect)
for each in balls:
each.move()
screen.blit(each.image,each.rect)
# for i in range(ballnum):
# item=balls.pop(i)
# if collide_check(item,balls):
# item.speed[0]=-item.speed[0]
# item.speed[1]=-item.speed[1]
# balls.insert(i,item)
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
each.speed[0]=-each.speed[0]
each.speed[1]=-each.speed[1]
group.add(each)
for i in range(ballnum):
item=balls.pop(i)
if collide_check(item,balls):
item.speed[0]=-item.speed[0]
item.speed[1]=-item.speed[1]
balls.insert(i,item)
# for each in group:
# group.remove(each)
# if pygame.sprite.spritecollide(each,group,False,pygame.sprite.collide_circle):
# each.speed[0]=-each.speed[0]
# each.speed[1]=-each.speed[1]
# group.add(each)
pygame.display.flip()
clock.tick(30)
main()
File added
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment