There is one GUI window in which there are a snake and the food at a random position , we can move snake through arrow keys.
Programming Language used Python.
We have used some of the following modules, functions, and widgets which are different from what we use regularly in Python language.
The project is based on the pygame module.
1 .init() - pygame.init() initialize all imported pygame modules and is a convenient way to get everything started.
2 .display.set_mode() - Setting the display mode in pygame creates a visible image surface on the monitor, or in simpler ways, it is used to create the window which will open when you run the game.
screen_width = 900 screen_height = 600 gameWindow = pygame.display.set_mode((screen_width, screen_height))
3 display.update() - updates the entire Surface, only if no argument is passed. It is like after you write code or make any changes then you should use this module to govern those changes.
4 .time.Clock() - Creates a new Clock object that can be used to track an amount of time. The clock also provides several functions to help control a game's frame rate. (frame rate- it is the frequency at which consecutive images called frames appear on a display)
The screen object represents your game screen. It is a thin wrapper around a Pygame surface that allows you to easily draw images to the screen (“blit” them). ... blit() accepts either a Surface or a string as its image parameter. If the image is str then the named image will be loaded from the images/ directory.
def text_screen(text, color, x, y): screen_text = font.render(text, True, color) gameWindow.blit(screen_text, [x,y])
6 mixer.music.load(): This will load a music filename/file object and prepare it for playback. If a music stream is already playing it will be stopped. This does not start the music playing.
7 python.event.get(): Since the game state is usually updated in response to events (such as mouse clicks or keyboard presses) or the passage of
time, the game loop is constantly checking and re-checking many times a second for any new events that have happened. Inside the main loop is code that looks at which events have been created (with Pygame, this is done by calling the pygame.event.get() function). The main loop also has code
that updates the game state based on which events have been created. This is usually called event handling.
The abs() function is used to return the absolute value of a number. ... The argument can be an integer, a floating-point number, or a complex number. If the argument is an integer or floating-point number, abs() returns the absolute value in integer or float.
if abs(snake_x - food_x)<12 and abs(snake_y - food_y)<12: food_sound=mixer.Sound(r'C:\Users\hp\Desktop\kunj\laser.wav') # this is the directrory of the laser file when snake eats the food. fps+=4 food_sound.play() score +=1 food_x = random.randint(20, screen_width / 2) food_y = random.randint(20, screen_height / 2) snk_length +=5
Submitted by Kunjesh Mahajan (kunmva)
Download packets of source code on Coders Packet