From a23d78a6351b30a56fca05a9aad3c8cd727be944 Mon Sep 17 00:00:00 2001 From: Gabriela20103967 <20103967@tafe.wa.edu.au> Date: Wed, 29 May 2024 13:34:40 +0800 Subject: [PATCH 1/3] feat:add the two new files where will be the modularization of the game --- Caterpillar_Game/caterpillarGame.py | 0 Caterpillar_Game/gameDesign.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Caterpillar_Game/caterpillarGame.py create mode 100644 Caterpillar_Game/gameDesign.py diff --git a/Caterpillar_Game/caterpillarGame.py b/Caterpillar_Game/caterpillarGame.py new file mode 100644 index 00000000..e69de29b diff --git a/Caterpillar_Game/gameDesign.py b/Caterpillar_Game/gameDesign.py new file mode 100644 index 00000000..e69de29b From a293550cca8f0cf19787630e33f9a8368ecad6ff Mon Sep 17 00:00:00 2001 From: Gabriela20103967 <20103967@tafe.wa.edu.au> Date: Wed, 29 May 2024 15:08:49 +0800 Subject: [PATCH 2/3] feat:modularize the file game Caterpillar into the two files caterpillarGame and gameDesign --- Caterpillar_Game/Caterpillar.py | 113 ------------------ .../__pycache__/gameDesign.cpython-39.pyc | Bin 0 -> 1819 bytes Caterpillar_Game/caterpillarGame.py | 84 +++++++++++++ Caterpillar_Game/gameDesign.py | 48 ++++++++ 4 files changed, 132 insertions(+), 113 deletions(-) delete mode 100644 Caterpillar_Game/Caterpillar.py create mode 100644 Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc diff --git a/Caterpillar_Game/Caterpillar.py b/Caterpillar_Game/Caterpillar.py deleted file mode 100644 index 4afc8538..00000000 --- a/Caterpillar_Game/Caterpillar.py +++ /dev/null @@ -1,113 +0,0 @@ -import turtle as t -import random as rd - -t.bgcolor('yellow') - -caterpillar = t.Turtle() -caterpillar.shape('square') -caterpillar.speed(0) -caterpillar.penup() -caterpillar.hideturtle() - -leaf = t.Turtle() -leaf_shape = ((0,0),(14,2),(18,6),(20,20),(6,18),(2,14)) -t.register_shape('leaf', leaf_shape) -leaf.shape('leaf') -leaf.color('green') -leaf.penup() -leaf.hideturtle() -leaf.speed() - -game_started = False -text_turtle = False -text_turtle = t.Turtle() -text_turtle.write('Press SPACE to start', align='center', font=('Arial', 18, 'bold')) -text_turtle.hideturtle() - -score_turtle = t.Turtle() -score_turtle.hideturtle() -score_turtle.speed(0) - -def outside_window(): - left_wall = -t.window_width()/2 - right_Wall = t.window_width()/2 - top_wall = t.window_height()/2 - bottom_wall = -t.window_height()/2 - (x,y) = caterpillar.pos() - outside = x < left_wall or x > right_Wall or y > top_wall or y < bottom_wall - return outside - -def game_over(): - caterpillar.color('yellow') - leaf.color('yellow') - t.penup() - t.hideturtle() - t.write('GAME OVER !', align='center', font=('Arial', 30, 'normal') ) - t.onkey(start_game,'space') - -def display_score(current_score): - score_turtle.clear() - score_turtle.penup() - x = (t.window_width()/2) - 70 - y = (t.window_height()/2) - 70 - score_turtle.setpos(x,y) - score_turtle.write(str(current_score), align='right', font=('Arial', 40, 'bold')) - -def place_leaf(): - leaf.hideturtle() - leaf.setx(rd.randint(-200,200)) - leaf.sety(rd.randint(-200,200)) - leaf.showturtle() - -def start_game(): - global game_started - if game_started: - return - game_started = True - - score = 0 - text_turtle.clear() - - caterpillar_speed = 2 - caterpillar_length = 3 - caterpillar.shapesize(1,caterpillar_length,1) - caterpillar.showturtle() - display_score(score) - place_leaf() - - while True: - caterpillar.forward(caterpillar_speed) - if caterpillar.distance(leaf) < 20: - place_leaf() - caterpillar_length = caterpillar_length + 1 - caterpillar.shapesize(1,caterpillar_length,1) - caterpillar_speed = caterpillar_speed + 1 - score = score + 10 - display_score(score) - if outside_window(): - game_over() - break - -def move_up(): - caterpillar.setheading(90) - -def move_down(): - caterpillar.setheading(270) - -def move_left(): - caterpillar.setheading(180) - -def move_right(): - caterpillar.setheading(0) - -def restart_game(): - start_game() - -t.onkey(start_game,'space') -t.onkey(restart_game,'Up') -t.onkey(move_up,'Up') -t.onkey(move_right,'Right') -t.onkey(move_down,'Down') -t.onkey(move_left,'Left') -t.listen() -t.mainloop() diff --git a/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc b/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc new file mode 100644 index 0000000000000000000000000000000000000000..bbb701d231433d27b793d00c6729e1601b390f90 GIT binary patch literal 1819 zcmZuxUvJ|?5a0FxI%zqM9`pc#4k2`w=t)Qj5JIS^h%jexD?m4!Z-6&0! zd%An&J0y?yO%SiV?JH0D3cQq=b>gPgt>xL7*_p9t_BWI5@5c<+JOBKCem`LBUlMkg zi^2)M>N6yQ30|@Rx4ScNpEBVH_caslD`(&d{wqtoHQKSWoapXyP&mO?eThWc7z0Oe z;k;%8SGdB%-4nhDaQ8(hBHRP9hY@2FKg}lcXQ_%&m2-68E{f^ldy2368;NF1F8GRb z;+z#*u+&|!3wP-WhqRXWh1>DLifyzZah+Eryq6rMJ>f6-%CR;PM)5bE(J~f6$8V!V zOB)5h5-m4h=_2+}+iTA|T_1J*&Zoci5e%~q&rC3vWm(Tmp#GR>@} zRIaHIiL<^>JYFL4hq%F>(5lC4j`-W>1J|E?ES%V3Fn}RN- z>E)T0&=h5vf$`NiYb0)sl*0Iptfq~L$AysES_1*e9Dp1Z3cb>ec^k2(<)uzrL(^Az z4RRwTj`5T%&j&I6PwVxD!4U@g@7d}1!)Ho@8cu4+${}Llg|3@njUPW&^%QbBZ034g zSI3j0DvledFJ!KVr#HWy5_X284nR+uxrx#gt#z7G9n=>{7>}Kp_wk7>=ULuLypJbz z8uP37Zh-h?8zUzKZWr+s)FSd5WblR&lN*#-Oj<0lrMuW7l3jQUMD4;A&Yv#F9>d#r z@%D3BVHw{z#?MMbzVXiMN+&)%z_7;8v04D{67P*_T^oM}g_em2ACNAbNTsroG*MHl zLKk%fpP=5BwIvV9EVfQ5T@D>9LJHObpFY2Ou#@eLT~1)zu3PyIQTw)hHTK^MC&x!P zEsoer2W5XprVeGxVBsv>O{cMoP)7^?iql*Wepi*?3<$pKUyv9diqTjnuJM0@A}iOA zN$EYWO93Avo9w~+xWT7Hs7%m9V*0b967?*d6+(~A{ze*0iiM3PHI@q=k)^9NY^&*8 zz#4vO`221O_eG(aGMlH?=5usaeI#ua@9}&5fL|T$RPvVjt#L;EP5D{yoFH?zm{dp_ znX3*uOaCo8JB~ImD!#kEzFz-@!IIE6DA16gj?uVpW~EWyLFSMj-iFML-B}V+&sErjX=>s$oz!Al65mf#oNZ-$0v}>LpbElQ zM7||Lb6|%MYGFI~?YAQyJ&b}Vvf9Kq!DbrKbq@l{uAacxTeqE1HnQ!4=!f=)exw^v JglP=w{11Qzuk-)_ literal 0 HcmV?d00001 diff --git a/Caterpillar_Game/caterpillarGame.py b/Caterpillar_Game/caterpillarGame.py index e69de29b..3a06b51a 100644 --- a/Caterpillar_Game/caterpillarGame.py +++ b/Caterpillar_Game/caterpillarGame.py @@ -0,0 +1,84 @@ +import turtle as t +from gameDesign import GameDesign + +class CaterpillarGame: + def __init__(self): + self.design = GameDesign() + self.game_started = False + self.score = 0 + self.caterpillar_speed = 2 + self.caterpillar_length = 3 + + self.design.write_text('Press Space to start', (0, 0), ('Arial', 18, 'bold')) + + def start_game(self): + if self.game_started: + return + self.game_started = True + + self.score = 0 + self.design.text_turtle.clear() + + self.caterpillar_speed = 2 + self.caterpillar_length = 3 + self.design.caterpillar.shapesize(1, self.caterpillar_length, 1) + self.design.caterpillar.showturtle() + + self.design.display_score(self.score) + self.design.place_leaf() + + self.run_game_loop() + + def run_game_loop(self): + while True: + self.design.caterpillar.forward(self.caterpillar_speed) + if self.design.caterpillar.distance(self.design.leaf) < 20: + self.design.place_leaf() + self.caterpillar_length += 1 + self.design.caterpillar.shapesize(1, self.caterpillar_length, 1) + self.caterpillar_speed += 1 + self.score += 10 + self.design.display_score(self.score) + if self.outside_window(): + self.game_over() + break + + def outside_window(self): + left_wall = -t.window_width() / 2 + right_wall = t.window_width() / 2 + top_wall = t.window_height() / 2 + bottom_wall = -t.window_height() / 2 + (x, y) = self.design.caterpillar.pos() + outside = x < left_wall or x > right_wall or y > top_wall or y < bottom_wall + return outside + + def game_over(self): + self.design.caterpillar.color('yellow') + self.design.leaf.color('yellow') + t.penup() + t.hideturtle() + t.write('Game Over!', align='center', font=('Arial', 30, 'normal')) + t.onkey(self.start_game, 'space') + + def move_up(self): + self.design.caterpillar.setheading(90) + + def move_down(self): + self.design.caterpillar.setheading(270) + + def move_left(self): + self.design.caterpillar.setheading(180) + + def move_right(self): + self.design.caterpillar.setheading(0) + +game = CaterpillarGame() + +t.onkey(game.start_game, 'space') +t.onkey(game.move_up, 'Up') +t.onkey(game.move_right, 'Right') +t.onkey(game.move_down, 'Down') +t.onkey(game.move_left, 'Left') +t.listen() +t.mainloop() + diff --git a/Caterpillar_Game/gameDesign.py b/Caterpillar_Game/gameDesign.py index e69de29b..2ba6b2c3 100644 --- a/Caterpillar_Game/gameDesign.py +++ b/Caterpillar_Game/gameDesign.py @@ -0,0 +1,48 @@ +import turtle as t +import random as rd + +class GameDesign: + def __init__(self): + t.bgcolor('yellow') + + self.caterpillar = t.Turtle() + self.caterpillar.shape('square') + self.caterpillar.speed(0) + self.caterpillar.penup() + self.caterpillar.hideturtle() + + self.leaf = t.Turtle() + leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14)) + t.register_shape('leaf', leaf_shape) + self.leaf.shape('leaf') + self.leaf.color('green') + self.leaf.penup() + self.leaf.hideturtle() + self.leaf.speed(0) + + self.text_turtle = t.Turtle() + self.text_turtle.hideturtle() + + self.score_turtle = t.Turtle() + self.score_turtle.hideturtle() + self.score_turtle.speed(0) + + def write_text(self, message, position, font): + self.text_turtle.clear() + self.text_turtle.penup() + self.text_turtle.goto(position) + self.text_turtle.write(message, align='center', font=font) + + def display_score(self, score): + self.score_turtle.clear() + self.score_turtle.penup() + x = (t.window_width() / 2) - 70 + y = (t.window_height() / 2) - 70 + self.score_turtle.setpos(x, y) + self.score_turtle.write(str(score), align='right', font=('Arial', 40, 'bold')) + + def place_leaf(self): + self.leaf.hideturtle() + self.leaf.setx(rd.randint(-200, 200)) + self.leaf.sety(rd.randint(-200, 200)) + self.leaf.showturtle() From 8e5cd6a1d37ad3c2250942b10355911063c59b1a Mon Sep 17 00:00:00 2001 From: Gabriela20103967 <20103967@tafe.wa.edu.au> Date: Wed, 29 May 2024 15:32:12 +0800 Subject: [PATCH 3/3] feat:improve the modularization of the game --- .../__pycache__/gameDesign.cpython-39.pyc | Bin 1819 -> 2614 bytes Caterpillar_Game/caterpillarGame.py | 20 +++++++++++------- Caterpillar_Game/gameDesign.py | 12 +++++++++++ 3 files changed, 24 insertions(+), 8 deletions(-) diff --git a/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc b/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc index bbb701d231433d27b793d00c6729e1601b390f90..193d881c584f5723dddd28cbb6d95509e0fb0ad9 100644 GIT binary patch literal 2614 zcmZ`*&2HO95a#kvG%Y!f<1}uQ07?H)qIG&}gR}@-xM*&=#K}PrKrr03M3*Aj-4$dR z$|>{`%+j=}F5l{>4G_ zv0!inulO7UH7H4q9?{zDSuYG~QtQ1zt#@Y6rsSm&*mGECbYqmw$ArNVyy64|(>{78 zCDeRx^ek#o8?;Rw>VkG?i+Z44xYANjGp*TQE5;LJ@Pu zGIhHg#gSyZNYXUo$}gu<7M<2VOLi_pIp#8DYOnr2=d7B$Cdw8pJ-s&gHpKMbtK(<= zSAucTALTqs`;f@*GB5gh!Lmb~NH1$zj3sGZxGK@iK}K0q74FC_E{q z$_qnSEyK{k>~*n;SN&bwSt9nNUbhAvQb0qu1BL|1D7hjfIDy$!?v$lzJ_$@kq-wnx z#Ce)?z6EP_UZrT`&<25qz+xEW;$CC-8Xv^>;}JZA?=XTZC|zjIj5G7nDx=Z1O-+nU z+fG?XSmD;C3@nIN{4tI=7s?+d6o`N& z-IyU_j4msjebRZGh|Gpjn! z##u{xGxCn$jYypZDsGqKX`BBBLODDc3}s*`=NFzt>HHgJxMZ}Pr^w;RdV-=G^+&LZ?rje)#ZV z{x>*-Hjgbx^B=EVd9F-Odo3PiG|8l%n!*J>%qJgj?aGH#V-{j7`4PM=2(wT2R|&IR z*A3cu{bq0@QpIo6V%r5Efonemom&@>w%w zN3-IN^VEvO>|6W7#*%~2V|1?Jg4CU|g>8C#ba7Woe=bsfQ3Y!oe{O7W8GpW7rn0PKw!W-axe#+XQZA>Kz+Jx6smJ!WJyVTNvgTZXy<9g{ngRVJ-EDkN21bGOj**oz^baq5!T@^K}Apg}VCXMGyCp z>RM3(854KSWSX68Ho5HINJqsuOD3`D>8L0E2Tn%@o9efJ`ygV*;<8Ghq%UJSn