赞
踩
import pygame
import sys
from pygame.locals import *
pygame.init()
pygame.mixer.init()
pygame.mixer.music.load("bg_music.wav") # 载入音乐
pygame.mixer.music.set_volume(0.2)# 设置音量为 0.2
pygame.mixer.music.play() # 播放音乐
dog_sound = pygame.mixer.Sound("dog.wav")
dog_sound.set_volume(0.2)
bird_sound = pygame.mixer.Sound("bird.wav")
bird_sound.set_volume(0.2)
bg_size = width,height = 300,200
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Music Demo")
pause = False
pause_imgae = pygame.image.load("pause.png").convert_alpha()
unpause_imgae = pygame.image.load("keep.png").convert_alpha()
pause_rect = pause_imgae.get_rect()
pause_rect.center = width // 2,height // 2
clock = pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == QUIT:
sys.exit()
if event.type == MOUSEBUTTONDOWN:
# button(1:左键;2:中间键;3:右键)
if event.button == 1: #表示点击鼠标左键
dog_sound.play()
if event.button == 3: # 表示点击鼠标右键
bird_sound.play()
if event.type == KEYDOWN:
if event.key == K_SPACE:
pause = not pause
screen.fill((255,255,255))
if pause:
screen.blit(pause_imgae,pause_rect)
else:
screen.blit(unpause_imgae,pause_rect)
pygame.display.flip()
clock.tick(30)
import pygame
import sys
from pygame.locals import *
from random import *
# pygame.sprite.Sprite 代表游戏对象的简单基类
class Ball(pygame.sprite.Sprite):
def __init__(self,image,position,speed,bg_size):
# pygame.sprite.Sprite.__init__(self)
super().__init__() # 注意 super 的格式
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]
# 由于采用 collide_circle()方法检测圆的碰撞,所以需要设置 radius 属性
self.radius = self.rect.width / 2
def move(self):
self.rect = self.rect.move(self.speed)
# 这里要的效果是小球整个超出屏幕左侧的应对方案
if self.rect.right < 0:
self.rect.left = self.width
elif self.rect.left > self.width:
self.rect.right = 0
elif self.rect.bottom <0:
self.rect.top = self.height
elif self.rect.top > self.height:
self.rect.bottom = 0
def main():
pygame.init()
ball_image = "gray_ball.png"
bg_image = "background.png"
running = True
# 添加背景音乐
pygame.mixer.music.load("bg_music.wav")
pygame.mixer.music.play()
# 添加音效
win_sound = pygame.mixer.Sound("dog.wav")
loser_sound = pygame.mixer.Sound("loser.wav")
laugh_sound = pygame.mixer.Sound("laugh.wav")
hole_sound = pygame.mixer.Sound("hole.wav")
# 音乐播放完毕,游戏结束
Gameover = USEREVENT # 自定义一个用户事件
pygame.mixer.music.set_endevent(Gameover)
#根据背景图片设置游戏界面的尺寸
bg_size = width,height = 1024,681
screen = pygame.display.set_mode(bg_size)
pygame.display.set_caption("Play the ball")
background = pygame.image.load(bg_image).convert_alpha()
# 用来存放小球对象的列表
balls = []
group = pygame.sprite.Group()
# 创建五个小球,位置随机,速度随机
for i in range(5):
# 球的大小是 100 x 100 防止越出边界,所以要宽和高减 100 的范围以内
position = randint(0,width-100),randint(0,height-100)
speed = [randint(-10,10),randint(-10,10)]
ball = Ball(ball_image,position,speed,bg_size)
# 创建小球时需要检测,防止球与球之间发生重叠
# while pygame.sprite.spritecollide(ball,group,False):
while pygame.sprite.spritecollide(ball,group,False,pygame.sprite.collide_circle):
# 若发生碰撞即有重叠,就重新分配位置
ball.rect.left,ball.rect.top = randint(0,width-100),randint(0,height-100)
balls.append(ball)
# 新建一个球后,将其加入检测组中
group.add(ball)
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))
for each in balls:
each.move()
screen.blit(each.image,each.rect)
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) #最高每秒不超过 30 次
if __name__ == '__main__':
main()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。