""" All necessary information can be gained by simply reading, as I was very attentive to details, but I also forced you to try and understand this for yourself to some extent. This program will be a extremely simple game where you can move around and try not to hit the border, or else you trigger a "game over". It wouldn't take much more code to add an obstacle that causes a game over, so why don't you try adding that feature to this program after gaining a clear understanding of it? You can use a Number Sign/Hashtag/# to create a single line comment in Python, or three immediately sequential parenthesis in order to create a multi-line comment. Comments are used to "comment" on information that you want any potential editors to know about to make their jobs easier, and don't effect what the program does at all if they are written properly. Comments have widely renowned typing methods such as leaving multiline comments' begginings and endings on the immediate beginnings and endings of your comment, but I would recommend typing them the way I type them as to read them easier. A Good Rule of Thumb for making Comments is to make comments on everything you think you will forget within a year, as you may forget it within a month. """ import pygame #This is needed to use the commands from the Pygame Module. import time #This is needed to use commands from the Time Module within Pygame. pygame.init() #This is needed to initialize Pygame. """ white, black, red, green, blue, display_width, display_height, are all variables within this program, variables are sensitive to capitalization so pay attention to how you type them, I would recommend limiting yourself to only using only letters and underscores for variable names as to remember variable names more easily, and also recommend to be descriptive and short when creating variable names. The colors listed below will be used with Pygame for Snake. The values within them are contained within a Tuple, which is effectively a list of values that can't be changed by other code. A List, formed by [] with the values within the two brackets, could also be used, but it could also be changed by using commands in the code. The colors are integrated with the color LED's in your computer which are Red, Green, and Blue. White is (255,255,255) representing the maximum values within each parameter. Coincidently, they are ordered in (red, green, blue), as evidenced by the way red, green, and blue are parametrized. """ white = (255,255,255) black = (0,0,0) red = (255,0,0) green = (0,255,0) blue = (0,0,255) display_width = 800 #This is the variable that will be used for setting the display width. display_height = 600 #This is the variable that will be used for setting the display height. gameDisplay = pygame.display.set_mode((display_width, display_height)) """ This is the first example of implementation of variables in the code, and I will have to explain all of this code. """ pygame.display.set_caption('Slither') #This sets a caption to Pygame's Display. clock = pygame.time.Clock() #This is a variable that is used to control time within Pygame. block_size = 10 #This is the variable used to set the block size to 10. FPS = 30 #This is the variable used to set the Frames Per Second to 30. font = pygame.font.SysFont(None, 25) #This sets Pygame's Font to 25, you can ignore the None for now, but you will need to include it to replicate the results. """ |The code below starting with a "def" for define is a function, a function is effectively a variable that when called makes the code below it run, and unless there is a "break" in the code |causing the program to stop, then the code beneath where the program was called will continue running, a infinite loop can result from this. Functions can take input from the code, |as in (msg,color), msg is one input and color is another, you need to input values for both of those or else an error will occur. * """ def message_to_screen(msg,color): #This function is used to create a message to the screen. screen_text = font.render(msg, True, color) #Keep in mind that "whitespace" is very important. If you don't have a certain amount of space below a statement such as a if or while statement, your program will have a error. #As a guideline, if the code ends in a ":", you should have 1 tab worth of whitespace in front of all statements incorporated in it. Some Development Environments do this automatically. gameDisplay.blit(screen_text, [display_width/2, display_height/2]) #Dividing display_width and display_height in this manner will cause the text to start displaying on the middle of the screen through it's coordinates. def gameLoop(): #This is another function, you need to add parentheses, even if you aren't taking any input. It is the loop in which the bulk of the game operates. gameExit = False #This is a variable used to keep this loop running. gameOver = False #This is a variable used to give a "Game Over" when it is changed to true, although you can simply restart. lead_x = display_width/2 #This variable will be used to set the block's location as well, this variable can and will change. / means divide in python. lead_y = display_height/2 #This variable will be used to set the block's location as well, this variable can and will change. / means divide in python. lead_x_change = 0 lead_y_change = 0 while not gameExit: #This is a while loop, google what that means, even if it is intuitive. Make sure to include Python as a word in your search. This is where gameExit = False comes into play, "while not" makes False statements into True Statements and vice versa. Simply putting True will ensure your loop runs. while gameOver == True: #This while loop runs when the variable gameOver is set to True, == is a operator that tests whether something is true or not. """ Since we are on the topic of operators, I might as well explain the other operators now. Keep in mind that Operators can change among different programming languages. / is the division operator, it was what was used in lead_x and lead_y. * is the multiplication operator. + is addition operator. - is the subtraction operator. % is the modulo operator, it returns what remains of a number after you have already dividided it, so 4%1 is 0 and 4%3 is 1, since 3 goes into 4 exactly one time. == is used to test whether one value is another value, it is === in JavaScript. > is used to test whether one value is more than another value, effectively a more than or less than sign just like the ones in Algebra. < is used to test whether one value is less than another value, again, effectively a more than or less than sign just like the ones in Algebra. <= is used to test whether one value is less than or equal to another value. >= is used to test whether one value is more than or equal to another value. != is used to test whether one value is not equal to another value. """ gameDisplay.fill(white) #This causes the Game's Display to be filled with the color white, you notice how the variable white is used? message_to_screen("Game Over, press C to play again or Q to quit", red) #This causes the message to the screen to be what it is, through implementing variables. pygame.display.update() #This causes the display to be updated. for event in pygame.event.get(): #This is a "for in" loop, you should google what this means in python. if event.type == pygame.KEYDOWN: #This limits what iterates through the for loop, as to save processing power. Processing Power is very important, especially in Python. if event.key == pygame.K_q: #This runs if the q key is pressed. gameExit = True gameOver = False if event.key == pygame.K_c: #This runs if the c key is pressed. gameLoop() #This causes the function, gameLoop() to run again, causing the game to start over. for event in pygame.event.get(): #This is the function controlling the cube's movement. if event.type == pygame.QUIT: #This triggers if pygame.QUIT is activated. gameExit = True #This sets the variable, gameExit, to True. if event.type == pygame.KEYDOWN:#This triggers if a key is pressed down. if event.key == pygame.K_LEFT: #This triggers if the left arrow key is pressed down. lead_x_change = -block_size #This causes the x value to be continuesly decreased by the value of the block_size variable. lead_y_change = 0 #This causes the y value to stop being effected after being triggered. elif event.key == pygame.K_RIGHT:#This triggers if the right arrow key is pressed down. lead_x_change = block_size #This causes the x value to be continuesly increased by the value of the block_size variable. lead_y_change = 0 #This causes the y value to stop being effected after being triggered. elif event.key == pygame.K_UP: #This triggers if the up arrow key is pressed down. lead_y_change = -block_size #This causes the y value to be continuesly decreased by the value of the block_size variable. lead_x_change = 0 #This causes the x value to stop being effected after being triggered. elif event.key == pygame.K_DOWN: #This triggers if the down arrow key is pressed down. lead_y_change = block_size #This causes the y value to increase by the value of the block_size. lead_x_change = 0 #This causes the x value to stop being effected after being triggered. if lead_x >= display_width or lead_x < 0 or lead_y >= display_height or lead_y < 0: #This creates the border for the cube, otherwise the cube would just keep on moving out of view and there would be no game over. gameOver = True #This sets the gameOver variable to True, which causes you to receive a "Game Over" if event.type == pygame.KEYUP: #This triggers if any key is released. if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT: #This triggers if the event key is the left key or the right key. lead_y_change = 0 #This sets the lead_y_change variable to 0. lead_x += lead_x_change #This adds lead_x_change to lead_x. lead_y += lead_y_change #This adds lead_y_change to lead_y. gameDisplay.fill(white)#This causes the pygame.draw.rect(gameDisplay, black, [lead_x,lead_y,block_size,block_size]) pygame.display.update() clock.tick(FPS) pygame.quit()#This causes Pygame to close. quit()#This causes Python to close. #If you understood all of this, you should be able to breeze through a Python Tutorial and then get started on a certain 100 Video series Youtube Pygame Tutorial. You should already know how to Google by now. #Then you should be able easily make this an educational game.
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question