Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turtlerace game #94

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
126 changes: 126 additions & 0 deletions freegames/overlap.py
@@ -0,0 +1,126 @@
"""Overlap, classic arcade game.

Exercises

1. Change the shape.
2. Change the color.
3. Change the speed of rectangle.
4. Change the a diminishing amount.
5. Change the the direction of the path(ex.horizontal orientation).
6. Change thr speed of frame.
"""

from turtle import *
from freegames import vector

rectangle = vector(0, 0)
move = vector(0, 3)
overlap = {0: vector(0, 0)}
defualt_size = 270
goal_size = 0


def empty_rectangle(x, y, size):
"""Draw empty rectangle at (x, y) with given width and height."""
up()
goto(x, y)
down()
draw_rectangle(size)


def filled_ractangle(x, y, size):
"""Draw filled rectangle at (x, y) with given width and height."""
up()
goto(x, y)
begin_fill()
draw_rectangle(size)
end_fill()


def draw_rectangle(size):
"""Draw rectangle line with given size."""
for count in range(2):
forward(size)
left(90)
forward(size)
left(90)


def stamp(x, y, text, _color='black', font_size=10):
"""Display `text` at coordinates `x` and `y`."""
goto(x, y)
color(_color)
write(text, font=('Arial', font_size, 'normal'))


def i_size(step):
"""Return the rectangle size of the step"""
return defualt_size - (step) * 30


def end():
"""End the game by stopping moving rectangle"""
move.y = 0
del overlap[len(overlap) - 1]


def draw():
"""Draw rectangle frames and a moving rectangle."""
clear()

if i_size(len(overlap)) == goal_size:
stamp(-200, 100, 'Win!', _color='red', font_size=30)
done()

stamp(-200, -200, 'Score : ' + str(len(overlap) - 1))

size = i_size(len(overlap))

for i in range(0, len(overlap)):
size_i = i_size(i)
empty_rectangle(overlap[i].x - size_i / 2, overlap[i].y - size_i / 2, size_i)

rectangle.move(move)
x = rectangle.x
y = rectangle.y

filled_ractangle(x - (size / 2), y - (size / 2), size)

if y + move.y - size * 0.5 <= -210 or y + move.y + size * 0.5 >= 210:
move.y = -move.y

ontimer(draw, 5)


def tap(x, y):
"""Respond to screen click at a moving rectangle."""
overlap[len(overlap)] = vector(rectangle.x, rectangle.y)

curr_size = i_size(len(overlap) - 1)
prev_size = i_size(len(overlap) - 2)

curr_square = overlap[len(overlap) - 1]
prev_square = overlap[len(overlap) - 2]

if curr_square.x - curr_size / 2 < prev_square.x - prev_size / 2:
end()
done()
elif curr_square.y - curr_size / 2 < prev_square.y - prev_size / 2:
end()
done()
elif curr_square.x + curr_size / 2 > prev_square.x + prev_size / 2:
end()
done()
elif curr_square.y + curr_size / 2 > prev_square.y + prev_size / 2:
end()
done()
elif i_size(len(overlap)) == goal_size:
move.y = 0


setup(width=420, height=420)
hideturtle()
tracer(False)
onscreenclick(tap)
draw()
done()
76 changes: 76 additions & 0 deletions freegames/turtlerace.py
@@ -0,0 +1,76 @@
"""TurtleRace, classic race game.

Exercises

1. Change the speed of obstacle.
2. Change the generation frequency of obstacle.
3. Change the color of obstacle.
3. Upgrade 3-line race to 5-line race.
"""

from freegames import line
from freegames import vector
from turtle import *
from random import choice, random

turtle = vector(-5, -160)
obstacle_speed = -2
ob_timer = 0
obstacle = []


def raceline():
"""Draw two race lines."""
line(-20, 200, -20, -200)
line(10, 200, 10, -200)


def move(change):
"""Move player position by change."""
if -30 <= turtle.x + change <= 20:
turtle.move(vector(change, 0))


def draw():
"""Draw game, move obstacles and player."""
clear()

raceline()

if ob_timer > 40 and random() > 0.8:
obstacle.append(vector(-25 * choice([-1, 0, 1]) - 5, 200))
ob_timer = 0
else:
ob_timer += 1

for i in range(len(obstacle)):
up()
goto(obstacle[i].x, obstacle[i].y)
dot(10, 'red')

for i in range(len(obstacle)):
obstacle[i].move(vector(0, obstacle_speed))

x = turtle.x
y = turtle.y

up()
goto(x, y)
dot(10)

for i in range(len(obstacle)):
if obstacle[i].__eq__(vector(x, y)):
done()

ontimer(draw, 5)


setup(width=100, height=400)
hideturtle()
tracer(False)
listen()
onkey(lambda: move(25), 'd')
onkey(lambda: move(-25), 'a')
draw()

done()