maxCells += 1 # canvas is 400x400 which is 25x25 grids apple. # drawing 1 px smaller than the grid creates a grid effect in the snake body so you can see how long it is pygame. # draw snake one cell at a time index = 0 for cell in snake. # remove cells as we move away from them if len( snake. front of the array is always the head snake. y = 0 # keep track of where snake has been. # wrap snake position vertically on edge of screen if snake. # wrap snake position horizontally on edge of screen if snake. QUIT:ĭone = True # slow game updates to 15 per second instead of 60 (60/15 = 4) count += 1 if count >= 4: # prevent snake from backtracking on itself by checking that it's # not already moving on the same axis (pressing left while moving # left won't do anything, and pressing right while moving left # shouldn't let you collide with your own body) if event. # listen to keyboard events to move the snake if event. grows when eating an apple maxCells = 4 class Apple: moves one grid length every frame in either the x or y direction dx = grid dy = 0 # keep track of all grids the snake body occupies cells = ![]() # allow us to close the game done = False count = 0 grid = 16 class Snake: # set the width and height of the screen size = ( 400, 400) Import pygame import random # define some colors BLACK = ( 0, 0, 0) unshift ( // check collision with all cells after this one (modified bubble sort) for ( var i = index + 1 i wrap snake position vertically on edge of screen slow game loop to 15 fps instead of 60 (60/15 = 4) get random whole numbers in a specific range keep track of all grids the snake body occupies moves one grid length every frame in either the x or y direction the canvas width & height, snake x & y, and the apple x & y, all need to be a multiples of the grid size in order for collision detection to work When you become a Patron, you get access to behind the scenes development logs, the ability to vote on which games I work on next, and early access to the next Basic HTML Game. Other Basic Gamesīasic HTML Games are made possible by users like you. Giving attribution is not required, but appreciated. There's no need to ask permission before using these. (CC0 1.0 Universal) You're free to use this game and code in any project, personal or commercial. This series is meant to give a basic outline of the game but nothing more. Important note: I will answer questions about the code but will not add more features or answer questions about adding more features. Improve it so it only spawns in empty grid locations ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |