当前位置:   article > 正文

python+gamere实现最强大脑2D版立方骑士

python+gamere实现最强大脑2D版立方骑士

gamere下载

gamere是我自己写的包,下载方法:

pip install gamere==1.3.1 -i https://pypi.org/project/
  • 1

资源下载

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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115

效果

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45144
推荐阅读
相关标签
  

闽ICP备14008679号