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/4] 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/4] 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/4] 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 Date: Thu, 6 Jun 2024 20:56:55 +0800 Subject: [PATCH 4/4] docs: update the docstring in the caterpillargame and game design, and update the README file with the new changes --- Caterpillar_Game/README.md | 53 +++++++++++++++++- .../__pycache__/gameDesign.cpython-39.pyc | Bin 2614 -> 2612 bytes Caterpillar_Game/caterpillarGame.py | 49 +++++++++++++++- Caterpillar_Game/gameDesign.py | 49 +++++++++++++++- 4 files changed, 145 insertions(+), 6 deletions(-) diff --git a/Caterpillar_Game/README.md b/Caterpillar_Game/README.md index 9d6cc298..09806be0 100644 --- a/Caterpillar_Game/README.md +++ b/Caterpillar_Game/README.md @@ -9,7 +9,9 @@ ## 🛠ī¸ Description -A simple Caterpillar game built in python. +A simple Caterpillar game built in Python where the player controls a caterpillar to +eat leaves and grow in length while avoiding the window boundaries. The game involves +increasing the score by eating leaves and ends if the caterpillar goes outside the window boundaries. ## ⚙ī¸ Languages or Frameworks Used ```bash @@ -20,11 +22,58 @@ pip install turtle Running the script is really simple! Just open a terminal in the folder where your script is located and run the following command: ```sh -python Caterpillar.py +python caterpillarGame.py ``` ## đŸ“ē Demo

+## 🕹ī¸ Game Instructions +- Start the Game: Press the 'space' key to start the game. +- Move Up: Press the 'up arrow' key to move the caterpillar up. +- Move Down: Press the 'down arrow' key to move the caterpillar down. +- Move Left: Press the 'left arrow' key to move the caterpillar left. +- Move Right: Press the 'Right arrow' key to move the caterpillar right. + +## 🎮 Gameplay Features +- Score: Gain 10 points for each leaf eaten. +- Caterpillar Growth: The caterpillar grows in length and increases in speed with each leaf eaten. +- Game Over: The game ends if the caterpillar hits the window boundary. + +## 🧩 Classes and Methods +`CaterpillarGame` +- Attributes: + - design (GameDesign): Handles game visuals. + - game_started (bool): Indicates if the game has started. + - score (int): Current game score. + - caterpillar_speed (int): Speed of the caterpillar. + - caterpillar_length (int): Length of the caterpillar. + +- Methods: + - __init__(): Initializes the game state and binds keyboard keys. + - bind_keys(): Binds arrows keys and space key. + - start_game(): Starts the game, resets score, speed, and length, and places the first leaf. + - run_game_loop(): Main game loop to move caterpillar, check collisions, update score, and check game over. + - outside_window(): Checks if the caterpillar is outside window boundaries. + - game_over(): Ends the game and displays 'Game Over' text. + - move_up(), move_down(), move_left(), move_right(): Changes caterpillar's direction. + +`GameDesign` +- Attributes: + - caterpillar (turtle.Turtle): Represents the caterpillar. + - leaf (turtle.Turtle): Represents the leaf. + - text_turtle (turtle.Turtle): Displays text messages. + - score_turtle (turtle.Turtle): Displays the score. + +- Methods: + - __init__(): Sets up the screen, caterpillar, leaf, and text and score turtles. + - setup_screen(): Sets up the game screen. + - setup_caterpillar(): Sets up the caterpillar. + - setup_leaf(): Sets up the leaf. + - setup_text_turtle(), setup_score_turtle(): Set up text and score turtles. + - write_text (message, position, font): Writes a text message on the screen. + - display_score(score): Displays the score. + - place_leaf(): Places the leaf at a random position within the window. + ## 🤖 Author [Leah Nguyen](https://github.com/ndleah) \ No newline at end of file diff --git a/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc b/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc index 193d881c584f5723dddd28cbb6d95509e0fb0ad9..66e5b6a4ae2a9773eaf98d456853d2c9cc5fc55e 100644 GIT binary patch delta 335 zcmdlcvPFb9k(ZZ?0SI&-g{1{;FnqHKenpeaKlI1Mo1IY@42pNzPzM|Ch%;J*NqWI#B#Ddf! zVUQqea(+&JQIRM}l^BQ+nLL4Ih6G&eE$))kijw$}(xQ@_)FQFT4y;q;5RyE_$@xX8 za9N0c2Udy6Tx^SZD*fStfM z`5C(=qxxh4j_Yh7M+0qq%dw4H3&b=B5w?@pak>kGToA=pl$e*2pIc-F5@!O6i%*v4 G5(fYn0#674 delta 309 zcmdlYvQ30Hk(ZZ?0SH#z2ulmu$ZN{Paf_`oH76&(e6k1AHb(x<|CuxxC1rprG?|K| zfmD$cge?hTOH6iPkuc*2F@->cEReXxSCpEbSzMA@6knW?SddyI0up3R&d4a(+=NTm{6853IX+ z6@W~j`8lbHK=UW>VAEz)o&1FD2&3fWmFz0w%0O97<ySdNmLMb^`z8C+wPx8k6}r ruCsw04Yct!$2M+l5Yqxg*iBx;>COkTJ&LU;F)t-Qx5#=jBbPA%X4Ooj diff --git a/Caterpillar_Game/caterpillarGame.py b/Caterpillar_Game/caterpillarGame.py index d9005362..91564b28 100644 --- a/Caterpillar_Game/caterpillarGame.py +++ b/Caterpillar_Game/caterpillarGame.py @@ -1,8 +1,23 @@ import turtle as t from gameDesign import GameDesign + class CaterpillarGame: + """ + The CaterPillarGame is a game where the player controls a caterpillar to eat leaves and grow in length, + while avoiding the window boundaries. + + Attributes: + design (GameDesign): An instance of the GameDesign class to handle game visuals. + game_started (bool): A flag indicating if the game has started. + score (int): The current score of the game. + caterpillar_speed (int): The current speed of the caterpillar. + caterpillar_length (int): The current length of the caterpillar. + """ def __init__(self): + """ + Initializes the game by setting up the initial game state and binding keyboard keys. + """ self.design = GameDesign() self.game_started = False self.score = 0 @@ -13,6 +28,9 @@ def __init__(self): self.bind_keys() def bind_keys(self): + """ + Binds the arrow keys for controlling the caterpillar and the space key to start the game. + """ t.onkey(self.start_game, 'space') t.onkey(self.move_up, 'Up') t.onkey(self.move_right, 'Right') @@ -21,6 +39,9 @@ def bind_keys(self): t.listen() def start_game(self): + """ + Start the game, resetting the score, caterpillar's speed, and length. Places the first leaf. + """ if self.game_started: return self.game_started = True @@ -39,6 +60,10 @@ def start_game(self): self.run_game_loop() def run_game_loop(self): + """ + Runs the main game loop, moving the caterpillar, checking for collisions with leaves, + updating the score, and checking for a game over conditions. + """ while True: self.design.caterpillar.forward(self.caterpillar_speed) if self.design.caterpillar.distance(self.design.leaf) < 20: @@ -53,6 +78,12 @@ def run_game_loop(self): break def outside_window(self): + """ + Checks if the caterpillar is outside the window boundaries. + + Returns: + bool: True if the capillar is outside the window, False otherwise. + """ left_wall = -t.window_width() / 2 right_wall = t.window_width() / 2 top_wall = t.window_height() / 2 @@ -62,6 +93,9 @@ def outside_window(self): return outside def game_over(self): + """ + Ends the game by changing the color of the caterpillar and leaf, and displaying 'Game Over' text. + """ self.design.caterpillar.color('yellow') self.design.leaf.color('yellow') t.penup() @@ -70,19 +104,30 @@ def game_over(self): t.onkey(self.start_game, 'space') def move_up(self): + """ + Changes the caterpillar's direction to up + """ self.design.caterpillar.setheading(90) def move_down(self): + """ + Changes the caterpillar's direction to down. + """ self.design.caterpillar.setheading(270) def move_left(self): + """ + Changes the caterpillar's direction to left + """ self.design.caterpillar.setheading(180) def move_right(self): + """ + Changes the caterpillar's direction to right + """ self.design.caterpillar.setheading(0) + if __name__ == '__main__': game = CaterpillarGame() t.mainloop() - - diff --git a/Caterpillar_Game/gameDesign.py b/Caterpillar_Game/gameDesign.py index bfb2e579..51e671bb 100644 --- a/Caterpillar_Game/gameDesign.py +++ b/Caterpillar_Game/gameDesign.py @@ -1,8 +1,22 @@ import turtle as t import random as rd + class GameDesign: + """ + The GameDesign class handles the setup and visual aspects of the caterpillar game, + including the screen, caterpillar, leaf, text and score display. + + Attributes: + caterpillar (turtle.Turtle): The turtle representing the caterpillar. + leaf (turtle.Turtle): The turtle representing the leaf. + text_turtle (turtle.Turtle): The turtle for displaying text messages. + score_turtle (turtle.Turtle): The turtle for displaying the score. + """ def __init__(self): + """ + Initializes the game design by setting up the screen, caterpillar, leaf, and text and score turtles. + """ self.setup_screen() self.setup_caterpillar() self.setup_leaf() @@ -10,9 +24,15 @@ def __init__(self): self.setup_score_turtle() def setup_screen(self): - t.bgcolor('yellow') + """ + Sets up the game screen with a pink background. + """ + t.bgcolor('pink') def setup_caterpillar(self): + """ + Sets up the caterpillar turtle with a square shape, hidden initially. + """ self.caterpillar = t.Turtle() self.caterpillar.shape('square') self.caterpillar.speed(0) @@ -20,6 +40,9 @@ def setup_caterpillar(self): self.caterpillar.hideturtle() def setup_leaf(self): + """ + Sets up the leaf turtle with a custom shape and green color, hidden initially. + """ self.leaf = t.Turtle() leaf_shape = ((0, 0), (14, 2), (18, 6), (20, 20), (6, 18), (2, 14)) t.register_shape('leaf', leaf_shape) @@ -30,21 +53,41 @@ def setup_leaf(self): self.leaf.speed(0) def setup_text_turtle(self): + """ + Sets up the text turtle for displaying messages, hidden initially. + """ self.text_turtle = t.Turtle() self.text_turtle.hideturtle() def setup_score_turtle(self): + """ + Sets up the score turtle for displaying the score, hidden initially. + """ self.score_turtle = t.Turtle() self.score_turtle.hideturtle() self.score_turtle.speed(0) def write_text(self, message, position, font): + """ + Writes a text message at a specified position on the screen. + + Parameters: + message (str): The message to be displayed. + position (tuple): The (x, y) position for the message. + font (tuple): The font type, size, and style. + """ 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): + """ + Displays the score in the top right corner of the screen. + + Parameters: + score (int): The current game score. + """ self.score_turtle.clear() self.score_turtle.penup() x = (t.window_width() / 2) - 70 @@ -53,8 +96,10 @@ def display_score(self, score): self.score_turtle.write(str(score), align='right', font=('Arial', 40, 'bold')) def place_leaf(self): + """ + Places the leaf turtles at a random position within the window boundaries. + """ self.leaf.hideturtle() self.leaf.setx(rd.randint(-200, 200)) self.leaf.sety(rd.randint(-200, 200)) self.leaf.showturtle() -