From 60bcf01ed0e54377cec4b69b7bc9e75ac4c6b1f5 Mon Sep 17 00:00:00 2001 From: Yijungu Date: Fri, 4 Nov 2022 19:22:24 +0900 Subject: [PATCH 1/6] overlap game --- freegames/overlap.py | 98 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 freegames/overlap.py diff --git a/freegames/overlap.py b/freegames/overlap.py new file mode 100644 index 00000000..59482495 --- /dev/null +++ b/freegames/overlap.py @@ -0,0 +1,98 @@ +"""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, 5) +top = {0: vector(0, 0)} +top_size = {0: 300, 1: 200} + + +def rectangle_top(x, y, size): + """Draw empty rectangle at (x, y) with given width and height.""" + up() + goto(x, y) + down() + for count in range(2): + forward(size) + left(90) + forward(size) + left(90) + + +def rectangle_move(x, y, size): + """Draw filled rectangle at (x, y) with given width and height.""" + up() + goto(x, y) + begin_fill() + for count in range(2): + forward(size) + left(90) + forward(size) + left(90) + end_fill() + update() + + +def score(x, y, text): + """Display `text` at coordinates `x` and `y`.""" + goto(x, y) + color('black') + write(text, font=('Arial', 10, 'normal')) + + +def draw(): + """Draw rectangles of top and a moving rectangle.""" + clear() + + score(-200, -200, 'Score : ' + str(len(top) - 1)) + + size = top_size[len(top_size) - 1] + + for i in range(0, len(top)): + size_i = top_size[i] + rectangle_top(top[i].x - size_i / 2, top[i].y - size_i / 2, size_i) + + rectangle.move(move) + x = rectangle.x + y = rectangle.y + + rectangle_move(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.""" + top[len(top)] = vector(rectangle.x, rectangle.y) + top_size[len(top)] = top_size[len(top) - 1] - 20 + if top[len(top) - 1].x - top_size[len(top) - 1] / 2 < top[len(top) - 2].x - top_size[len(top) - 2] / 2 or \ + top[len(top) - 1].y - top_size[len(top) - 1] / 2 < top[len(top) - 2].y - top_size[len(top) - 2] / 2 or \ + top[len(top) - 1].x + top_size[len(top) - 1] / 2 > top[len(top) - 2].x + top_size[len(top) - 2] / 2 or \ + top[len(top) - 1].y + top_size[len(top) - 1] / 2 > top[len(top) - 2].y + top_size[len(top) - 2] / 2: + move.y = 0 + del top[len(top) - 1] + del top_size[len(top_size) - 1] + + +setup(width=420, height=420) +hideturtle() +tracer(False) +onscreenclick(tap) +draw() + +done() From fcc032eb0cee4ea2af0d4ab25af40331fc8a4b9e Mon Sep 17 00:00:00 2001 From: Yijungu Date: Tue, 8 Nov 2022 15:07:33 +0900 Subject: [PATCH 2/6] primary feedback --- freegames/overlap.py | 86 ++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 31 deletions(-) diff --git a/freegames/overlap.py b/freegames/overlap.py index 59482495..1263b347 100644 --- a/freegames/overlap.py +++ b/freegames/overlap.py @@ -14,61 +14,77 @@ from freegames import vector rectangle = vector(0, 0) -move = vector(0, 5) -top = {0: vector(0, 0)} -top_size = {0: 300, 1: 200} +move = vector(0, 3) +overlap = {0: vector(0, 0)} +defualt_size = 270 +goal_size = 0 -def rectangle_top(x, y, size): +def empty_rectangle(x, y, size): """Draw empty rectangle at (x, y) with given width and height.""" up() goto(x, y) down() - for count in range(2): - forward(size) - left(90) - forward(size) - left(90) + draw_rectangle(size) -def rectangle_move(x, y, 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() + update() + + +def draw_rectangle(size): + """Draw rectangle line with given size.""" for count in range(2): forward(size) left(90) forward(size) left(90) - end_fill() - update() -def score(x, y, text): +def stamp(x, y, text, _color='black', font_size=10): """Display `text` at coordinates `x` and `y`.""" goto(x, y) - color('black') - write(text, font=('Arial', 10, 'normal')) + 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 rectangles of top and a moving rectangle.""" + """Draw rectangle frames and a moving rectangle.""" clear() - score(-200, -200, 'Score : ' + str(len(top) - 1)) + if i_size(len(overlap)) == goal_size: + stamp(-200, 100, 'Win!', _color='red', font_size=30) - size = top_size[len(top_size) - 1] + stamp(-200, -200, 'Score : ' + str(len(overlap) - 1)) - for i in range(0, len(top)): - size_i = top_size[i] - rectangle_top(top[i].x - size_i / 2, top[i].y - size_i / 2, size_i) + 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 - rectangle_move(x - (size / 2), y - (size / 2), size) + 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 @@ -78,15 +94,24 @@ def draw(): def tap(x, y): """Respond to screen click at a moving rectangle.""" - top[len(top)] = vector(rectangle.x, rectangle.y) - top_size[len(top)] = top_size[len(top) - 1] - 20 - if top[len(top) - 1].x - top_size[len(top) - 1] / 2 < top[len(top) - 2].x - top_size[len(top) - 2] / 2 or \ - top[len(top) - 1].y - top_size[len(top) - 1] / 2 < top[len(top) - 2].y - top_size[len(top) - 2] / 2 or \ - top[len(top) - 1].x + top_size[len(top) - 1] / 2 > top[len(top) - 2].x + top_size[len(top) - 2] / 2 or \ - top[len(top) - 1].y + top_size[len(top) - 1] / 2 > top[len(top) - 2].y + top_size[len(top) - 2] / 2: + 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() + elif curr_square.y - curr_size / 2 < prev_square.y - prev_size / 2: + end() + elif curr_square.x + curr_size / 2 > prev_square.x + prev_size / 2: + end() + elif curr_square.y + curr_size / 2 > prev_square.y + prev_size / 2: + end() + elif i_size(len(overlap)) == goal_size: move.y = 0 - del top[len(top) - 1] - del top_size[len(top_size) - 1] setup(width=420, height=420) @@ -94,5 +119,4 @@ def tap(x, y): tracer(False) onscreenclick(tap) draw() - done() From c9aa11392a45500d08abb3827bad157b3023f173 Mon Sep 17 00:00:00 2001 From: Yijungu Date: Fri, 25 Nov 2022 18:39:24 +0900 Subject: [PATCH 3/6] end() -> done() --- freegames/overlap.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/freegames/overlap.py b/freegames/overlap.py index 1263b347..b7e9d90b 100644 --- a/freegames/overlap.py +++ b/freegames/overlap.py @@ -35,7 +35,6 @@ def filled_ractangle(x, y, size): begin_fill() draw_rectangle(size) end_fill() - update() def draw_rectangle(size): @@ -71,6 +70,7 @@ def draw(): 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)) @@ -103,13 +103,13 @@ def tap(x, y): 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 From 643ff2aa8a42edc798220396fd1a6a7cb8cccc5d Mon Sep 17 00:00:00 2001 From: Yijungu Date: Fri, 25 Nov 2022 19:42:41 +0900 Subject: [PATCH 4/6] Tutle race game --- freegames/turtlerace.py | 76 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 freegames/turtlerace.py diff --git a/freegames/turtlerace.py b/freegames/turtlerace.py new file mode 100644 index 00000000..64e962fa --- /dev/null +++ b/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() From a86cee0e692e8047fe9beaff25be0a429da0e893 Mon Sep 17 00:00:00 2001 From: Yijungu <81271644+Yijungu@users.noreply.github.com> Date: Fri, 25 Nov 2022 19:50:23 +0900 Subject: [PATCH 5/6] Delete turtlerace.py --- freegames/turtlerace.py | 76 ----------------------------------------- 1 file changed, 76 deletions(-) delete mode 100644 freegames/turtlerace.py diff --git a/freegames/turtlerace.py b/freegames/turtlerace.py deleted file mode 100644 index 64e962fa..00000000 --- a/freegames/turtlerace.py +++ /dev/null @@ -1,76 +0,0 @@ -"""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() From 9cbde0fedfd9bd48a70034a2ee7602e392c40843 Mon Sep 17 00:00:00 2001 From: Yijungu Date: Wed, 30 Nov 2022 16:12:40 +0900 Subject: [PATCH 6/6] modify code --- freegames/overlap.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/freegames/overlap.py b/freegames/overlap.py index b7e9d90b..65b814a5 100644 --- a/freegames/overlap.py +++ b/freegames/overlap.py @@ -103,12 +103,16 @@ def tap(x, y): 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