Advertisement
D1mitroV

Untitled

Apr 6th, 2021
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.77 KB | None | 0 0
  1. import random
  2. import turtle
  3. import time
  4.  
  5.  
  6. class Square:
  7.     def __init__(self, x, y):
  8.         self.x = x
  9.         self.y = y
  10.  
  11.     def drawself(self, turtle):
  12.         # draw a black box at its coordinates, leaving a small gap between cubes
  13.         turtle.goto(self.x - 9, self.y - 9)
  14.         turtle.begin_fill()
  15.         for i in range(4):
  16.             turtle.forward(18)
  17.             turtle.left(90)
  18.         turtle.end_fill()
  19.  
  20.  
  21. class Food:
  22.     def __init__(self, x, y):
  23.         self.x = x
  24.         self.y = y
  25.         self.state = "ON"
  26.  
  27.     def changelocation(self):
  28.         # I haven't programmed it to spawn outside the snake's body yet
  29.         self.x = random.randint(0, 20)*20 - 200
  30.         self.y = random.randint(0, 20)*20 - 200
  31.  
  32.     def drawself(self, turtle):
  33.         # similar to the Square drawself, but blinks on and off
  34.         if self.state == "ON":
  35.             turtle.goto(self.x - 9, self.y - 9)
  36.             turtle.begin_fill()
  37.             for i in range(4):
  38.                 turtle.forward(18)
  39.                 turtle.left(90)
  40.             turtle.end_fill()
  41.  
  42.     def changestate(self):
  43.         # controls the blinking
  44.         self.state = "OFF" if self.state == "ON" else "ON"
  45.  
  46.  
  47. class Snake:
  48.     def __init__(self):
  49.         self.headposition = [20, 0] # keeps track of where it needs to go next
  50.         self.body = [Square(-20, 0), Square(0, 0), Square(20, 0)] # body is a list of squares
  51.         self.nextX = 1 # tells the snake which way it's going next
  52.         self.nextY = 0
  53.         self.crashed = False # I'll use this when I get around to collision detection
  54.         self.nextposition = [self.headposition[0] + 20*self.nextX,
  55.                              self.headposition[1] + 20*self.nextY]
  56.         # prepares the next location to add to the snake
  57.  
  58.     def moveOneStep(self):
  59.         if Square(self.nextposition[0], self.nextposition[1]) not in self.body:
  60.             # attempt (unsuccessful) at collision detection
  61.             self.body.append(Square(self.nextposition[0], self.nextposition[1]))
  62.             # moves the snake head to the next spot, deleting the tail
  63.             del self.body[0]
  64.             self.headposition[0], self.headposition[1] = self.body[-1].x, self.body[-1].y
  65.         # resets the head and nextposition
  66.             self.nextposition = [self.headposition[0] + 20*self.nextX,
  67.                                  self.headposition[1] + 20*self.nextY]
  68.         else:
  69.             self.crashed = True # more unsuccessful collision detection
  70.  
  71.     def moveup(self): # pretty obvious what these do
  72.         self.nextX = 0
  73.         self.nextY = 1
  74.  
  75.     def moveleft(self):
  76.         self.nextX = -1
  77.         self.nextY = 0
  78.  
  79.     def moveright(self):
  80.         self.nextX = 1
  81.         self.nextY = 0
  82.  
  83.     def movedown(self):
  84.         self.nextX = 0
  85.         self.nextY = -1
  86.  
  87.     def eatFood(self):
  88.         # adds the next spot without deleting the tail, extending the snake by 1
  89.         self.body.append(Square(self.nextposition[0], self.nextposition[1]))
  90.         self.headposition[0], self.headposition[1] = self.body[-1].x, self.body[-1].y
  91.         self.nextposition = [self.headposition[0] + 20*self.nextX,
  92.                              self.headposition[1] + 20*self.nextY]
  93.  
  94.     def drawself(self, turtle): # draws the whole snake when called
  95.         for segment in self.body:
  96.             segment.drawself(turtle)
  97.  
  98.  
  99. class Game:
  100.     def __init__(self):
  101.         # game object has a screen, a turtle, a basic snake and a food
  102.         self.screen = turtle.Screen()
  103.         self.artist = turtle.Turtle()
  104.         self.artist.up()
  105.         self.artist.hideturtle()
  106.         self.snake = Snake()
  107.         self.food = Food(100, 0)
  108.         self.counter = 0 # this will be used later
  109.         self.commandpending = False # as will this
  110.  
  111.     def nextFrame(self):
  112.         while True: # now here's where it gets fiddly...
  113.             game.screen.listen()
  114.             game.screen.onkey(game.snakedown, "Down")
  115.             game.screen.onkey(game.snakeup, "Up")
  116.             game.screen.onkey(game.snakeleft, "Left")
  117.             game.screen.onkey(game.snakeright, "Right")
  118.             turtle.tracer(0) # follow it so far?
  119.             self.artist.clear()
  120.             if self.counter == 5:
  121.             # only moves to next frame every 5 loops, this was an attempt to get rid of the turning delay
  122.                 if (self.snake.nextposition[0], self.snake.nextposition[1]) == (self.food.x, self.food.y):
  123.                     self.snake.eatFood()
  124.                     self.food.changelocation()
  125.                 else:
  126.                     self.snake.moveOneStep()
  127.                 self.counter = 0
  128.             else:
  129.                 self.counter += 1
  130.             self.food.changestate() # makes the food flash
  131.             self.food.drawself(self.artist) # show the food and snake
  132.             self.snake.drawself(self.artist)
  133.             turtle.update()
  134.             self.commandpending = False
  135.             time.sleep(0.05)
  136.  
  137.     def snakeup(self):
  138.         print("going up") # put this in for debugging purposes
  139.         if not self.commandpending:
  140.         # should allow only one turn each frame; I don't think it's working
  141.             self.snake.moveup()
  142.             self.commandpending = True
  143.  
  144.     def snakedown(self):
  145.         print("going down")
  146.         if not self.commandpending:
  147.             self.snake.movedown()
  148.             self.commandpending = True
  149.  
  150.     def snakeleft(self):
  151.         print("going left")
  152.         if not self.commandpending:
  153.             self.snake.moveleft()
  154.             self.commandpending = True
  155.  
  156.     def snakeright(self):
  157.         print("going right")
  158.         if not self.commandpending:
  159.             self.snake.moveright()
  160.             self.commandpending = True
  161.  
  162.  
  163. game = Game()
  164. game.nextFrame()
  165. print("game over!")
  166.  
  167. game.screen.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement