1 Star 2 Fork 1

xi2p/eatNote

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
gamebase.py 3.54 KB
一键复制 编辑 原始数据 按行查看 历史
xi2p 提交于 2022-02-01 21:28 +08:00 . commit
import pygame
import core
import time
# pygame init
pygame.init()
# load local files
targetImage = pygame.image.load("target.png")
errImage = pygame.image.load("err.png")
hitImage = pygame.image.load("hit.png")
hitSound = pygame.mixer.Sound("hit.wav")
errSound = pygame.mixer.Sound("err.wav")
my_font = pygame.font.SysFont("arial", 16)
my_font_2 = pygame.font.SysFont("arial", 32)
my_font_3 = pygame.font.SysFont("arial", 48)
# define class
class CPSCounter:
def __init__(self):
self.startTime = time.time()
self.clickNum = 0
def getCPS(self):
nowTime = time.time()
CPS = self.clickNum / (nowTime - self.startTime + 0.001)
if nowTime - self.startTime > 1:
self.startTime = nowTime
self.clickNum = 0
return CPS
def click(self):
self.clickNum += 1
class FPSCounter:
def __init__(self):
self.startTime = time.time()
self.frameNum = 0
def getFPS(self):
nowTime = time.time()
FPS = self.frameNum / (nowTime - self.startTime + 0.001)
if nowTime - self.startTime > 1:
self.startTime = nowTime
self.frameNum = 0
if FPS == 0:
FPS = 60
return FPS
def update(self):
self.frameNum += 1
class Slide:
# 当图标被点击后,使画面平滑下降
def __init__(self):
self.lastPx = 100 # 还要下降多少
def update(self):
self.lastPx -= 100 / FPSCounter.getFPS() * 5
if self.lastPx <= 0:
self.lastPx = 0
return 0
return self.lastPx
class GameState:
def __init__(self):
self.state = PREPARE
def set(self, state):
self.state = state
def __eq__(self, other):
# other 只能是常量
# print("compared to", other)
return True if self.state == other else False
class TimeJudge:
"""
负责计时,同时需要完成对于游戏结束等提示语的书写
"""
def __init__(self, gameState: GameState, _screen, valid):
self._gameState = gameState
self.startTime = 0
self.screen = _screen
self.valid = valid
def update(self):
if not self.valid:
return 0
if time.time() - self.startTime >= 20 and self._gameState == PLAYING:
self._gameState.set(state=END)
if self._gameState == END:
Text = my_font_2.render("Press <Space> to restart", True, (0, 0, 0))
self.screen.blit(Text, (30, 300))
Text = my_font_2.render("last score: %d" % core.score, True, (0, 0, 0))
self.screen.blit(Text, (30, 350))
elif self._gameState == PREPARE:
Text = my_font_2.render(" Press to start ", True, (0, 0, 0))
self.screen.blit(Text, (30, 300))
Text = my_font_2.render("last score: %d" % core.score, True, (0, 0, 0))
self.screen.blit(Text, (30, 350))
self.startTime = 0
elif self._gameState == PLAYING:
Text = my_font_3.render("TIME: %d" % (20 - (time.time() - self.startTime) + 1), True, (0, 0, 0))
self.screen.blit(Text, (100, 100))
@property
def gameState(self):
if self.valid:
return self._gameState
else:
_gameState = GameState()
_gameState.set(PLAYING)
return _gameState
# 全局变量 / 常量
PREPARE = 1
PLAYING = 2
END = 3
# 实体化类
CPSCounter = CPSCounter()
FPSCounter = FPSCounter()
GAMESTATE = GameState()
# 准备启动
clock = pygame.time.Clock() # 设置时钟
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xi2p/eat-note.git
git@gitee.com:xi2p/eat-note.git
xi2p
eat-note
eatNote
master

搜索帮助