Dec-11-2017, 09:02 PM
There is a lot of extra code here that is not needed. First of all
https://www.pygame.org/docs/ref/key.html
You can substitute that for pygame.K_w for 'W' and so on.
You can also just use the in operator and do
So is this
you can remove all that and just do
A rule of thumb is you should only every have to type this once EVER in your game
A proper structure would be in the tutorials in how to organize the code.
You also shouldnt be doing this anyways
Quote:if event.key == pygame.K_RIGHT or event.key == ord('d'):pygame has a keys for every possible keyboard input, including WASD movements. Im not sure why you chose to do ord()?
https://www.pygame.org/docs/ref/key.html
You can substitute that for pygame.K_w for 'W' and so on.
You can also just use the in operator and do
if event.key in [pygame.K_RIGHT, pygame.K_d]:
Quote:If you input the right arrow key than you wouldnt be inputting the left one. This is just redundant code that doesnt need to be there.if changeTo == 'RIGHT' and not direction == 'LEFT':
So is this
Quote:if direction == 'RIGHT':You are already checking the event.key for pygame.RIGHT....why do it again?
you can remove all that and just do
if event.key in [pygame.K_RIGHT, pygame.K_d]: snakePos[0] += 10Please save yourself the headache and dont do this
Quote:#Game Over Function def gameOver(): myFont = pygame.font.SysFont('monaco', 72) GOsurf = myFont.render('You Suck', True, red) GOrect = GOsurf.get_rect() GOrect.midtop = ((360, 15)) playSurface.blit(GOsurf, GOrect) pygame.display.update() time.sleep(5) pygame.QUIT() #pygame exit sys.exit() #console exit
A rule of thumb is you should only every have to type this once EVER in your game
pygame.display.update()If you do it more then you are creating spaghetti code. The proper way would be to use a state machine. Which would be a game over state to handle what happens if a player died etc.
A proper structure would be in the tutorials in how to organize the code.
You also shouldnt be doing this anyways
Quote:why bother when pygame has pygame.Rect whihc handles position and collision which you will need anyways. Its also more simple to read player.rect.x rather than snakePos[0]snakePos = [100, 50]
Recommended Tutorials: