Question
Make a snake game in PYTHON 3 without using pygame and using the provided code as the only graphics module and using time module as
Make a snake game in PYTHON 3 without using pygame and using the provided code as the only graphics module and using time module as the only other module.
Provide Algorithm and code.
python_graphics.py
try: import tkinter as tk except ImportError: import Tkinter as tk
class Rect(object): def __init__(self, x1, y1, x2, y2, color): self.x1 = x1 self.x2 = x2 self.y1 = y1 self.y2 = y2 self.color = color
class Text(object): def __init__(self, x1, y1, text, color, font): self.x1 = x1 self.y1 = y1 self.text = text self.color = color self.font = font
class Window(object): def __init__(self, width, height, title = "Python Graphics", background="Black"): self.__root = tk.Tk() self.__root.resizable(width=False, height=False) self.__root.geometry('{}x{}'.format(width, height)) self.__root.bind("
def __get_key(self, event): """ Records any key events that occur """ self.__last_key.append(event.keysym)
def get_last_key(self): """ Returns the last key pressed. None if no keys have been pressed since the last call. """ if len(self.__last_key) > 0: return self.__last_key.pop(0) else: return None
def draw_screen(self): """ Draws any objects you've sent to the screen """ self.__win.delete(tk.ALL)
for item in self.__objects: if isinstance(item, Rect): self.__win.create_rectangle(item.x1, item.y1, item.x2, item.y2, fill=item.color) elif isinstance(item, Text): self.__win.create_text(item.x1, item.y1, text=item.text, fill=item.color, font=item.font)
self.__win.update() self.__objects.clear()
def draw_rect(self, x1, y1, x2, y2, fill): """ Draws a square at the given location """ self.__objects.append(Rect(x1, y1, x2, y2, fill))
def draw_text(self, x1, y1, text, color, font=("Consolas", 8)): """ Draws text onto the screen """ self.__objects.append(Text(x1, y1, text, color, font))
Specification and hints
The program shows the beginning screen with some instructions.
Once the player hits, left, right, up or down the game starts with the snake in the middle of the screen moving in the direction the user choose. A food pellet is randomly placed on the screen.
If the player runs over the food, he gets one space longer, and a new food pellet appears somewhere the snake isnt.
The player doesnt have to keep the key held down. If he hits down the snake goes down until the player presses another key and then it switches directions.
If the player runs into a wall or himself the game is over and hell be given the game over screen with his score and directions to play again.
Colors can be most of the primary colors, green, blue, black, white, red, etc.
The snake can probably be represented with a list. The head coordinates being at index 0, and the tail being the last index available. Youll want to play around with how to keep track of the head and the tail. When the snake runs over food his tail doesnt move that round and his head moves onto the food making him one square larger. ( again you may want to play around with this idea. )
Break things down into proper functions. The game will be a tile based game. If the screen is 100,100 and you want the tileboard to be 10x10, then tile 0, 0 would be a rectangle from pixels at (0, 0) to (9, 9). Tile at position 1, 2 would be a rectangle from pixels (10, 20) to (19, 29). You may want to write a function to make this easier.
You probably want to start with showing a single square on the screen and then have it move in a direction of your choosing, until you press a different direction. Make sure you play around with drawing things and moving them around on the screen. Dont try to write it all at once.
There are 2 different game states. One is while you are playing, and there will be a loop that gets the user input and changes directions if needed. Update the snake on the screen, check to see if it ran over food, or hit itself or a wall and end the game etc. The other state is the title screen.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started