index
game c:\users\jaffar\softwareprojects\snake-hunt\game.py
Modules
comm pickle
Classes
builtins.object
BodyPart
Camera
Game
Pellet
Player
RandomPellets
Snake
class BodyPart(builtins.object)
BodyPart(position, xdir, ydir, color)
A part of a snake.
Attributes
----------
position (tuple[int, int]):
x and y positions, respectively
xdir (int):
Horizontal direction (-1 left, 0 still, 1 right)
ydir (int):
Vertical direction (-1 up, 0 still, 1 down)
color (tuple[int, int, int]):
Values for red, green, and blue (RGB), respectively
Methods
-------
set_direction(xdir, ydir)
move()
Methods defined here:
__init__(self, position, xdir, ydir, color)
Create body part.
move(self)
Move the part based on speed and direction.
Return
------
None
set_direction(self, xdir, ydir)
Set the horizontal and veritcal direction
Parameters
----------
xdir (int):
The horizontal direction (see BodyPart's documentation for details)
ydir (int):
The vertical direction (see BodyPart's documentation for details)
Return
------
None
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
Data and other attributes defined here:
width = 10
class Camera(builtins.object)
Camera(width, height)
A class representing a camera.
This camera keeps its target in the middle.
Attributes
----------
dimensions (tuple[int, int]):
Width and height of camera
Methods
-------
within_bounds(object_pos, target_pos)
Methods defined here:
__init__(self, width, height)
Create a camera object.
within_bounds(self, object_pos, target_pos)
Check if an object is within the camera's sight
Parameters
----------
object_pos (tuple[int, int]):
Position of object
target_pos (tuple[int, int]):
Position of camera's target
Return
------
True if object_pos is within camera's sight, False otherwise
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
class Game(builtins.object)
Game(server)
Game class
Attributes
----------
server (Server):
Game server
players (list):
List of current players
camera (Camera):
Camera object
random_pellets (RandomPellets):
Generates the game's pellets
running (Boolean):
Whether or not the game is running
bounds (object):
Left, right, up and down bounds of the playing field
Methods
-------
add_player(player)
remove_player(player)
get_leaderboard()
get_visible_snakes(receiver_player, camera_target)
get_visible_pellets(camera_target)
get_random_position()
game_loop()
Methods defined here:
__init__(self, server)
Initialize game.
add_player(self, player)
Add player to list of players
Parameters
----------
player (Player):
Player to add
Return
------
None
game_loop(self)
The game loop
Return
------
None
get_leaderboard(self)
Retreive the current state of the leaderboard.
Return
------
List containing the names and lengths of the top 10 largest snakes
get_random_position(self)
Get a random position within playing field that does not collide with any snake.
Return
------
A tuple[int, int] containing the position
get_visible_pellets(self, camera_target)
Get the pellets that are visible in camera.
Parameters
----------
camera_target (tuple[int, int]):
Position of the camera's target, basis for what is and isn't visible
Return
------
List of pellets
get_visible_snakes(self, receiver_player, camera_target)
Get the parts of the snakes that are visible in camera.
Parameters
----------
receiver_player (Player):
The receiver of the return value of this function
camera_target (tuple[int, int]):
Position of the camera's target, basis for what is and isn't visible
Return
------
List of snakes
remove_player(self, player)
Remove player from list of players and close their socket.
Parameters
----------
player (Player):
Player to remove
Return
------
None
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
class Pellet(builtins.object)
Pellet(color_val, is_remains=False)
A class representing a consumable food object.
Attributes
----------
position (tuple[int, int]):
Position of the pellet
color (tuple[int, int, int]):
Color of the pellet
val (int):
Number of body parts that a snake gets by consuming this pellet
is_remains (Boolean):
Whether this pellet is the remains of a dead snake
width (int):
Width of pellet
height (int):
Height of pellet
Methods
-------
setRandomPos()
getPos()
setPos()
Methods defined here:
__init__(self, color_val, is_remains=False)
Create pellet object.
getPos(self)
Get the pellet's position.
Return
------
A tuple [int, int] current position
setPos(self, xpos, ypos)
Set the pellet's position.
Parameters
----------
xpos (int):
x position
ypos (int):
y position
Return
------
None
setRandomPos(self)
Give the pellet a random position.
Return
------
A tuple [int, int] representing the random position
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
class Player(builtins.object)
Player(id, snake, socket)
A connected player.
Attributes
----------
id (int):
A unique identifyer
snake (Snake):
The player's snake
socket (socket.socket):
Player's connection
name (str):
Keeps track of the current name entered by user
Methods defined here:
__init__(self, id, snake, socket)
Create player.
set_name(self, name)
Set the player's name
Parameters
----------
name (str):
The name to set
Return
------
None
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
class RandomPellets(builtins.object)
RandomPellets(numPellets)
A class that creates multiple pellets at random non-overlapping positions.
This class maintains the pellets by restoring them at new positions when consumed.
IMPORTANT NOTE
For a 32 bit system, the maximum array size in python is 536,870,912
elements. Since this implementation is dependent on the board and cell size,
this will not work for anything larger than a 23170 by 23170 size board/cell
ratio for 32 bit systems.
Attributes
----------
numPellets (int):
Number of pellets to generate
availablePositions (list):
List of available positions
pellets (list):
List of pellets
Methods
-------
setColor()
genPellets()
setPositions()
getPositions()
resetPellet(pel)
addPellets(pellets)
Methods defined here:
__init__(self, numPellets)
Create RandomPellets object.
addPellets(self, pellets)
Join the list of pellets with another list of pellets.
Parameters
----------
pellets (list):
List of pellets to join
Return
------
None
genPellets(self)
Generate pellets at random positions.
Return
------
List of pellets generated
getPositions(self)
Get a list of the positions of all pellets.
Return
------
List
resetPellet(self, pel)
Remove a pellet then generate a new one at a random position.
Parameters
----------
pel (Pellet):
The pellet to remove
Return
------
None
setColor(self)
Give the pellet a random color and value.
Return
------
A tuple containing the color and the value
setPositions(self)
Initialize all possible pellet positions
Return
------
List of all possible positions
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
Data and other attributes defined here:
val_1 = ((150, 255, 150), 1)
val_2 = ((150, 150, 255), 2)
val_3 = ((255, 150, 150), 3)
class Snake(builtins.object)
Snake(position, length, xdir, ydir, bounds)
A class representing a snake game object.
Attributes
----------
bounds (tuple[int, int, int, int]):
Borders of the playable area. Left, right, up and down respectively
color (tuple[int, int, int]):
Initial color of the snake in RGB
body (list):
List of BodyPart objects representing the snake's body parts
turns (dict):
Dictionary containing positions in which the head has turned. Used to turn remaining body parts
length (int):
Initial length of the snake
Methods
-------
initialize(position, xdir, ydir)
reset(position)
change_direction(direction)
move()
grow(amount, color)
collides_self()
collides_other(other_snakes)
collides_position(position)
cook()
get_visible_bodyparts(camera, camera_target)
Methods defined here:
__init__(self, position, length, xdir, ydir, bounds)
Create snake.
change_direction(self, direction)
Change snake's head's direction.
Parameters
----------
direction (tuple[int, int]):
New direction or None if direction hasn't changed.
Return
------
None
collides_other(self, other_snakes)
Check if snake collided with another snake.
Parameters
----------
other_snakes (list):
List of all other snakes in the game.
Return
------
True if the snake has collided with another snake, False otherwise.
collides_position(self, position)
Check if the snake collides with the given position.
Parameters
----------
position (tuple[int, int]):
A position.
Return
------
True if the snake collides with the given position, False otherwise.
collides_self(self)
Check if snake collided with its own body.
Return
------
True if the snake has collided with its own body, false otherwise.
cook(self)
Turn the snake's body into consumable food pellets.
Return
------
A list containing Pellet objects.
get_visible_bodyparts(self, camera, camera_target)
Get the body parts of this snake that are visible by a given camera.
Parameters
----------
camera (Camera):
The camera for which to check visibility of snake
camera_target (tuple[int, int]):
Position of the camera's target
Return
------
A list of body parts of this snake that are within the camera's lens
grow(self, amount, color)
Add more BodyPart objects to the snake
Parameters
----------
amount (int):
Number of parts to add
color (tuple[int, int, int]):
Color of the parts to add.
Return
------
None
initialize(self, position, xdir, ydir)
Create all of the snake's body parts.
Parameters
----------
position (tuple[int, int]):
The position of the snake's head
xdir (int):
Horizontal direction (see BodyPart's documentation for details)
ydir (int):
Vertical direction (see BodyPart's documentation for details)
Return
------
None
is_invincible(self)
Check if the snake cannot die.
Return
------
True if the snake cannot die, False otherwise.
move(self)
Move each body part of the snake.
Head moves based on the direction, and so do other parts.
Other parts are affected by the head's movement. When a part hits a position
that the head has turned at, it mimics the head's turn.
Return
------
None
reset(self, position)
Restore snake to initial length and set its new position.
Parameters
----------
position (tuple[int, int]):
New position
Return
------
None
Data descriptors defined here:
__dict__
dictionary for instance variables (if defined)
__weakref__
list of weak references to the object (if defined)
Data and other attributes defined here:
INITIAL_LENGTH = 1
MAX_INVINCIBLE_LENGTH = 3
Functions
Clock(...)
Clock() -> Clock
create an object to help track time
flr = floor(x, /)
Return the floor of x as an Integral.
This is the largest integer <= x.
Data
BOARD = (1000, 1000)
CELL = 10
COLS = 100.0
MAX_NAME_LENGTH = 32
ROWS = 100.0
SHUT_RDWR = 2
SPEED = 10