Posted by King_Critter on Fri 12th Feb 02:53
download | new post | report as spam

  1. #! /usr/bin/python
  2.  
  3. import random
  4.  
  5. class Snake:
  6.     def __init__(self):
  7.         self.x = 5     
  8.         self.y = 5     
  9.         # self.score = 0
  10.     # def eat(self): #eat function added
  11.     #     if self.x == Food().x and self.y == Food().y:
  12.     #         self.score += Food().points             
  13.     #         Food().eaten()#calls eat func but eat func doesn't do anything
  14.     def move(self, direction):                                             
  15.         if "up"    in direction: self.y -= 1                               
  16.         if "down"  in direction: self.y += 1                               
  17.         if "right" in direction: self.x += 1                               
  18.         if "left"  in direction: self.x -= 1                               
  19.         if self.y > maxgrid: self.y = 1                                     
  20.         if self.y < 1:       self.y = maxgrid                               
  21.         if self.x > maxgrid: self.x = 1                                     
  22.         if self.x < 1:       self.x = maxgrid                               
  23.         # self.eat()                                                       
  24.     def sleep(self):                                                       
  25.         pass                                                               
  26.  
  27. class Badguy:
  28.     def __init__(self):
  29.         self.x = 2     
  30.         self.y = 2     
  31.     def chase(self):   
  32.         if self.x == player.x:
  33.             if self.y < player.y: self.y += 1
  34.             if self.y > player.y: self.y -= 1
  35.         elif self.y == player.y:             
  36.             if self.x < player.x: self.x += 1
  37.             if self.x > player.x: self.x -= 1
  38.         else:                               
  39.             self.wander()                   
  40.         self.check()                         
  41.     def wander(self):                       
  42.         self.x += random.choice((-1, 0, 1)) 
  43.         self.y += random.choice((-1, 0, 1)) 
  44.         self.check()                         
  45.     def check(self):                         
  46.         if self.y > maxgrid: self.y = 1     
  47.         if self.y < 1:       self.y = maxgrid
  48.         if self.x > maxgrid: self.x = 1     
  49.         if self.x < 1:       self.x = maxgrid
  50.  
  51. # class Food: #food class declared
  52. #     def __init__(self): #for some reason keeps re-calling init
  53. #         self.x = 4                                           
  54. #         self.y = 4                                           
  55. #         self.points = random.randint(1, 9)                   
  56. #     def eaten(self): #doesn't work                           
  57. #         self.x = random.randint(1, maxgrid)                   
  58. #         self.y = random.randint(1, maxgrid)                   
  59. #         self.points = random.randint(1, 9)                   
  60.  
  61. def show_field():
  62.     bookends = ("| {0}|".format(" - "*maxgrid))
  63.     print(bookends)                           
  64.     for y in range(maxgrid):                   
  65.         line = ""                             
  66.         for x in range(maxgrid):               
  67.             if x == vader.x-1 and y == vader.y-1:
  68.                 line += " C "                   
  69.             elif x == player.x-1 and y == player.y-1:
  70.                 line += " P "                       
  71.             # elif x == berry.x-1 and y == berry.y-1:
  72.             #     line += (" {0} ").format(Food().points)
  73.             else:
  74.                 line = line + " . "
  75.         print("| {0}|".format(line))
  76.     print(bookends)
  77.  
  78. maxgrid = int(input("Size of square grid?"))
  79. player = Snake()
  80. # berry = Food()
  81. vader = Badguy()
  82.  
  83. while True:
  84.     show_field()
  85.     query = input("What direction? ").lower() #added for flexibility
  86.     vader.chase()
  87.     player.move(query)

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily
.
Syntax highlighting:

To highlight particular lines, prefix each line with @@
   Remember me