gamere下载
gamere是我自己写的包,下载方法:
pip install gamere==1.3.1 -i https://pypi.org/project/
资源下载
https://pan.baidu.com/s/1Eq81rGgOPRbvQy57uobxDQ
提取码: bija
项目结构
- resources/blue.bmp为障碍
- resources/bold.ttf为游戏字体
- horse-shape-of-game-cipp.py为主程序
代码
"""
平面骑士:按照马走“日”字的规则,将白色格都走一次
"""
import gamere as cpp
import random
from typing import Tuple
flat = lambda L: sum(map(flat, L), []) if isinstance(L, list) else [L]
def judge(start: Tuple[int, int], end: Tuple[int, int]) -> bool:
"""
:type start: Tuple[int, int]
:type end: Tuple[int, int]
:param start: the start position
:param end: the end position
:return: True if it's legal to go else False
"""
startr, startc = start
endr, endc = end
if startr < 0 or endr < 0 or startc < 0 or endc < 0 or \
startr >= gamewidth or endr >= gamewidth or endc >= gamewidth or \
startc >= gamewidth:
return False
return (startr + 1 == endr and startc - 2 == endc) or \
(startr + 2 == endr and startc - 1 == endc) or \
(startr - 1 == endr and startc - 2 == endc) or \
(startr - 2 == endr and startc - 1 == endc) or \
(startr - 2 == endr and startc + 1 == endc) or \
(startr - 1 == endr and startc + 2 == endc) or \
(startr + 1 == endr and startc + 2 == endc) or \
(startr + 2 == endr and startc + 1 == endc)
def handler(event: cpp.event_type):
"""
:type event: cpp.eventtype
:param event: event
:return: None
"""
global position, fatime
if event.type == cpp.MOUSEBUTTONDOWN:
if cpp.Mouse().button(event) == 1:
x, y = cpp.Mouse().pos()
row, column = x // gridwidth, y // gridwidth
if judge(position, (row, column)) and layout.value()[row][column] == white:
if not position == playerp:
lastr, lastc = position
value = layout.value()
value[lastr][lastc] = grey
layout.set(value)
position = (row, column)
value = layout.value()
value[row][column] = green
layout.set(value)
elif event.type == cpp.KEYDOWN:
if event.key == cpp.K_HOME or event.key == cpp.K_F1:
layout.set([i[:] for i in start])
fatime = round(timer.get(), 2)
position = playerp[:]
def update():
"""
:return: None
"""
timertext = cpp.Text('resources/bold.ttf', window, 25, f'Time: {round(timer.get() + fatime, 2)}')
timertext.update()
if white not in flat(layout.value()):
window.spat()
def last():
"""
:return: None
"""
root = cpp.Window(title='挑战成功', size=(300, 300))
text = cpp.Text('resources/bold.ttf', window, 28, f'Win! Time: {round(timer.get() + fatime, 2)}',
color=(255, 255, 255))
root.loop(render=text.update)
gridwidth = 48
gamewidth = 13
blue = cpp.Image('resources/blue.bmp', size=(gridwidth, gridwidth))
grey = cpp.Image('resources/grey.bmp', size=(gridwidth, gridwidth))
player = cpp.Image('resources/player.bmp', size=(gridwidth, gridwidth))
white = cpp.Image('resources/white.bmp', size=(gridwidth, gridwidth))
green = cpp.Image('resources/green.bmp', size=(gridwidth, gridwidth))
difficultly = 2650
board = [[blue] * gamewidth for i in range(gamewidth)]
board[0][-1] = player
timer = cpp.Timer()
fatime = 0
window = cpp.Window(title='平面骑士', size=(gridwidth * gamewidth, gridwidth * gamewidth),
fps=100)
layout = cpp.Layout(board)
position = layout.find(player)
playerp = position[:]
value = layout.value()
assert difficultly != 0
for i in range(difficultly):
row, column = random.randint(0, gamewidth - 1), random.randint(0, gamewidth - 1)
if (row, column) == playerp:
continue
if judge(position, (row, column)) and value[row][column] == blue:
position = (row, column)
value[row][column] = white
else:
position = playerp[:]
start = [i[:] for i in layout.value()]
layout.loop(window, width=gridwidth, height=gridwidth, draw=True,
handler=handler, update=update, last=last)