Python之freegames 零代码的22个小游戏集合攻略
1. 介绍
Python之freegames是一个由Python语言实现的,由22个小游戏组成的集合。这些游戏非常容易上手,因为它们都是使用Python标准库和freegames模块编写的。更重要的是,它们没有任何代码,因此无需担心程序语法错误或逻辑错误。
这些游戏的难度各不相同,既有简单的,也有复杂的。这些游戏包括连连看、2048、弹球、跳跃方块等。
2. 安装
安装Python,访问 https://www.python.org/downloads/ 查找相应版本。
在终端中输入以下命令来安装 freegames
pip install freegames
3. 游戏示例
- 连连看
from random import randrange
from turtle import *
from freegames import line
def grid():
"Draw tic-tac-toe grid."
line(-67, 200, -67, -200)
line(67, 200, 67, -200)
line(-200, -67, 200, -67)
line(-200, 67, 200, 67)
def drawx(x, y):
"Draw X player."
line(x, y, x + 133, y + 133)
line(x, y + 133, x + 133, y)
def drawo(x, y):
"Draw O player."
up()
goto(x + 67, y + 5)
down()
circle(62)
def floor(value):
"Round value down to grid with square size."
return ((value + 200) // 133) * 133 - 200
state = {'player': 0}
players = [drawx, drawo]
def tap(x, y):
"Draw X or O in tapped square."
x = floor(x)
y = floor(y)
player = state['player']
draw = players[player]
draw(x, y)
update()
state['player'] = not player
def draw():
"Draw slightly thick tic-tac-toe grid."
pensize(5)
grid()
pensize(1)
hideturtle()
tracer(False)
update()
onscreenclick(tap)
done()
draw()
在命令行中输入该代码即可开始游戏。
- 2048
"""
2048 5x5 grid version by Filipe Kiss <filipe.kiss@gmail.com>
"""
from itertools import *
from random import *
import os
import sys
import tty
import termios
import atexit
def display(game):
size = len(game)
for i in range(size):
for j in range(size):
v = game[i][j]
print("+------", end="")
print("+")
for j in range(size):
v = game[i][j]
print("|{0: ^6}".format(v), end="")
print("|")
for j in range(size):
print("+------", end="")
print("+")
def axisRotation(game):
lines = []
size = len(game)
for i in range(size):
line = []
for j in range(size):
line.append(game[size-j-1][i])
lines.append(line)
return lines
def collapse(line):
def collapsePairs(line):
i = 1
while i < len(line):
if line[i] == line[i-1]:
line[i-1] += 1
line[i] = 0
i += 1
return line
res = []
for i in range(len(line)):
if line[i] > 0:
res.append(line[i])
res = collapsePairs(res)
while len(res) < len(line):
res.append(0)
return res
def slide(game, direction):
axis = game
if direction == 'w':
axis = axisRotation(game)
axis = list(transpose(collapse(transpose(axis))))
axis = axisRotation(axisRotation(axisRotation(axis))))
game = axis
elif direction == 's':
axis = axisRotation(axisRotation(axisRotation(game)))
axis = list(transpose(collapse(transpose(axis))))
axis = axisRotation(game)
game = axis
elif direction == 'd':
game = [collapse(line) for line in game]
elif direction == 'a':
game = [collapse(line[::-1])[::-1] for line in game]
return game
def transpose(matrix):
"""
Transpose matrix nxn.
"""
size = len(game)
return [[matrix[j][i] for j in range(size)] for i in range(size)]
def update_board(game):
game_len = len(game[0])*len(game)
if not any(x == 0 for row in game for x in row):
return False, "Game Over"
if any(x == 2048 for row in game for x in row):
return False, "You Won"
if game != slide(slide(game, "w"), "s"):
return True, None
if game != slide(slide(game, "d"), "a"):
return True, None
return False, None
def get_input():
x = getch()
if ord(x) == 27:
x2 = getch()
x3 = getch()
return x+x2+x3
return x
def getch():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
return sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def init_game():
"""Sets up the game"""
os.system('clear')
game = []
size = 4
for i in range(size):
r = []
for j in range(size):
r.append(0)
game.append(r)
game[randint(0, size-1)][randint(0, size-1)] = 2
return game
def game_loop():
"""Constantly receives input from user"""
game = init_game()
os.system('clear')
display(game)
while True:
x = get_input()
if x == "q":
break
elif x == "A":
game = slide(game, 'w')
elif x == "B":
game = slide(game, 's')
elif x == "D":
game = slide(game, 'a')
elif x == "C":
game = slide(game, 'd')
updated, info = update_board(game)
os.system('clear')
if info:
print(info)
break
display(game)
if updated:
game[randint(0, len(game)-1)][randint(0, len(game[0])-1)] = 2
updated, info = update_board(game)
if info:
print(info)
break
def reset_terminal():
termios.tcsetattr(IPT, termios.TCSANOW, old_settings)
if __name__ == '__main__':
IPT = sys.stdin.fileno()
old_settings = termios.tcgetattr(IPT)
atexit.register(reset_terminal)
game_loop()
在命令行中输入该代码即可开始游戏。
4. 总结
通过Python之freegames,我们可以很容易地制作出不同类型的小游戏,而且不需要编写任何代码。不仅适合初学者,还对有经验的Python用户来说也是一个有趣的挑战。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python之freegames 零代码的22个小游戏集合 - Python技术站