def __init__(self): self._arena = Arena((480, 360)) Ball(self._arena, (40, 80)) Ball(self._arena, (80, 40)) Ghost(self._arena, (120, 80)) self._hero = Turtle(self._arena, (80, 80)) self._playtime = 120 # seconds
def test_collide_ball(self): a = Arena((480, 360)) b = Ball(a, (0, 0)) t = Turtle(a, (230, 170)) t.collide(b) t.collide(b) # no effect self.assertTrue(t.lives() == 2)
def test_right(self): a = Arena((480, 360)) t = Turtle(a, (230, 170)) t.go_right(True) t.move() t.move() t.go_right(False) t.move() # no effect self.assertTrue(t.position() == (234, 170, 20, 20))
def test_move(self): a = Arena((480, 360)) test_values = ((40, 80, 45, 85), (40, 215, 45, 220), (40, 340, 45, 335), (295, 80, 300, 85), (460, 80, 455, 85)) for param in test_values: x0, y0, x1, y1 = param b = Ball(a, (x0, y0)) b.move() self.assertTrue(b.position() == (x1, y1, 20, 20))
class BounceGame: def __init__(self): self._arena = Arena((480, 360)) Ball(self._arena, (40, 80)) Ball(self._arena, (80, 40)) Ghost(self._arena, (120, 80)) self._hero = Turtle(self._arena, (80, 80)) self._playtime = 120 # seconds def arena(self) -> Arena: return self._arena def hero(self) -> Turtle: return self._hero def game_over(self) -> bool: return self._hero.lives() <= 0 def game_won(self) -> bool: return self.remaining_time() <= 0 def remaining_time(self) -> int: return (self._playtime - self._arena.count() // 30)
#!/usr/bin/env python3 ''' @author Michele Tomaiuolo - http://www.ce.unipr.it/people/tomamic @license This software is free - http://www.gnu.org/licenses/gpl.html ''' import g2d from p3_oop_bounce import Arena, Ball, Ghost, Turtle arena = Arena((480, 360)) b1 = Ball(arena, (40, 80)) b2 = Ball(arena, (80, 40)) g = Ghost(arena, (120, 80)) turtle = Turtle(arena, (80, 80)) sprites = g2d.load_image("sprites.png") def tick(): if g2d.key_pressed("ArrowUp"): turtle.go_up() elif g2d.key_pressed("ArrowRight"): turtle.go_right() elif g2d.key_pressed("ArrowDown"): turtle.go_down() elif g2d.key_pressed("ArrowLeft"): turtle.go_left() elif (g2d.key_released("ArrowUp") or g2d.key_released("ArrowRight") or g2d.key_released("ArrowDown") or g2d.key_released("ArrowLeft")): turtle.stay() arena.move_all() # Game logic
def test_corner(self): a = Arena((480, 360)) b = Ball(a, (460, 340)) # dx = 5, dy = 5 b.move() # dx = -5, dy = -5 b.move() self.assertTrue(b.position() == (450, 330, 20, 20))
def test_collide_ghost(self): a = Arena((480, 360)) g = Ghost(a, (0, 0)) t = Turtle(a, (230, 170)) t.collide(g) self.assertTrue(t.lives() == 0)