So I've been working on a few games in Python (battleships, tic-tac-toe etc.) and this week's project is Snake. I've got a basic set-up going; the snake can move and eats the food but I haven't programmed in collision detection or going off the edge yet. The problem is response time. If you run the code below, you'll see that the snake responds to key presses, but not for a couple of - I'll call them frames - after the press. I don't quite understand how the listen method works; am I using it properly? If not, how should I use it, and if so, how can I fix the delay?
Create fantastic Games and learn about Python GUI using the Tkinter Module! Create fantastic Games and learn about Python GUI using the Tkinter Module! Search for anything Udemy for Business Get your team access to Udemy’s top 3,000 courses anytime, anywhere. Python GUI and Gaming 101 with Tkinter. Prerequisite: Python GUI Tkinter TKinter is widely used for developing GUI applications.Along with applications, we can also use Tkinter GUI to develop games. Let’s try to make a game using Tkinter. In this game player has to enter color of the word that appears on the screen and hence the score increases by one, the total time to play this game is 30 seconds.
Google Snake
Project: Snake Game in Python with source code – To download Snake Game project for free (scroll down) About Project. Snake Game project is written in Python. The project file contains Assets, python scripts (main.py, food.py, block.py, world.py) with text files.
I know about Pygame, but a) I can't find an easy to install 64 bit version for python 3.4 (this one is not easy to install, what the heck is a whl file?) and b) I want to challenge myself anyway.Any help would be appreciated. Whenever you use while True: (sans break) in turtle code, you're defeating the event hander.
You should instead use an ontimer event to run your code compatibly with the event handler. It probably is a challenge to make a speedy game this way (but that's not all bad). I think that listen should go after the onkeys.You should also look at getting rid of all the duplicated code. It might seem easy short term to just copy/paste then alter. But if you have to make major changes (like after asking on a forum) it will be tedious and error prone to alter.PS (EDIT) also your Snake.moveOneStep method makes a new instance of Square just to check for self collision, this seems extravagant for the sake of elegance. Better to just keep a list of locations that python (ho, ho) can check through.
Snake Game Using Tkinter Python
(Quite apart from this probably not working. Try print(Square(1,2) in Square(1,2))) def checkselfcollision(self, x, y):for s in self.body:if s.x x and s.y y:return Falsereturn Truedef moveOneStep(self):if self.checkselfcollision(self.nextposition0, self.nextposition1):# attempt (unsuccessful) at collision detectionself.body.append(Square(self.nextposition0, self.nextposition1)).