Python Forum
[PyGame] Snake controls not working
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] Snake controls not working
#3
There is a lot of extra code here that is not needed. First of all
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 changeTo == 'RIGHT' and not direction == 'LEFT':
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.

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] += 10
Please 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:
snakePos  = [100, 50]
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]
Recommended Tutorials:
Reply


Messages In This Thread
Snake controls not working - by jakegold98 - Dec-11-2017, 06:00 PM
RE: Snake controls not working - by Windspar - Dec-11-2017, 08:50 PM
RE: Snake controls not working - by metulburr - Dec-11-2017, 09:02 PM
RE: Snake controls not working - by jakegold98 - Dec-11-2017, 10:17 PM
RE: Snake controls not working - by metulburr - Dec-11-2017, 10:26 PM
RE: Snake controls not working - by Windspar - Dec-12-2017, 01:45 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  how to add segments to the snake body blacklight 1 2,902 Sep-13-2023, 07:33 AM
Last Post: angelabarrios
  [PyGame] Snake game: how to get an instance for my snake's body multiple times? hajebazil 2 2,162 Jan-30-2022, 04:58 AM
Last Post: hajebazil
  help with snake game blacklight 3 2,624 Jul-30-2020, 01:13 AM
Last Post: nilamo
  [PyGame] Terrible Sprite controls, need help. michael1789 16 5,444 Dec-18-2019, 10:32 PM
Last Post: michael1789
  Snake Game - obstacle problem Samira 3 5,654 Oct-31-2019, 02:58 PM
Last Post: Samira
  [PyGame] Made my first Python program: Snake. Please help me improve it andrerocha1998 7 6,202 Feb-19-2019, 07:08 PM
Last Post: Windspar
  Creating Snake game in Turtle Shadower 1 8,660 Feb-11-2019, 07:00 PM
Last Post: woooee
  [PyGame] Basic Snake game (using OOP) PyAlex 1 12,571 Sep-10-2018, 09:02 PM
Last Post: Mekire
  [PyGame] Snake not changing directions in Snake Game Bjdamaster 4 4,956 Aug-13-2018, 05:09 AM
Last Post: Bjdamaster

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020