Python之freegames 零代码的22个小游戏集合是一个Python项目,包含22个小游戏,每个游戏都可以零代码运行。本文将详细讲解如何下载、安装和运行这个项目,并以两个游戏为例进行说明。
下载与安装
- 下载项目
在GitHub上下载该项目的压缩文件,或使用git clone命令将项目克隆到本地:
git clone https://github.com/grantjenks/free-python-games
- 安装依赖
进入项目目录后,使用以下命令安装依赖:
pip install -r requirements.txt
运行游戏
在项目目录下,找到想要运行的游戏的.py文件,使用Python命令即可运行,例如:
python3 sudoku/sudoku.py
游戏示例
小蛇游戏
小蛇游戏的.py文件位于snake/目录下,文件名为snake.py。运行该文件即可启动小蛇游戏。
游戏规则:
- 使用键盘上的方向键控制蛇的移动方向;
- 吃到食物后蛇的长度增加,分数增加;
- 如果蛇碰到边界或者自己的身体,则游戏结束。
示例代码:
from random import randrange
from turtle import *
from freegames import square, vector
food = vector(0, 0)
snake = [vector(10, 0)]
aim = vector(0, -10)
def change(x, y):
"Change snake direction."
aim.x = x
aim.y = y
def inside(head):
"Return True if head inside boundaries."
return -200 < head.x < 190 and -200 < head.y < 190
def move():
"Move snake forward one segment."
head = snake[-1].copy()
head.move(aim)
if not inside(head) or head in snake:
square(head.x, head.y, 9, 'red')
update()
return
snake.append(head)
if head == food:
print('Snake:', len(snake))
food.x = randrange(-15, 15) * 10
food.y = randrange(-15, 15) * 10
else:
snake.pop(0)
clear()
for body in snake:
square(body.x, body.y, 9, 'black')
square(food.x, food.y, 9, 'green')
update()
ontimer(move, 100)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: change(10, 0), 'Right')
onkey(lambda: change(-10, 0), 'Left')
onkey(lambda: change(0, 10), 'Up')
onkey(lambda: change(0, -10), 'Down')
move()
done()
Ping-pong游戏
Ping-pong游戏的.py文件位于pong/目录下,文件名为pong.py。运行该文件即可启动Ping-pong游戏。
游戏规则:
- 使用键盘上的
w
和s
键控制左侧板子的上下移动,使用i
和k
键控制右侧板子的上下移动; - 通过控制板子使得球不超出边界和被挡住;
- 根据打球方向,球会改变方向。
示例代码:
from random import choice
from turtle import *
from freegames import vector
def value():
"Randomly generate value between (-5, -3) or (3, 5)."
return (3 + choice(range(5))) * [-1, 1][choice([0, 1])]
ball = vector(0, 0)
aim = vector(value(), value())
state = {1: 0, 2: 0}
def move(player):
"Move ball and check for collisions."
ball.move(aim)
if ball.x < -200 or ball.x > 200:
aim.x = -aim.x
state[player] += 1
if ball.y < -200 or ball.y > 200:
aim.y = -aim.y
goto(ball.x, ball.y)
dot(10)
return state[player]
def power(x, y, w, h):
"Create and return a turtle with customized attributes."
t = Turtle(visible=False)
t.speed(0)
t.penup()
t.goto(x, y)
t.shape('square')
t.shapesize(w, h)
t.fillcolor('white')
t.showturtle()
return t
def final_scores():
"Display the final scores."
clear()
goto(-100, 0)
write(f'Player 1: {state[1]} Player 2: {state[2]}', font=('Arial', 30, 'normal'), align='center')
def winner():
"Display winner."
clear()
winner = 1 if state[1] > state[2] else 2
goto(-100, 0)
write(f'Player {winner} Wins!', font=('Arial', 30, 'normal'), align='center')
# Set up game board
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
window = Turtle(visible=False)
window.penup()
window.goto(0, 200)
window.write("Python Pong", align="center", font=("Courier", 24, "normal"))
window.goto(170, -195)
window.write("Player 1", align="center", font=("Courier", 14, "normal"))
window.goto(-150, -195)
window.write("Player 2", align="center", font=("Courier", 14, "normal"))
window.goto(-145, 185)
window.write("Python Pong", align="center", font=("Courier", 14, "normal"))
# Draw game board
l1 = power(-220, 20, 1, 10)
l2 = power(-220, -20, 1, 10)
r1 = power(220, 20, 1, 10)
r2 = power(220, -20, 1, 10)
# Game loop
while abs(ball.x) < 200 and abs(ball.y) < 200:
if l1.distance(ball) < 15:
aim.x = -aim.x
state[1] += 1
if l2.distance(ball) < 15:
aim.x = -aim.x
state[1] += 1
if r1.distance(ball) < 15:
aim.x = -aim.x
state[2] += 1
if r2.distance(ball) < 15:
aim.x = -aim.x
state[2] += 1
delay = abs(int(25 - (move(1) + move(2))/2))
ontimer(1, lambda:None)
if state[1] == 10 or state[2] == 10:
winner()
else:
final_scores()
done()
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python之freegames 零代码的22个小游戏集合 - Python技术站