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

Feat: Modularize the game Caterpillar #266

Open
wants to merge 3 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.

Binary file not shown.
88 changes: 88 additions & 0 deletions Caterpillar_Game/caterpillarGame.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
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'))
self.bind_keys()

def bind_keys(self):
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):
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)

if __name__ == '__main__':
game = CaterpillarGame()
t.mainloop()


60 changes: 60 additions & 0 deletions Caterpillar_Game/gameDesign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import turtle as t
import random as rd

class GameDesign:
def __init__(self):
self.setup_screen()
self.setup_caterpillar()
self.setup_leaf()
self.setup_text_turtle()
self.setup_score_turtle()

def setup_screen(self):
t.bgcolor('yellow')

def setup_caterpillar(self):
self.caterpillar = t.Turtle()
self.caterpillar.shape('square')
self.caterpillar.speed(0)
self.caterpillar.penup()
self.caterpillar.hideturtle()

def setup_leaf(self):
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):
self.text_turtle = t.Turtle()
self.text_turtle.hideturtle()

def setup_score_turtle(self):
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()