idk
This commit is contained in:
1 parent
069c04cd71
commit
dd64defcda
1 file changed
+86
-89
@@ -82,95 +82,92 @@
|
||||
|
||||
|
||||
<script type="text/python">
|
||||
|
||||
from browser import document, html, window
|
||||
import random
|
||||
|
||||
score = 0
|
||||
high_score = 0
|
||||
|
||||
px = py = 10
|
||||
gs = tc = 20
|
||||
ax = ay = 15
|
||||
xv = yv = 0
|
||||
trail = []
|
||||
tail = 5
|
||||
|
||||
pre_pause = [0,0]
|
||||
paused = False
|
||||
|
||||
def game():
|
||||
global px, py, tc, gs, ax, ay, trail, tail, score
|
||||
px += xv
|
||||
py += yv
|
||||
if px < 0:
|
||||
px = tc-1
|
||||
if px > tc-1:
|
||||
px = 0
|
||||
if py < 0:
|
||||
py = tc-1
|
||||
if py > tc-1:
|
||||
py = 0
|
||||
ctx.fillStyle = "black"
|
||||
ctx.fillRect(0, 0, canvas.width, canvas.height)
|
||||
ctx.fillStyle = "lime"
|
||||
for i in range(len(trail)):
|
||||
ctx.fillRect(trail[i][0]*gs, trail[i][1]*gs, gs-2, gs-2)
|
||||
if trail[i][0] == px and trail[i][1] == py:
|
||||
score = score if paused else 0
|
||||
tail = tail if paused else 5
|
||||
trail.insert(0, [px, py])
|
||||
while len(trail) > tail:
|
||||
trail.pop()
|
||||
|
||||
if ax == px and ay == py:
|
||||
tail += 1
|
||||
ax = int(random.random()*tc)
|
||||
ay = int(random.random()*tc)
|
||||
score += 1
|
||||
update_score(score)
|
||||
ctx.fillStyle = "red"
|
||||
ctx.fillRect(ax*gs, ay*gs, gs-2, gs-2)
|
||||
|
||||
def update_score(new_score):
|
||||
global high_score
|
||||
document["score"].innerHTML = "Score: " + str(new_score)
|
||||
if new_score > high_score:
|
||||
document["high-score"].innerHTML = "High Score: " + str(new_score)
|
||||
high_score = new_score
|
||||
|
||||
def key_push(evt):
|
||||
global xv, yv, pre_pause, paused
|
||||
key = evt.keyCode
|
||||
if key == 37 and not paused:
|
||||
xv = -1
|
||||
yv = 0
|
||||
elif key == 38 and not paused:
|
||||
xv = 0
|
||||
yv = -1
|
||||
elif key == 39 and not paused:
|
||||
xv = 1
|
||||
yv = 0
|
||||
elif key == 40 and not paused:
|
||||
xv = 0
|
||||
yv = 1
|
||||
elif key == 32:
|
||||
temp = [xv, yv]
|
||||
xv = pre_pause[0]
|
||||
yv = pre_pause[1]
|
||||
pre_pause = [*temp]
|
||||
paused = not paused
|
||||
|
||||
def show_instructions(evt):
|
||||
window.alert("Use the arrow keys to move and press spacebar to pause the game.")
|
||||
|
||||
canvas = document["game-board"]
|
||||
ctx = canvas.getContext("2d")
|
||||
document.addEventListener("keydown", key_push)
|
||||
game_loop = window.setInterval(game, 1000/10)
|
||||
instructions_btn = document["instructions-btn"]
|
||||
instructions_btn.addEventListener("click", show_instructions)
|
||||
|
||||
'''
|
||||
Function:
|
||||
Python小游戏合集
|
||||
Author:
|
||||
Charles
|
||||
微信公众号:
|
||||
Charles的皮卡丘
|
||||
'''
|
||||
import sys
|
||||
import warnings
|
||||
from PyQt5.QtWidgets import QApplication
|
||||
if __name__ == '__main__':
|
||||
from core import *
|
||||
else:
|
||||
from .core import *
|
||||
warnings.filterwarnings('ignore')
|
||||
|
||||
|
||||
'''Python实用工具集'''
|
||||
class CPGames():
|
||||
def __init__(self, **kwargs):
|
||||
for key, value in kwargs.items(): setattr(self, key, value)
|
||||
self.supported_games = self.initialize()
|
||||
'''执行对应的小程序'''
|
||||
def execute(self, game_type=None, config={}):
|
||||
assert game_type in self.supported_games, 'unsupport game_type %s...' % game_type
|
||||
qt_games = ['tetris', 'gobang']
|
||||
if game_type in qt_games:
|
||||
app = QApplication(sys.argv)
|
||||
client = self.supported_games[game_type](**config)
|
||||
client.show()
|
||||
sys.exit(app.exec_())
|
||||
else:
|
||||
client = self.supported_games[game_type](**config)
|
||||
client.run()
|
||||
'''初始化'''
|
||||
def initialize(self):
|
||||
supported_games = {
|
||||
'ski': SkiGame,
|
||||
'maze': MazeGame,
|
||||
'gobang': GobangGame,
|
||||
'tetris': TetrisGame,
|
||||
'pacman': PacmanGame,
|
||||
'gemgem': GemGemGame,
|
||||
'tankwar': TankWarGame,
|
||||
'sokoban': SokobanGame,
|
||||
'pingpong': PingpongGame,
|
||||
'trexrush': TRexRushGame,
|
||||
'bomberman': BomberManGame,
|
||||
'whacamole': WhacAMoleGame,
|
||||
'catchcoins': CatchCoinsGame,
|
||||
'flappybird': FlappyBirdGame,
|
||||
'angrybirds': AngryBirdsGame,
|
||||
'magictower': MagicTowerGame,
|
||||
'aircraftwar': AircraftWarGame,
|
||||
'bunnybadger': BunnyBadgerGame,
|
||||
'minesweeper': MineSweeperGame,
|
||||
'greedysnake': GreedySnakeGame,
|
||||
'puzzlepieces': PuzzlePiecesGame,
|
||||
'towerdefense': TowerDefenseGame,
|
||||
'bloodfootball': BloodFootballGame,
|
||||
'alieninvasion': AlienInvasionGame,
|
||||
'breakoutclone': BreakoutcloneGame,
|
||||
'twentyfourpoint': TwentyfourPointGame,
|
||||
'flipcardbymemory': FlipCardByMemoryGame,
|
||||
'twozerofoureight': TwoZeroFourEightGame,
|
||||
'voicecontrolpikachu': VoiceControlPikachuGame,
|
||||
}
|
||||
return supported_games
|
||||
'''获得所有支持的游戏信息'''
|
||||
def getallsupported(self):
|
||||
all_supports = {}
|
||||
for key, value in self.supported_games.items():
|
||||
all_supports[value.game_type] = key
|
||||
return all_supports
|
||||
'''str'''
|
||||
def __str__(self):
|
||||
return 'Welcome to use CPGames!\nYou can visit https://github.com/CharlesPikachu/Games for more details.'
|
||||
|
||||
|
||||
'''run'''
|
||||
if __name__ == '__main__':
|
||||
import random
|
||||
games_client = CPGames()
|
||||
all_supports = games_client.getallsupported()
|
||||
games_client.execute(random.choice(list(all_supports.values())))
|
||||
|
||||
</script>
|
||||
|
||||
|
||||
Reference in new issue
Block a user