简介:此游戏为一个2D小游戏,进入游戏界面后点击“PLAY”开始,然后点击”LEVEL 1“进入准备开始,然后点击空格键开始,正方形格子落下,当落在左右键的时候要按左右键,成功则加分,错过一次“MISSED”数值加1,直到加到4游戏结束,按“R”键重玩,按“Q”键返回。
代码:
import pygame
import sys
import math
from pygame import mixer
import random
import shelve
import time
pygame.init()
screen = pygame.display.set_mode((800, 600)) #creates window screen
background = pygame.image.load('Game Assets\\background.png').convert_alpha() #background
background_level2 = pygame.image.load("Game Assets\\background2.png").convert_alpha()
background_level3 = pygame.image.load("Game Assets\\background3.png").convert_alpha()
main_menu = pygame.image.load("Game Assets\\main_menu.png").convert()
level_select_23locked = pygame.image.load("Game Assets\\level_select_23locked.png").convert()
level_select_3locked = pygame.image.load("Game Assets\\level_select_3locked.png").convert()
level_select_full = pygame.image.load("Game Assets\\level_select.png").convert()
mixer.music.load("Game Assets\\background_music.mp3") #loads background music
mixer.music.play(-1)
pygame.display.set_caption("Galactiblock") #icon and title
icon = pygame.image.load('Game Assets\\icon.png')
pygame.display.set_icon(icon)
class Block: #block class
def __init__(self, Img, ImgX, ImgY):
self.Img = Img
self.ImgX = ImgX
self.ImgY = ImgY
def spawn(self, Img, ImgX, ImgY):
screen.blit(Img, (ImgX, ImgY))
#PRESS SPACE TO BEGIN
start = False
press_start_font = pygame.font.Font('Game Assets\\slkscr.ttf', 42)
def press_to_start():
space_start = press_start_font.render('PRESS SPACE TO BEGIN', True, (255,255,255))
screen.blit(space_start, (130,280))
#UNLOCKED LEVEL CHECK
unlocked = shelve.open('game_data')
#unlocked['level_select'] = 1
#SCORE
score_value = 0
font = pygame.font.Font('Game Assets\\slkscr.ttf', 36)
textX = 10
textY = 10
def show_score(x, y):
score = font.render("Score: " + str(score_value), True, (0, 255, 0))
screen.blit(score, (x, y))
#RECORD
record = shelve.open('game_data')
highX = 10
highY = 100
def personal_record(x, y, level):
high_s = font.render("Record: " + str(record[level]), True, (255, 255, 0))
screen.blit(high_s, (x, y))
#MISSED
missed_value = 0
missedX = 10
missedY = 55
def show_missed(x, y, num):
missed = font.render("Missed: " + str(missed_value) + "/" + str(num), True, (255, 0, 0))
screen.blit(missed, (x, y))
#GAME OVER
game_over_font = pygame.font.Font('Game Assets\\slkscr.ttf', 80)
def game_over():
game_over_text = game_over_font.render('GAME OVER', True, (255,0,0))
screen.blit(game_over_text, (160,250))
reset_text = font.render("Press R to restart", True, (255, 255, 255))
screen.blit(reset_text, (200, 350))
return_text = font.render("Press Q to return", True, (255, 255, 255))
screen.blit(return_text, (205, 400))
#INITIAL SPEED
IntervalA = 2.8
IntervalB = 3.8
#RED BLOCKS
redImg = pygame.image.load("Game Assets\\red_block.png").convert()
redX = 274
redY = random.randint(-1472, -128) #randomizes intial y-position
redChange = random.uniform(IntervalA, IntervalB) #randomizes intial speed
red_block = Block(redImg, redX, redY)
#SEA-FOAM BLOCKS
seafoamImg = pygame.image.load("Game Assets\\seafoam_block.png").convert()
seafoamX = 462
seafoamY = random.randint(-1472, -128) #randomizes intial y-position
seafoamChange = random.uniform(IntervalA, IntervalB) #randomizes initial speed
seafoam_block = Block(seafoamImg, seafoamX, seafoamY)
#JADE BLOCKS
jadeImg = pygame.image.load("Game Assets\\bluegreen_block.png").convert()
jadeX = 402
jadeY = random.randint(-1472, -128) #randomizes initial y-position
jadeChange = random.uniform(IntervalA, IntervalB) #randomizes initial speed
jade_block = Block(jadeImg, jadeX, jadeY)
#VIOLET BLOCKS
violetImg = pygame.image.load("Game Assets\\violet_block.png").convert()
violetX = 530
violetY = random.randint(-1472, -128) #randomizes initial y-position
violetChange = random.uniform(IntervalA, IntervalB) #randomizes initial speed
violet_block = Block(violetImg, violetX, violetY)
#LEFT BUTTON
leftButtonImg = pygame.image.load("Game Assets\\left_button.png").convert_alpha()
leftButtonImgPressed = pygame.image.load("Game Assets\\left_button_pressed.png")
leftB = [leftButtonImg, leftButtonImgPressed]
left_pressed = False
#RIGHT BUTTON
rightButtonImg = pygame.image.load("Game Assets\\right_button.png")
rightButtonImgPressed = pygame.image.load("Game Assets\\right_button_pressed.png")
rightB = [rightButtonImg, rightButtonImgPressed]
right_pressed = False
#UP BUTTON
upButtonImg = pygame.image.load("Game Assets\\up_button.png")
upButtonImgPressed = pygame.image.load("Game Assets\\up_button_pressed.png")
upB = [upButtonImg, upButtonImgPressed]
up_pressed = False
#DOWN BUTTON
downButtonImg = pygame.image.load("Game Assets\\down_button.png")
downButtonImgPressed