代码结构
app.png是游戏运行主界面的图片(可以加载自己喜欢的主界面图片)
Assets文件夹里面装的是一些需要用到的游戏图片
全部都可以替换为自己喜欢的图片
Fonts里面装的是
Sounds文件夹里面装的是
一 . 主程序代码
1.运行这个代码使得游戏开始
2.主界面的图片可以自己下载一些喜欢的图片 修改路径即可
3.报错就是缺少必要文件 有需要的可以联系我 (我把整个代码给你)
# 导入必要的包
import json
import random
import pygame
from threading import Thread
from objects import Tile, Square, Text, Button, Counter
pygame.init()
SCREEN = WIDTH, HEIGHT = 288, 512
TILE_WIDTH = WIDTH // 4
TILE_HEIGHT = 130
info = pygame.display.Info()
width = info.current_w
height = info.current_h
if width >= height:
win = pygame.display.set_mode(SCREEN, pygame.NOFRAME)
else:
win = pygame.display.set_mode(SCREEN, pygame.NOFRAME | pygame.SCALED | pygame.FULLSCREEN)
clock = pygame.time.Clock()
FPS = 30
# COLORS *********************************************************************
WHITE = (255, 255, 255)
GRAY = (75, 75, 75)
BLUE = (30, 144, 255)
# IMAGES *********************************************************************
bg_img = pygame.image.load('Assets/bg.png')
bg_img = pygame.transform.scale(bg_img, (WIDTH, HEIGHT))
piano_img = pygame.image.load('Assets/piano.png')
piano_img = pygame.transform.scale(piano_img, (212, 212))
title_img = pygame.image.load('Assets/title.png')
title_img = pygame.transform.scale(title_img, (200, 50))
start_img = pygame.image.load('Assets/start.png')
start_img = pygame.transform.scale(start_img, (120, 40))
start_rect = start_img.get_rect(center=(WIDTH//2, HEIGHT-80))
overlay = pygame.image.load('Assets/red overlay.png')
overlay = pygame.transform.scale(overlay, (WIDTH, HEIGHT))
# MUSIC **********************************************************************
buzzer_fx = pygame.mixer.Sound('Sounds/piano-buzzer.mp3')
pygame.mixer.music.load('Sounds/piano-bgmusic.mp3')
pygame.mixer.music.set_volume(0.8)
pygame.mixer.music.play(loops=-1)
# FONTS **********************************************************************
score_font = pygame.font.Font('Fonts/Futura condensed.ttf', 32)
title_font = pygame.font.Font('Fonts/Alternity-8w7J.ttf', 30)
gameover_font = pygame.font.Font('Fonts/Alternity-8w7J.ttf', 40)
title_img = title_font.render('Piano Tiles', True, WHITE)
# BUTTONS ********************************************************************
close_img = pygame.image.load('Assets/closeBtn.png')
replay_img = pygame.image.load('Assets/replay.png')
sound_off_img = pygame.image.load("Assets/soundOffBtn.png")
sound_on_img = pygame.image.load("Assets/soundOnBtn.png")
close_btn = Button(close_img, (24, 24), WIDTH // 4 - 18, HEIGHT//2 + 120)
replay_btn = Button(replay_img, (36,36), WIDTH // 2 - 18, HEIGHT//2 + 115)
sound_btn = Button(sound_on_img, (24, 24), WIDTH - WIDTH // 4 - 18, HEIGHT//2 + 120)
# GROUPS & OBJECTS ***********************************************************
tile_group = pygame.sprite.Group()
square_group = pygame.sprite.Group()
text_group = pygame.sprite.Group()
time_counter = Counter(win, gameover_font)
# FUNCTIONS ******************************************************************
def get_speed(score):
return 200 + 5 * score
def play_notes(notePath):
pygame.mixer.Sound(notePath).play()
# NOTES **********************************************************************
with open('notes.json') as file:
notes_dict = json.load(file)
# VARIABLES ******************************************************************
score = 0
high_score = 0
speed = 0
clicked = False
pos = None
home_page = True
game_page = False
game_over = False
sound_on = True
count = 0
overlay_index = 0
running = True
while running:
pos = None
count += 1
if count % 100 == 0:
square = Square(win)
square_group.add(square)
counter = 0
win.blit(bg_img, (0,0))
square_group.update()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE or \
event.key == pygame.K_q:
running = False
if event.type == pygame.MOUSEBUTTONDOWN and not game_over:
pos