Skip to content

Commit

Permalink
Add test cases for Caterpillar game (Issue ndleah#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
1zj23 committed Nov 13, 2024
1 parent 057cab7 commit 22912b1
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
31 changes: 29 additions & 2 deletions Caterpillar_Game/Caterpillar.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

# Leaf setup
leaf = t.Turtle()
leaf_shape = ((0,0), (14,2), (18,6), (20,20), (6,18), (2,14))
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')
Expand All @@ -34,6 +34,7 @@
# Game state variables
game_started = False


def outside_window():
"""Check if the caterpillar is outside the window."""
left_wall = -t.window_width() / 2
Expand All @@ -44,6 +45,7 @@ def outside_window():
outside = x < left_wall or x > right_wall or y > top_wall or y < bottom_wall
return outside


def game_over():
"""Handle game over scenario."""
caterpillar.color('yellow')
Expand All @@ -53,6 +55,7 @@ def game_over():
t.write('GAME OVER!', align='center', font=('Arial', 30, 'normal'))
t.onkey(start_game, 'space') # Allow the user to restart the game by pressing SPACE


def display_score(current_score):
"""Display the score on the screen."""
score_turtle.clear()
Expand All @@ -62,13 +65,15 @@ def display_score(current_score):
score_turtle.setpos(x, y)
score_turtle.write(str(current_score), align='right', font=('Arial', 40, 'bold'))


def place_leaf():
"""Randomly place the leaf on the screen."""
leaf.hideturtle()
leaf.setx(rd.randint(-200, 200))
leaf.sety(rd.randint(-200, 200))
leaf.showturtle()


def start_game():
"""Start the game."""
global game_started
Expand Down Expand Up @@ -111,22 +116,27 @@ def start_game():
game_over()
break


def move_up():
if caterpillar.heading() != 270:
caterpillar.setheading(90)


def move_down():
if caterpillar.heading() != 90:
caterpillar.setheading(270)


def move_left():
if caterpillar.heading() != 0:
caterpillar.setheading(180)


def move_right():
if caterpillar.heading() != 180:
caterpillar.setheading(0)


def restart_game():
"""Restart the game when 'R' is pressed."""
global game_started
Expand All @@ -139,6 +149,7 @@ def restart_game():
t.clear() # Clear any game over text
start_game()


# Bind keys
t.onkey(start_game, 'space')
t.onkey(restart_game, 'r')
Expand All @@ -149,5 +160,21 @@ def restart_game():

# Listen to the keyboard inputs
t.listen()
t.mainloop()

# Add some assert-based tests for the game logic
def test_outside_window():
"""Test the outside_window function."""
# Move caterpillar to an out-of-bounds position
caterpillar.setpos(1000, 1000)
assert outside_window() == True, "Caterpillar should be outside the window."

# Move caterpillar to an in-bounds position
caterpillar.setpos(0, 0)
assert outside_window() == False, "Caterpillar should be inside the window."
print("test_outside_window passed.")

# Run tests
test_outside_window()

# Start the main loop
t.mainloop()
65 changes: 65 additions & 0 deletions Caterpillar_Game/test_caterpillar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import unittest
from unittest.mock import patch, MagicMock
import caterpillar

class TestCaterpillarGame(unittest.TestCase):

def setUp(self):
"""在每个测试之前运行"""
self.caterpillar = caterpillar.caterpillar

@patch('caterpillar.t.Turtle')
def test_start_game(self, MockTurtle):
"""测试开始游戏"""
mock_turtle = MockTurtle()
caterpillar.start_game()

# 验证调用了显示初始得分的方法
self.assertTrue(mock_turtle.write.called, "Failed to display score at the start.")

def test_outside_window(self):
"""测试毛毛虫是否在窗口外"""
# 设置位置为窗口边界外
self.caterpillar.setx(caterpillar.t.window_width() / 2 + 10)
self.caterpillar.sety(caterpillar.t.window_height() / 2 + 10)

# 使用断言来检查
self.assertTrue(caterpillar.outside_window(), "Caterpillar should be outside the window.")

@patch('caterpillar.t.Turtle')
def test_move_up(self, MockTurtle):
"""测试毛毛虫向上移动"""
caterpillar.caterpillar.setheading(0) # Initially set to right
caterpillar.move_up()
self.assertEqual(caterpillar.caterpillar.heading(), 90, "Caterpillar did not change heading to up correctly.")

@patch('caterpillar.t.Turtle')
def test_move_down(self, MockTurtle):
"""测试毛毛虫向下移动"""
caterpillar.caterpillar.setheading(0) # Initially set to right
caterpillar.move_down()
self.assertEqual(caterpillar.caterpillar.heading(), 270, "Caterpillar did not change heading to down correctly.")

@patch('caterpillar.t.Turtle')
def test_move_left(self, MockTurtle):
"""测试毛毛虫向左移动"""
caterpillar.caterpillar.setheading(90) # Initially set to up
caterpillar.move_left()
self.assertEqual(caterpillar.caterpillar.heading(), 180, "Caterpillar did not change heading to left correctly.")

@patch('caterpillar.t.Turtle')
def test_move_right(self, MockTurtle):
"""测试毛毛虫向右移动"""
caterpillar.caterpillar.setheading(180) # Initially set to left
caterpillar.move_right()
self.assertEqual(caterpillar.caterpillar.heading(), 0, "Caterpillar did not change heading to right correctly.")

@patch('caterpillar.t.Turtle')
def test_place_leaf(self, MockTurtle):
"""测试随机放置叶子"""
mock_leaf = MockTurtle()
caterpillar.place_leaf()
self.assertTrue(mock_leaf.showturtle.called, "Leaf was not displayed after placing.")

if __name__ == '__main__':
unittest.main()

0 comments on commit 22912b1

Please sign in to comment.