Assignment 7 - HodyahAdler/temp_oop_assigment GitHub Wiki

Snake Game Assignment

Introduction

In this assignment, you will create a Snake game using Python. The game will involve controlling a snake to eat food and grow while avoiding obstacles in the form of blocks. You will learn about object-oriented programming, classes, inheritance, and interfaces in python while implementing this game.

###Rules of the Game

  • The snake starts as a 3 square and moves in the direction specified by the player's input (up, down, left, or right).
  • The snake can wrap around the screen boundaries, meaning it reappears on the opposite side if it moves out of bounds.
  • The snake must eat the food (red square) to grow in length.
  • The game ends if the snake collides with any block or itself.
  • The objective is to achieve the highest possible score by eating as much food as possible without colliding with obstacles.

##Libraries You will need the following library to complete this assignment.

Pygame: Pygame is a cross-platform set of Python modules designed for writing video games. It includes computer graphics and sound libraries. You can install Pygame using pip:

pip install pygame


##Part 1: The Class


###A Rectangle Class In this game, we start by creating a base class called Rectangle. The Rectangle class will serve as the foundation for other objects in the game, such as blocks and food. It includes common attributes and methods for positioning and rendering rectangles on the screen.

The Rectangle class should have at least the following methods (you can add more, of course): class Rectangle: def __init__(self, x, y): # Initialize rectangle's attributes (e.g., position)

`def get_position(self):`
    `# Get the coordinates of the rectangle`

`def render(self):`
    `# Render the rectangle on the screen`

###A Block Class (Inherits from Rectangle) The Block class represents obstacles on the screen. Blocks can come in various configurations, including blocks with multiple contiguous rectangles and single rectangles. The Block class inherits from the Rectangle class to reuse its attributes and methods while adding block-specific behavior.

The Block class should have at least the following methods (you can add more, of course):

class Block(Rectangle): def __init__(self, position, length): # Initialize block's attributes, including position and length of the rectangle

`def get_rectangles(self):`
    `# Return a list of rectangles that make up the block`

A Food Class (Inherits from Rectangle) The Food class represents the food item that appears on the screen. Similar to blocks, the Food class inherits from the Rectangle class to reuse its common attributes and methods while adding food-specific behavior.

The Food class should have at least the following methods (you can add more, of course):

class Food(Rectangle): def __init__(self, screen_width, screen_height): # Initialize food's attributes and position

`def place_food(self, screen_width, screen_height):`
    `# Randomly place the food on the screen`

###A Snake Class The Snake class represents the snake character in the game. It includes attributes and methods to control the snake's movement, growth, and collision detection. The Snake class inherits from an interface called Movement to ensure it implements the necessary movement methods.

The Snake class should have at least the following methods (you can add more, of course):

class Snake(Movement): def __init__(self, x, y): # Initialize snake's attributes (e.g., position, direction)

`def move(self, wrap_around=True):`
    `# Move the snake in the current direction`

`def change_direction(self, key):`
    `# Change the snake's direction based on user input`

`def check_collision(self, screen_width, screen_height):`
    `# Check if the snake has collided with boundaries or itself`

`def get_head(self):`
    `# Get the coordinates of the snake's head`

`def grow(self):`
    `# Increase the snake's length when it eats food`

##Part 2: The Game Create your game to look like this: [Image]

Submission

Please submit your assignment with the following components:

Your Python code implementing the Snake game, organized according to the class structure explained. Any additional documentation or comments to explain your code.

Good luck!