import pygame, sys
import random
import asyncio
pygame.init()
class GameState:
def __init__(self):
# Set resolution and title
self.resolution = (500,500)
self.screen = pygame.display.set_mode(self.resolution)
pygame.display.set_caption('Snake but from memory')
# Starting position of Player
self.x = 200
self.y = 200
# Starting position of Fruit (will change when randomized)
self.fx = 0
self.fy = 0
self.lastkey = [(50,0)] # Stores keypresses and plays them out in order in every update
self.followers = 0 # The snake's body
self.pastkeys = [] # Previous locations of the player, used for the body
self.body = [] # The actual body rectangles, in a list for collision purposes
self.reset()
self.generate_fruit()
def reset(self):
self.x = 200
self.y = 200
self.followers = 0
self.lastkey = [(50,0)]
self.pastkeys = []
self.body = []
def generate_fruit(self):
self.fx = random.randint(0,9) * 50 # Wanted for it to be a 500x500 window with 10 "pixels"
self.fy = random.randint(0,9) * 50
if self.fx == self.x and self.fy == self.y:
print('Cant spawn there, snake head is occupying the space!')
self.generate_fruit()
else:
for rect in self.body:
if rect.x == self.fx and rect.y == self.fy:
print('Cant spawn there, snake body is occupying the space!')
self.generate_fruit()
break
self.fx = self.fx + 2
self.fy = self.fy + 2
async def main():
gamestate = GameState()
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
fruit = pygame.draw.rect(gamestate.screen,(255,0,0),(1000,1000,25,25)) # since it doesn't appear at the start
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
if event.type==pygame.KEYDOWN:
if event.key==pygame.K_RIGHT:
gamestate.lastkey.append((50,0))
elif event.key==pygame.K_LEFT:
gamestate.lastkey.append((-50,0))
elif event.key==pygame.K_UP:
gamestate.lastkey.append((0,-50))
elif event.key==pygame.K_DOWN:
gamestate.lastkey.append((0,50))
'''
elif event.key==pygame.K_r:
gamestate.generate_fruit()
''' # for debug purposes
gamestate.x = gamestate.x + gamestate.lastkey[0][0] # To prevent creating more useless variables, I just tell it directly how much to move.
gamestate.y = gamestate.y + gamestate.lastkey[0][1]
if len(gamestate.lastkey) > 1:
gamestate.lastkey.remove(gamestate.lastkey[0]) # If the player hasn't pressed any more buttons after the last one, keep going the same direction
# ---Boundries stuff---
if gamestate.x >= gamestate.resolution[0]:
gamestate.x = 0
elif gamestate.x < 0:
gamestate.x = gamestate.resolution[0] - 50
if gamestate.y >= gamestate.resolution[1]:
gamestate.y = 0
elif gamestate.y < 0:
gamestate.y = gamestate.resolution[1] - 50
# --------------------
gamestate.pastkeys.insert(0,(gamestate.x, gamestate.y)) # Previous coords of the player
fruit = pygame.draw.rect(gamestate.screen,(255,0,0),(gamestate.fx,gamestate.fy,25,25)) # Update the location of the fruit and the player.
player = pygame.draw.rect(gamestate.screen,(100,255,100),(gamestate.x,gamestate.y,30,30))
for _ in range(gamestate.followers): # Create the snake's body
if _ == 0:
continue
gamestate.body.append(pygame.draw.rect(gamestate.screen,(150,255,150),(gamestate.pastkeys[_][0],gamestate.pastkeys[_][1],30,30)))
while len(gamestate.body) > gamestate.followers - 1:
gamestate.body.remove(gamestate.body[0])
# --- Collisions ---
if pygame.Rect.colliderect(player,fruit): # Fruit yummy
gamestate.generate_fruit()
if gamestate.followers == 0:
gamestate.followers = gamestate.followers + 2
else:
gamestate.followers = gamestate.followers + 1
if pygame.Rect.collidelist(player,gamestate.body) > -1: # Body not yummy
pygame.time.wait(4000) # Look at what you've done. Be ashamed.
gamestate.reset()
# -------------------
pygame.display.update() # update the screen
gamestate.screen.fill((0,0,0))
await sleep(200) # game speed NOT WORKING RN
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.create_task(main())
loop.run_until_complete()