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

Enhance Documentation and Docstrings # 267 #268

Open
wants to merge 4 commits into
base: main
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 0 additions & 113 deletions Caterpillar_Game/Caterpillar.py

This file was deleted.

53 changes: 51 additions & 2 deletions Caterpillar_Game/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
<p align="center">
<img src="https://github.com/ndleah/python-mini-project/blob/main/IMG/caterpillar.gif" width=70% height=70%>

## 🕹️ 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)
Binary file not shown.
133 changes: 133 additions & 0 deletions Caterpillar_Game/caterpillarGame.py
Original file line number Diff line number Diff line change
@@ -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()
Loading