ManifestJW

PySnake Source Code

Dec 4th, 2020 (edited)
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.29 KB | None | 0 0
  1. import pygame
  2. import os
  3. from random import *
  4. pygame.init()
  5. pygame.font.init()
  6. pygame.mixer.init()
  7.  
  8. def resource_path(relative_path):
  9.     try:
  10.         # PyInstaller creates a temp folder and set the path in _MEIPASS
  11.         base_path = sys._MEIPASS
  12.     except Exception:
  13.         base_path = os.path.abspath(".")
  14.  
  15.     return os.path.join(base_path, relative_path)
  16.  
  17. class Game:
  18.     def __init__(self, screenwidth, screenheight, score):
  19.         self.screenwidth = screenwidth
  20.         self.screenheight = screenheight
  21.         self.win = pygame.display.set_mode((screenwidth, screenheight))
  22.         self.font = pygame.font.SysFont('Comic Sans MS', 30)
  23.         self.moveevent = pygame.USEREVENT + 1
  24.         self.score = score
  25.         self.explosionImg = pygame.image.load(resource_path('assets/Images/Explosion.png'))
  26.         self.explosionSound = pygame.mixer.Sound(resource_path('assets/Sounds/Explosion.mp3'))
  27.         self.textSurface = self.font.render('Score: ' + str(self.score), False, (255, 255, 255))
  28.         self.memeSound = pygame.mixer.Sound(resource_path('assets/Sounds/memeSound.mp3'))
  29.         pygame.display.set_caption("Snake")
  30.         pygame.time.set_timer(self.moveevent, 200)
  31.         self.explosionImg = pygame.transform.scale(self.explosionImg, (800, 704))
  32.         self.run = True
  33.  
  34.     def handleEvents(self):
  35.         # Event for loop that gets every event in the entire pygame module
  36.         for event in pygame.event.get():
  37.             # If statements that compares the current event to the event assigned
  38.             if event.type == pygame.QUIT:
  39.                 self.run = False
  40.             # This event is what moves the snake
  41.             if event.type == self.moveevent:
  42.                 if player.snakeDirection == 1:
  43.                     player.grid_y += -32
  44.                 elif player.snakeDirection == 2:
  45.                     player.grid_y += 32
  46.                 elif player.snakeDirection == 3:
  47.                     player.grid_x += -32
  48.                 elif player.snakeDirection == 4:
  49.                     player.grid_x += 32
  50.  
  51.                 # Gives the illusion of movement by removing the tail, and adding the new head position to the front
  52.                 player.snakeBody.pop(-1)
  53.                 player.snakeBody.insert(0, [player.grid_x, player.grid_y])
  54.  
  55.                 player.justTurned = 0
  56.  
  57.         # Gets the key events and puts them into a variable
  58.         keys = pygame.key.get_pressed()
  59.  
  60.         # This is what allows for the arrow keys to control the snake
  61.         if keys[pygame.K_UP]:
  62.             if player.snakeDirection != 2 and player.justTurned == 0:
  63.                 player.snakeDirection = 1
  64.                 player.justTurned = 1
  65.         if keys[pygame.K_DOWN]:
  66.             if player.snakeDirection != 1 and player.justTurned == 0:
  67.                 player.snakeDirection = 2
  68.                 player.justTurned = 1
  69.         if keys[pygame.K_LEFT]:
  70.             if player.snakeDirection != 4 and player.justTurned == 0:
  71.                 player.snakeDirection = 3
  72.                 player.justTurned = 1
  73.         if keys[pygame.K_RIGHT]:
  74.             if player.snakeDirection != 3 and player.justTurned == 0:
  75.                 player.snakeDirection = 4
  76.                 player.justTurned = 1
  77.  
  78.         # Checks to see if the snake has crashed into a wall
  79.         if player.grid_x < 0 or player.grid_x > 768:
  80.             self.run = False
  81.             self.win.blit(self.explosionImg, (0, 0))
  82.             self.win.blit(self.textSurface, (650, 650))
  83.             pygame.mixer.Sound.play(self.memeSound)
  84.             pygame.time.delay(1480)
  85.             pygame.mixer.Sound.stop(self.memeSound)
  86.             pygame.mixer.Sound.play(self.explosionSound)
  87.             pygame.display.update()
  88.             pygame.time.delay(1000)
  89.         if player.grid_y < 0 or player.grid_y > 672:
  90.             self.run = False
  91.             self.win.blit(self.explosionImg, (0, 0))
  92.             self.win.blit(self.textSurface, (650, 650))
  93.             pygame.mixer.Sound.play(self.memeSound)
  94.             pygame.time.delay(1480)
  95.             pygame.mixer.Sound.stop(self.memeSound)
  96.             pygame.mixer.Sound.play(self.explosionSound)
  97.             pygame.display.update()
  98.             pygame.time.delay(1000)
  99.  
  100.         # Checks to see if the snake has collided with itself
  101.         if player.snakeBody[0] in player.snakeBody[1:]:
  102.             self.run = False
  103.             self.win.blit(self.explosionImg, (0, 0))
  104.             self.win.blit(self.textSurface, (650, 650))
  105.             pygame.mixer.Sound.play(self.memeSound)
  106.             pygame.time.delay(1480)
  107.             pygame.mixer.Sound.stop(self.memeSound)
  108.             pygame.mixer.Sound.play(self.explosionSound)
  109.             pygame.display.update()
  110.             pygame.time.delay(1000)
  111.  
  112.         # Collision detection between the snake head and the apple
  113.         if player.grid_x == apple.x and player.grid_y == apple.y:
  114.             player.length += 1
  115.             player.snakeBody.append([1000, 1000])
  116.             self.score += 1
  117.             apple.randomizeLocation()
  118.  
  119.     def updateScreen(self):
  120.         # Controls the FPS for the Game (100 FPS)
  121.         pygame.time.delay(10)
  122.  
  123.         # fills the entire screen with black, erasing the old snake position
  124.         self.win.fill((0, 0, 0))
  125.  
  126.         # Draws the apple to the screen
  127.         pygame.draw.rect(self.win, (255, 0, 0), (apple.x, apple.y, apple.width, apple.height))
  128.  
  129.         # Draws the snake to the screen
  130.         for segment in player.snakeBody:
  131.             pygame.draw.rect(self.win, (0, 255, 0), (segment[0], segment[1], player.width, player.height))
  132.  
  133.         # Displays Score
  134.         self.win.blit(self.textSurface, (650, 650))
  135.  
  136.         # Updates the Screen
  137.         pygame.display.update()
  138.  
  139.  
  140. class Player:
  141.     def __init__(self, grid_x, grid_y, width, height, length, snakeDirection, justTurned,):
  142.         self.grid_x = grid_x
  143.         self.grid_y = grid_y
  144.         self.width = width
  145.         self.height = height
  146.         self.length = length
  147.         self.snakeDirection = snakeDirection
  148.         self.justTurned = justTurned
  149.         self.snakeBody = [[grid_x, grid_y]]
  150.  
  151.  
  152. class Apple:
  153.     def __init__(self, x, y, width, height, apple_in_snake):
  154.         self.x = x
  155.         self.y = y
  156.         self.width = width
  157.         self.height = height
  158.         self.apple_in_snake = apple_in_snake
  159.  
  160.     def randomizeLocation(self):
  161.         self.apple_in_snake = True
  162.         while self.apple_in_snake:
  163.             self.x = round(randint(0, 768) / 32) * 32
  164.             self.y = round(randint(0, 672) / 32) * 32
  165.             self.apple_in_snake = False
  166.             if len(player.snakeBody) == 1:
  167.                 if self.x == player.snakeBody[0][0] and self.y == player.snakeBody[0][1]:
  168.                     self.apple_in_snake = True
  169.             else:
  170.                 if self.x in player.snakeBody[0:][0] and self.y in player.snakeBody[0:][1]:
  171.                     self.apple_in_snake = True
  172.  
  173.  
  174. # Initializes the Game class
  175. game = Game(800, 704, 0)
  176.  
  177. # Initializes the Player class
  178. player = Player(384, 320, 32, 32, 1, 0, 0)
  179.  
  180. # Initializes the Apple class and picks a random position
  181. apple = Apple(0, 0, 32, 32, True)
  182. apple.randomizeLocation()
  183.  
  184. # Main Loop
  185. while game.run:
  186.     game.handleEvents()
  187.     game.updateScreen()
  188.  
  189. # Close the window after the game is no longer running
  190. pygame.quit()
  191.  
Add Comment
Please, Sign In to add comment