
Better to get this right while things are still simple. But as soon as you add other game elements that need to animate at different speeds, you'll run up against this difficulty. The concepts frames per second and speed of the snake in moves per second are distinct, so it's good practice to separate them.Īt the moment there's nothing in the game other than the snake, so you get away with this. This design decision commits you to processing everything in the game at the same frequency as the snake moves. You use FPS (the number of frames per second) to control the speed of the snake. Alternatively, you might allow the player to use the arrow keys too (these are natural keys the player might try).
#Tron screen snake code#
I had to look at the source code to see that I need to use WASD for movement. The game could do with some instructions. I've made many comments below, but don't take the length of this answer to heart: there are always many things to say about a piece of code of this length. This is not bad overall, considering that this is your first program written with PyGame. If self.ticks % FPS_INCREMENT_FREQUENCY = 0: self.fps += 1 (self.screen, FOOD_COLOR, (blockWidth * (fx-1), blockHeight * (fy-1), blockWidth, blockHeight)) (self.screen, SNAKE_COLOR, (blockWidth * (px-1), blockHeight * (py-1), blockWidth, blockHeight)) # Draws snake and food objects to the screen If () or hx self.sizeX or hy > self.sizeY: # If snake collides with self or the screen boundaries, then game over # If snake hits a food block, then consume the food, add new food and grow the snake
#Tron screen snake update#
# Update gamestate - update snake and check for death While fx is None or fy is None or (fx, fy) in self.food:Įlif e.key = K_s: self.nextDirection = 2Įlif e.key = K_a: self.nextDirection = 3Įlif e.key = K_d: self.nextDirection = 4Įlif e.key = K_SPACE and not aying: # Adds a new piece of food to a random block Self.snake = Snake(WORLD_SIZE_X / 2, WORLD_SIZE_Y / 2, SNAKE_START_LENGTH) # Initializes SnakeGame object with pre-initialized objects and configuration settingsĭef _init_(self, window, screen, clock, font): If len(self.pieces) - len() > 1: return True # algorithm only checks if the position of the head piece contains more than one block. # So instead of checking if any of the spots have two pieces at once, the new # new food block could cause the snake to die if it's in a certain position. # Because of the way new pieces are added when the snake grows, eating a # Are two pieces of the snake occupying the same block? If self.direction = 1: piece = (tx, ty + 1)Įlif self.direction = 2: piece = (tx, ty - 1)Įlif self.direction = 3: piece = (tx + 1, ty)Įlif self.direction = 4: piece = (tx - 1, ty) # Adds a new piece to the end of the snake # Remove tail of the snake and add a new head If self.direction = 1: head = (headX, headY - 1)Įlif self.direction = 2: head = (headX, headY + 1)Įlif self.direction = 3: head = (headX - 1, headY)Įlif self.direction = 4: head = (headX + 1, headY) # Create new piece that is the new head of the snake # Updates snake by moving blocks in direction of movement If self.direction = 4 and direction = 3: return If self.direction = 3 and direction = 4: return If self.direction = 2 and direction = 1: return If self.direction = 1 and direction = 2: return # Moving in the opposite direction of current movement is not allowed # Resets snake back to its original state Game = SnakeGame(window, screen, clock, font) Can someone give me some advice on how I can refactor/make it better? I wrote a simple Python snake game which is about 250 lines of code.
