遊戲截圖及玩法
在下方緩慢拖動鼠標,球變灰色之後用鍵盤WASD控制上下左右.將灰球移動到灰洞上按下空格鎖定.全部5個都進洞遊戲勝利.
代碼及圖片音頻資源
背景圖
其他圖
音頻不能導入,留言郵箱我會私發
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 5 15:36:12 2020
@author: WSX
"""
import pygame
from pygame.locals import *
import sys
from random import *
#球類
class Ball(pygame.sprite.Sprite):
def __init__(self, green_ball_image, gray_ball_image, position, speed, bg_size, target):
pygame.sprite.Sprite.__init__(self)
self.green_ball_image = pygame.image.load(green_ball_image).convert_alpha()
self.gray_ball_image = pygame.image.load(gray_ball_image).convert_alpha()
self.rect = self.green_ball_image.get_rect()
self.rect.left, self.rect.top = position
self.speed = speed
self.side = [1 if self.speed[0] >= 0 else -1, 1 if self.speed[1] >= 0 else -1]
self.width, self.height = bg_size[0], bg_size[1]
self.radius = self.rect.width / 2 + 5;
self.target = target
self.control = False
def move(self):
self.rect = self.rect.move(self.speed)
if self.rect.left >= self.width:
self.rect.right = 0
elif self.rect.right <= 0:
self.rect.left = self.width
if self.rect.top >= self.height:
self.rect.bottom = 0
elif self.rect.bottom <= 0:
self.rect.top = self.height
def check(self, motion):
if self.target - 5 < 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()
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()
#添加圖片
green_ball_image = 'green_ball.png'
gray_ball_image = 'gray_ball.png'
bg_image = 'bg.png'
glass_image = 'glass.png'
mouse_image = 'hand.png'
useless_image = 'useless.png'
#添加音樂
pygame.mixer.music.load('gaming2.ogg')
pygame.mixer.music.set_volume(0.2)
pygame.mixer.music.play()
#添加音效
hole_sound = pygame.mixer.Sound('hole.ogg')
hole_sound.set_volume(0.1)
win_sound = pygame.mixer.Sound('win.ogg')
win_sound.set_volume(0.1)
fail_sound = pygame.mixer.Sound('fail.ogg')
fail_sound.set_volume(0.1)
laugh_sound = pygame.mixer.Sound('laugh.ogg')
laugh_sound.set_volume(0.1)
#音樂播放完,遊戲結束
GAMEOVER = USEREVENT
pygame.mixer.music.set_endevent(GAMEOVER)#發送到消息隊列中
#指定窗口標題,背景,寬高,鍵盤重複響應速度
bg_size = width, height = 1024, 511
screen = pygame.display.set_mode(bg_size)
background = pygame.image.load(bg_image).convert_alpha()
pygame.display.set_caption('PLAY THE BALL!')
pygame.key.set_repeat(100, 100)#第一個參數代表第一次發送事件的延遲,第二個參數指定重複發送事件的時間間隔,無參數表示取消重複發送
#存放小球的列表
balls = []
group =pygame.sprite.Group()
#使用spritecollide方法,需要使用sprite.Group()
#spritecollide(sprite, group, dokill, collide = None),第三個參數是是否從組中刪除,第四個是碰撞檢測方法
#洞
holes = [(154,158,221,225),(367,371,92,96),(467,471,334,338),\
(770,774,343,347),(831,835,143,147)]
#消息隊列
msgs = []
#實例化小球,並加入碰撞檢測
for i in range(5):
position = randint(0, width-50), randint(0, height-50)
speed = [randint(-5, 5), randint(-5, 5)]
ball = Ball(green_ball_image, gray_ball_image, position, speed, bg_size, 6 * (i+1))
while pygame.sprite.spritecollide(ball, group, False, pygame.sprite.collide_circle):
ball.rect.left, ball.rect.top = randint(0, width-50), randint(0, height-50)
balls.append(ball)
group.add(ball)
#實例化玻璃
glass = Glass(glass_image, mouse_image, bg_size)
motion = 0
MYTIMER = USEREVENT + 1
pygame.time.set_timer(MYTIMER, 1 * 1000)
#運行起來
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == GAMEOVER:
fail_sound.play()
pygame.time.delay(2000)
laugh_sound.play()
running = False
elif event.type == MYTIMER:
if motion:
print(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 group:
if each.control:
for i in holes:
if i[0] <= each.rect.centerx <= i[1] and i[2] <= each.rect.centery <= i[3]:
hole_sound.play()
each.speed = [0, 0]
group.remove(each)
temp = balls.pop(balls.index(each))
balls.insert(0, temp)
holes.remove(i)
if not holes:
pygame.mixer.music.stop()
win_sound.play()
msg = pygame.image.load(useless_image).convert_alpha()
msg_pos = (width - msg.get_width()) // 2, (height - msg.get_height()) // 2
msgs.append((msg, msg_pos))
laugh_sound.play()
#繪製背景
screen.blit(background, (0, 0))
#繪製玻璃,指針
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.glass_image, glass.glass_rect)
screen.blit(glass.mouse_image, glass.mouse_rect)
#繪製小球
for each in balls:
each.move()
if each.control == True:
screen.blit(each.gray_ball_image, each.rect)
else:
screen.blit(each.green_ball_image, each.rect)
#碰撞檢測
for each in group:
group.remove(each)
if pygame.sprite.spritecollide(each, group, False, pygame.sprite.collide_circle):
each.side = [1 if each.speed[0] >= 0 else -1, 1 if each.speed[1] >= 0 else -1]
each.speed = [randint(1, 5)*(-each.side[0]), randint(1, 5)*(-each.side[1])]
each.control = False
group.add(each)
#打印消息
for msg in msgs:
screen.blit(msg[0], msg[1])
#圖像顯示到屏幕上
pygame.display.flip()
clock.tick(30)
pygame.quit()
sys.exit()
if __name__ == '__main__':
main()