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/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 new file mode 100644 index 00000000..66e5b6a4 Binary files /dev/null and b/Caterpillar_Game/__pycache__/gameDesign.cpython-39.pyc differ diff --git a/Caterpillar_Game/caterpillarGame.py b/Caterpillar_Game/caterpillarGame.py new file mode 100644 index 00000000..91564b28 --- /dev/null +++ b/Caterpillar_Game/caterpillarGame.py @@ -0,0 +1,133 @@ +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 + self.caterpillar_speed = 2 + self.caterpillar_length = 3 + + self.design.write_text('Press Space to start', (0, 0), ('Arial', 18, 'bold')) + 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') + t.onkey(self.move_down, 'Down') + t.onkey(self.move_left, 'Left') + 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 + + 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): + """ + 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: + 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): + """ + 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 + 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): + """ + 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() + t.hideturtle() + t.write('Game Over!', align='center', font=('Arial', 30, 'normal')) + 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 new file mode 100644 index 00000000..51e671bb --- /dev/null +++ b/Caterpillar_Game/gameDesign.py @@ -0,0 +1,105 @@ +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() + self.setup_text_turtle() + self.setup_score_turtle() + + def setup_screen(self): + """ + 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) + self.caterpillar.penup() + 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) + self.leaf.shape('leaf') + self.leaf.color('green') + self.leaf.penup() + self.leaf.hideturtle() + 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 + 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): + """ + 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()