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

Ben's branch #20

Open
wants to merge 9 commits into
base: master
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
File renamed without changes.
12 changes: 12 additions & 0 deletions Bat/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from game.director import Director
from game.input_service import InputService
from game.output_service import OutputService
from asciimatics.screen import Screen

def main(screen):
input_service = InputService(screen)
output_service = OutputService(screen)
director = Director(input_service, output_service)
director.start_game()

Screen.wrapper(main)
3 changes: 3 additions & 0 deletions Bat/game/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
The game package contains specific classes for playing Hunter.
"""
102 changes: 102 additions & 0 deletions Bat/game/actor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
from game import constants
from game.point import Point

class Actor:
"""A visible, moveable thing that participates in the game. The responsibility of Actor is to keep track of its appearance, position
and velocity in 2d space.

Stereotype:
Information Holder

Attributes:
_text (string): The textual representation of the actor.
_position (Point): The actor's position in 2d space.
_velocity (Point): The actor's speed and direction.
"""

def __init__(self):
"""The class constructor.

Args:
self (Actor): an instance of Actor.
"""
self._text = ""
self._position = Point(0, 0)
self._velocity = Point(0, 0)

def get_position(self):
"""Gets the actor's position in 2d space.

Args:
self (Actor): an instance of Actor.

Returns:
Point: The actor's position in 2d space.
"""
return self._position

def get_text(self):
"""Gets the actor's textual representation.

Args:
self (Actor): an instance of Actor.

Returns:
string: The actor's textual representation.
"""
return self._text

def get_velocity(self):
"""Gets the actor's speed and direction.

Args:
self (Actor): an instance of Actor.

Returns:
Point: The actor's speed and direction.
"""
return self._velocity

def move_next(self):
"""Moves the actor to its next position according to its velocity. Will
wrap the position from one side of the screen to the other when it
reaches the boundary in either direction.

Args:
self (Actor): an instance of Actor.
"""
x1 = self._position.get_x()
y1 = self._position.get_y()
x2 = self._velocity.get_x()
y2 = self._velocity.get_y()
x = 1 + (x1 + x2 - 1) % (constants.MAX_X - 1)
y = 1 + (y1 + y2 - 1) % (constants.MAX_Y - 1)
position = Point(x, y)
self._position = position

def set_position(self, position):
"""Updates the actor's position to the given one.

Args:
self (Actor): An instance of Actor.
position (Point): The given position.
"""
self._position = position

def set_text(self, text):
"""Updates the actor's text to the given value.

Args:
self (Actor): An instance of Actor.
text (string): The given value.
"""
self._text = text

def set_velocity(self, velocity):
"""Updates the actor's velocity to the given one.

Args:
self (Actor): An instance of Actor.
position (Point): The given velocity.
"""
self._velocity = velocity
48 changes: 48 additions & 0 deletions Bat/game/ball.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
from game import constants
from game.actor import Actor
from game.point import Point

class Ball(Actor):

def __init__(self):
self._position = Point(30, 10)
self._velocity = Point(-1,1)
self.set_text("o")

def ball_collision(self, list):
x = self.get_velocity().get_x()
y = self.get_velocity().get_y()
for i in list:
if self.get_position().get_y() == i.get_position().get_y():
x = self.get_velocity().get_x()
y = -self.get_velocity().get_y()
self.set_velocity(Point(x,y))
return i



def wall_bounce(self):
x = self.get_velocity().get_x()
y = self.get_velocity().get_y()

if self.get_position().get_y() == -constants.MAX_Y - 1:
x = self.get_velocity().get_x()
y = -self.get_velocity().get_y()

if self.get_position().get_x() == constants.MAX_X - 1:
x = -self.get_velocity().get_x()
y = self.get_velocity().get_y()

self.set_velocity(Point(x,y))

def missed(self):
if self.get_position().get_y() == constants.MAX_Y - 1:
return True
else:
return False


def bounce(self):
x = self.get_velocity().get_x()
y = self.get_velocity().get_y()
self.set_velocity(Point(x,-y))
25 changes: 25 additions & 0 deletions Bat/game/bat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from game import constants
from game.actor import Actor
from game.point import Point

class Bat(Actor):
"""A limbless reptile. The responsibility of Snake is keep track of its segments. It contains methods for moving and growing among others.

Stereotype:
Structurer, Information Holder

Attributes:
_body (List): The snake's body (a list of Actor instances)
"""
def __init__(self):
self._position = Point(-constants.MAX_X + 8 , constants.MAX_Y - 2 )
self._velocity = Point(4,0)
self._text = "=========="

def right_or_left(self,point):
self._velocity = point

def bat_collision(self,point):
for i in range(10):
if point.get_position().get_x() == self.get_position().get_x() + i and point.get_position().get_y() == constants.MAX_Y - 2:
return True
22 changes: 22 additions & 0 deletions Bat/game/brick.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import random
from game import constants
from game.actor import Actor
from game.point import Point



class Brick(Actor):
def __init__(self,x,y):
self._position = Point(x,y)
self._velocity = Point(0,0)
self._text = "*"

def get_points(self):
return 5

def reset(self):
self.move_next()



# TODO: Define the Food class here
4 changes: 4 additions & 0 deletions Bat/game/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MAX_X = 60
MAX_Y = 20
SNAKE_LENGTH = 3
FRAME_LENGTH = 0.1
116 changes: 116 additions & 0 deletions Bat/game/director.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
from time import sleep
from game import constants, input_service
from game.brick import Brick
from game.score import Score
from game.bat import Bat
from game.ball import Ball
from game.point import Point

class Director:
"""A code template for a person who directs the game. The responsibility of
this class of objects is to control the sequence of play.

Stereotype:
Controller

Attributes:
food (Food): The snake's target.
input_service (InputService): The input mechanism.
keep_playing (boolean): Whether or not the game can continue.
output_service (OutputService): The output mechanism.
score (Score): The current score.
snake (Snake): The player or snake.
"""

def __init__(self, input_service, output_service):
"""The class constructor.

Args:
self (Director): an instance of Director.
"""
self.bricks = []
self.broken_bricks = ''
self._input_service = input_service
self._keep_playing = True
self._output_service = output_service
self._score = Score()
self._bat = Bat()
self._ball = Ball()

def start_game(self):
"""Starts the game loop to control the sequence of play.

Args:
self (Director): an instance of Director.
"""
y = 1
for row in range(4):
for col in range(constants.MAX_X):
brick = Brick(col,y)
self.bricks.append(brick)
y +=1

while self._keep_playing:
self._get_inputs()
self._do_updates()
self._do_outputs()
sleep(constants.FRAME_LENGTH)


def _get_inputs(self):
"""Gets the inputs at the beginning of each round of play. In this case,
that means getting the desired direction and moving the snake.

Args:
self (Director): An instance of Director.
"""
self._bat.right_or_left(self._input_service.get_direction())
self._ball.wall_bounce()
self.broken_bricks = self._ball.ball_collision(self.bricks)

if self._ball.get_position() == self._bat.get_position():
self._ball.bounce()




def _do_updates(self):
"""Updates the important game information for each round of play. In
this case, that means checking for a collision and updating the score.

Args:
self (Director): An instance of Director.
"""
self._bat.move_next()
self._ball.move_next()

for i in self.bricks:
if i == self.broken_bricks:
self.bricks.remove(i)
self._score.add_points(i.get_points())

if self._bat.bat_collision(self._ball):
self._ball.bounce()

if self._ball.missed():
self._keep_playing = False


def _do_outputs(self):
"""Outputs the important game information for each round of play. In
this case, that means checking if there are stones left and declaring
the winner.

Args:
self (Director): An instance of Director.
"""
self._output_service.clear_screen()

for i in self.bricks:
self._output_service.draw_actor(i)


self._output_service.draw_actor(self._score)
self._output_service.draw_actor(self._bat)
self._output_service.draw_actor(self._ball)
self._output_service.flush_buffer()
Loading