Not allowing snake to move backwards-is there a better way

Not allowing snake to move backwards-is there a better way

Postby dboxall123 » Fri Sep 27, 2013 11:30 pm

Ok, sorry for the poor question, but I couldn't think of a way to summarize this into one sentence. So I'm making a version of Snake, and I need to make sure that my snake can never move backward, otherwise his head touches his body and it's game over. So one way (the only way) I've thought of is to set these variables inside my snake class:
Code: Select all
self.moving_right = True
self.moving_left = False
self.moving_up = False
self.moving_down = False
This way, I can do something like this:
Code: Select all
if key[pygame.K_UP] and self.moving_down == False:
    self.x_change = 0
    self.y_change = 15
This is all OK, but then I have to follow it with this:
Code: Select all
#inside the if statement
    self.moving_right = False
    self.moving_left = False
    self.moving_up = True
    self.moving_down = False
This really doesn't seem like the best way of doing things, does anyone know of a better way?
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Not allowing snake to move backwards-is there a better w

Postby DrakeMagi » Sat Sep 28, 2013 1:06 pm

assuming that the snake is always moving.
here an option
Code: Select all
class myclass(object):
   def __init__(self):
      self.moving = None
      # other variables
      
   def event(self,event):
      
      if event.type == pygame.KEYDOWN:
         if self.moving not in [pygame.K_UP, pygame.K_DOWN]:
            self.moving = event.key
         elif self.moving not in [pygame.K_LEFT, pygame.K_RIGHT]:
            self.moving = event.key

   def update(self):
      
      if self.moving == pygame.K_UP:
         self.x_change = 0
         self.y_change = 15


# edit fix
Last edited by DrakeMagi on Sun Sep 29, 2013 1:49 pm, edited 4 times in total.
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: Not allowing snake to move backwards-is there a better w

Postby dboxall123 » Sun Sep 29, 2013 12:00 pm

Thanks DrakeMagi. I've ended up using my original version, but have managed to condense it down quite a bit:
Code: Select all
def key_check(self):
        self.key = pg.key.get_pressed()
        if self.key[pg.K_UP] and not self.moving_down:
            self.x_change,self.y_change = 0,-18
            self.moving_right,self.moving_left = False,False
            self.moving_up = True
        elif self.key[pg.K_DOWN] and not self.moving_up:
            self.x_change,self.y_change = 0,18
            self.moving_right,self.moving_left = False,False
            self.moving_down = True
        elif self.key[pg.K_RIGHT] and not self.moving_left:
            self.x_change,self.y_change = 18,0
            self.moving_up,self.moving_down = False,False
            self.moving_right = True
        elif self.key[pg.K_LEFT] and not self.moving_right:
            self.x_change,self.y_change = -18,0
            self.moving_up, self.moving_down = False,False
            self.moving_left = True
I'm jsut about to give your way a go. Cheers mate :D
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm

Re: Not allowing snake to move backwards-is there a better w

Postby DrakeMagi » Sun Sep 29, 2013 1:45 pm

I fix my code above had to many errors.
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: Not allowing snake to move backwards-is there a better w

Postby DrakeMagi » Sun Sep 29, 2013 8:23 pm

here a fix code example
Code: Select all
import pygame
import random

pygame.init()

class PygameString(object):
   def __init__(self, pystr, position, font=pygame.font.Font(None, 30)):
      self.font = font
      self.position = position
      self.color = (0,100,200)
      self.set_text(pystr)
      
   def set_text(self, pystr):
      self.text = self.font.render(pystr, 1, self.color)
      
   def blit(self, surface):
      surface.blit(self.text, self.position)

class mysnake(object):
   def __init__(self):
      self.moving = random.choice([pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT])
      self.old_movement = None
      # other variables
      
   def event(self,event):
      
      if event.type == pygame.KEYDOWN:
         if self.moving in [pygame.K_UP, pygame.K_DOWN]:
            if event.key in [pygame.K_LEFT, pygame.K_RIGHT]:
               self.moving = event.key
         elif self.moving in [pygame.K_LEFT, pygame.K_RIGHT]:
            if event.key in [pygame.K_UP, pygame.K_DOWN]:
               self.moving = event.key

   def update(self):
      
      if self.moving == pygame.K_UP:
         self.x_change = 0
         self.y_change = 15
         
   def string_update(self, string):
      if self.moving != self.old_movement:
         if self.moving == pygame.K_UP:
            string.set_text('UP')
         elif self.moving == pygame.K_DOWN:
            string.set_text('DOWN')
         elif self.moving == pygame.K_LEFT:
            string.set_text('LEFT')
         elif self.moving == pygame.K_RIGHT:
            string.set_text('RIGHT')
         self.old_movement = self.moving


def main():
   pygame.display.set_caption('Pygame Snake Movement')
   screen = pygame.display.set_mode((800,600))
   clock = pygame.time.Clock()
   
   snake = mysnake()
   text = PygameString("None",(30,30))
   
   snake.string_update(text)
   
   running = True
   while running:
      for event in pygame.event.get():
         if event.type == pygame.QUIT:
            running = False
         else:
            snake.event(event)
      
      screen.fill((0,0,0))
      snake.string_update(text)
      text.blit(screen)
      
      pygame.display.flip()
      clock.tick(60)
      
main()
DrakeMagi
 
Posts: 72
Joined: Sun May 12, 2013 8:36 pm

Re: Not allowing snake to move backwards-is there a better w

Postby dboxall123 » Mon Sep 30, 2013 2:54 pm

Sweet, cheers DrakeMagi :D
dboxall123
 
Posts: 108
Joined: Fri Jul 12, 2013 5:28 pm


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest