In this article, we will see how we can design the simple snake game using PyQt5.
Snake is the common name for a video game concept where the player maneuvers a line which grows in length, with the line itself being a primary obstacle. The concept originated in the 1976 arcade game Blockade, and the ease of implementing Snake has led to hundreds of versions (some of which have the word snake or worm in the title) for many platforms.
Implementation steps :
1. Create a main window add status bar to it, to show the score and create an object of board class and add it as central widget
2. Create a class named board which inherits the QFrame
3. Inside the board class create a timer object which calls the timer method after certain amount of time
4. Inside the timer method call other action of the snake game like movement, food eaten and if snake committed suicide
5. Create a key press event method that check if arrow keys are pressed and change the direction of the snake according to it.
6. Create a paint event method that draws snake and the food
7. Create move method to move the snake according to the direction
8. Create food eaten method that checks the snake current position and position if food is eaten remove the current food increment the snake length and drop a new food at random location.
9. Create check suicide method that checks if snakehead position is similar to the body position or not, if matches stop the timer and show the message
Below is the implementation
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python program to convert camel case string to snake case
- Python - Convert Snake case to Pascal case
- Python - Convert Snake Case String to Camel Case
- PyQt5 - The Color Game
- PyQt5 - Number Guessing Game
- PyQt5 – Jumble Word Game
- PyQt5 - Rock Paper and Scissor Game
- Tic Tac Toe Game using PyQt5 in Python
- Hangman Game in Python
- Conway's Game Of Life (Python Implementation)
- Python implementation of automatic Tic Tac Toe game using random number
- Color game using Tkinter in Python
- Hangman Game in Python
- Python | Program to implement Jumbled word game
- Python | Catching the ball game
- Python | Program to implement simple FLAMES game
- Python | Simple FLAMES game using Tkinter
- 21 Number game in Python
- Python program for word guessing game
- Python | Pokémon Training Game
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.