1 Star 0 Fork 6

csx123234/Python有趣的代码

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Free Games - Memory 小游戏.py 1.73 KB
一键复制 编辑 原始数据 按行查看 历史
合肥吃货食光 提交于 2019-07-23 02:01 . Free Games 游戏系列
"""Pong, classic arcade game.
Exercises
1. Change the colors.
2. What is the frame rate? Make it faster or slower.
3. Change the speed of the ball.
4. Change the size of the paddles.
5. Change how the ball bounces off walls.
6. How would you add a computer player?
6. Add a second ball.
"""
from random import choice, random
from turtle import *
from freegames import vector
def value():
"Randomly generate value between (-5, -3) or (3, 5)."
return (3 + random() * 2) * choice([1, -1])
ball = vector(0, 0)
aim = vector(value(), value())
state = {1: 0, 2: 0}
def move(player, change):
"Move player position by change."
state[player] += change
def rectangle(x, y, width, height):
"Draw rectangle at (x, y) with given width and height."
up()
goto(x, y)
down()
begin_fill()
for count in range(2):
forward(width)
left(90)
forward(height)
left(90)
end_fill()
def draw():
"Draw game and move pong ball."
clear()
rectangle(-200, state[1], 10, 50)
rectangle(190, state[2], 10, 50)
ball.move(aim)
x = ball.x
y = ball.y
up()
goto(x, y)
dot(10)
update()
if y < -200 or y > 200:
aim.y = -aim.y
if x < -185:
low = state[1]
high = state[1] + 50
if low <= y <= high:
aim.x = -aim.x
else:
return
if x > 185:
low = state[2]
high = state[2] + 50
if low <= y <= high:
aim.x = -aim.x
else:
return
ontimer(draw, 50)
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
listen()
onkey(lambda: move(1, 20), 'w')
onkey(lambda: move(1, -20), 's')
onkey(lambda: move(2, 20), 'i')
onkey(lambda: move(2, -20), 'k')
draw()
done()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/csx123234/fun.git
git@gitee.com:csx123234/fun.git
csx123234
fun
Python有趣的代码
master

搜索帮助