Advertisement
SmallBlue

AAAAAAA

Apr 20th, 2021
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.32 KB | None | 0 0
  1. import pygame, sys
  2. import random
  3. import asyncio
  4.  
  5. pygame.init()
  6.  
  7. class GameState:
  8.     def __init__(self):
  9.  
  10.         # Set resolution and title
  11.         self.resolution = (500,500)
  12.         self.screen = pygame.display.set_mode(self.resolution)
  13.         pygame.display.set_caption('Snake but from memory')
  14.  
  15.         # Starting position of Player
  16.         self.x = 200
  17.         self.y = 200
  18.         # Starting position of Fruit (will change when randomized)
  19.         self.fx = 0
  20.         self.fy = 0
  21.  
  22.         self.lastkey = [(50,0)] # Stores keypresses and plays them out in order in every update
  23.  
  24.         self.followers = 0 # The snake's body
  25.         self.pastkeys = [] # Previous locations of the player, used for the body
  26.         self.body = [] # The actual body rectangles, in a list for collision purposes
  27.  
  28.         self.reset()
  29.         self.generate_fruit()
  30.  
  31.     def reset(self):
  32.         self.x = 200
  33.         self.y = 200
  34.         self.followers = 0
  35.         self.lastkey = [(50,0)]
  36.         self.pastkeys = []
  37.         self.body = []
  38.  
  39.     def generate_fruit(self):
  40.         self.fx = random.randint(0,9) * 50 # Wanted for it to be a 500x500 window with 10 "pixels"
  41.         self.fy = random.randint(0,9) * 50
  42.         if self.fx == self.x and self.fy == self.y:
  43.             print('Cant spawn there, snake head is occupying the space!')
  44.             self.generate_fruit()
  45.         else:
  46.             for rect in self.body:
  47.                 if rect.x == self.fx and rect.y == self.fy:
  48.                     print('Cant spawn there, snake body is occupying the space!')
  49.                     self.generate_fruit()
  50.                     break
  51.             self.fx = self.fx + 2
  52.             self.fy = self.fy + 2
  53.  
  54. async def main():
  55.     gamestate = GameState()
  56.  
  57.     player = pygame.draw.rect(gamestate.screen,(255,255,255),(1000,1000,50,50)) # Initialization of the head and fruit. The body doesn't need one
  58.     fruit = pygame.draw.rect(gamestate.screen,(255,0,0),(1000,1000,25,25))      # since it doesn't appear at the start
  59.  
  60.     while True:
  61.         for event in pygame.event.get():
  62.             if event.type == pygame.QUIT: sys.exit()
  63.  
  64.             if event.type==pygame.KEYDOWN:
  65.  
  66.                 if event.key==pygame.K_RIGHT:
  67.                     gamestate.lastkey.append((50,0))
  68.  
  69.                 elif event.key==pygame.K_LEFT:
  70.                     gamestate.lastkey.append((-50,0))
  71.  
  72.                 elif event.key==pygame.K_UP:
  73.                     gamestate.lastkey.append((0,-50))
  74.  
  75.                 elif event.key==pygame.K_DOWN:
  76.                     gamestate.lastkey.append((0,50))
  77.                     '''
  78.                 elif event.key==pygame.K_r:
  79.                     gamestate.generate_fruit()
  80.                     ''' # for debug purposes
  81.  
  82.         gamestate.x = gamestate.x + gamestate.lastkey[0][0] # To prevent creating more useless variables, I just tell it directly how much to move.
  83.         gamestate.y = gamestate.y + gamestate.lastkey[0][1]
  84.  
  85.         if len(gamestate.lastkey) > 1:
  86.             gamestate.lastkey.remove(gamestate.lastkey[0]) # If the player hasn't pressed any more buttons after the last one, keep going the same direction
  87.  
  88.         # ---Boundries stuff---
  89.         if gamestate.x >= gamestate.resolution[0]:
  90.             gamestate.x = 0
  91.         elif gamestate.x < 0:
  92.             gamestate.x = gamestate.resolution[0] - 50
  93.  
  94.         if gamestate.y >= gamestate.resolution[1]:
  95.             gamestate.y = 0
  96.         elif gamestate.y < 0:
  97.             gamestate.y = gamestate.resolution[1] - 50
  98.         # --------------------
  99.  
  100.         gamestate.pastkeys.insert(0,(gamestate.x, gamestate.y)) # Previous coords of the player
  101.  
  102.  
  103.         fruit = pygame.draw.rect(gamestate.screen,(255,0,0),(gamestate.fx,gamestate.fy,25,25)) # Update the location of the fruit and the player.
  104.  
  105.         player = pygame.draw.rect(gamestate.screen,(100,255,100),(gamestate.x,gamestate.y,30,30))
  106.  
  107.         for _ in range(gamestate.followers): # Create the snake's body
  108.             if _ == 0:
  109.                 continue
  110.             gamestate.body.append(pygame.draw.rect(gamestate.screen,(150,255,150),(gamestate.pastkeys[_][0],gamestate.pastkeys[_][1],30,30)))
  111.             while len(gamestate.body) > gamestate.followers - 1:
  112.                 gamestate.body.remove(gamestate.body[0])
  113.  
  114.  
  115.         # --- Collisions ---
  116.         if pygame.Rect.colliderect(player,fruit): # Fruit yummy
  117.             gamestate.generate_fruit()
  118.             if gamestate.followers == 0:
  119.                 gamestate.followers = gamestate.followers + 2
  120.             else:
  121.                 gamestate.followers = gamestate.followers + 1
  122.  
  123.         if pygame.Rect.collidelist(player,gamestate.body) > -1: # Body not yummy
  124.             pygame.time.wait(4000) # Look at what you've done. Be ashamed.
  125.             gamestate.reset()
  126.  
  127.         # -------------------
  128.  
  129.         pygame.display.update() # update the screen
  130.         gamestate.screen.fill((0,0,0))
  131.         await sleep(200) # game speed NOT WORKING RN
  132.  
  133. if __name__ == "__main__":
  134.   loop = asyncio.get_event_loop()
  135.   loop.create_task(main())
  136.   loop.run_until_complete()
  137.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement