|
| 1 | + |
| 2 | +# SNAKE GAME |
| 3 | +# Author : Apaar Gupta (@apaar97) |
| 4 | +# Python 3.5.2 Pygame |
| 5 | + |
| 6 | +import pygame, sys, time, random |
| 7 | + |
| 8 | +# Pygame Init |
| 9 | +init_status = pygame.init() |
| 10 | +if init_status[1]>0 : |
| 11 | + print("(!) Had {0} initialising errors, exiting... ".format(init_status[1])) |
| 12 | + sys.exit() |
| 13 | +else : |
| 14 | + print("(+) Pygame initialised successfully ") |
| 15 | + |
| 16 | +# Play Surface |
| 17 | +size = width,height = 640,320 |
| 18 | +playSurface = pygame.display.set_mode(size) |
| 19 | +pygame.display.set_caption("Snake Game") |
| 20 | + |
| 21 | +# Colors |
| 22 | +red = pygame.Color(255,0,0) |
| 23 | +green = pygame.Color(0,255,0) |
| 24 | +black = pygame.Color(0,0,0) |
| 25 | +white = pygame.Color(255,255,255) |
| 26 | +brown = pygame.Color(165,42,42) |
| 27 | + |
| 28 | +# FPS controller |
| 29 | +fpsController = pygame.time.Clock() |
| 30 | + |
| 31 | +delta = 10 |
| 32 | +snakePos = [100,50] |
| 33 | +snakeBody = [[100,50],[90,50],[80,50]] |
| 34 | +foodPos = [400,50] |
| 35 | +foodSpawn = True |
| 36 | +direction = 'RIGHT' |
| 37 | +changeto = '' |
| 38 | +score = 0 |
| 39 | + |
| 40 | +# Game Over |
| 41 | +def gameOver(): |
| 42 | + myFont = pygame.font.SysFont('monaco',72) |
| 43 | + GOsurf = myFont.render("Game Over",True,red) |
| 44 | + GOrect = GOsurf.get_rect() |
| 45 | + GOrect.midtop = (320,25) |
| 46 | + playSurface.blit(GOsurf,GOrect) |
| 47 | + showScore(0) |
| 48 | + pygame.display.flip() |
| 49 | + time.sleep(4) |
| 50 | + pygame.quit() |
| 51 | + sys.exit() |
| 52 | + |
| 53 | +# Show Score |
| 54 | +def showScore(choice = 1): |
| 55 | + SFont = pygame.font.SysFont('monaco', 32) |
| 56 | + Ssurf = SFont.render("Score : {0}".format(score), True, black) |
| 57 | + Srect = Ssurf.get_rect() |
| 58 | + if choice == 1 : |
| 59 | + Srect.midtop = (80, 10) |
| 60 | + else : |
| 61 | + Srect.midtop = (320,100) |
| 62 | + playSurface.blit(Ssurf, Srect) |
| 63 | + |
| 64 | +while True : |
| 65 | + for event in pygame.event.get(): |
| 66 | + if event.type == pygame.QUIT : |
| 67 | + pygame.quit() |
| 68 | + sys.exit() |
| 69 | + elif event.type == pygame.KEYDOWN : |
| 70 | + if event.key == pygame.K_RIGHT or event.key == pygame.K_d : |
| 71 | + changeto = 'RIGHT' |
| 72 | + if event.key == pygame.K_LEFT or event.key == pygame.K_a: |
| 73 | + changeto = 'LEFT' |
| 74 | + if event.key == pygame.K_UP or event.key == pygame.K_w: |
| 75 | + changeto = 'UP' |
| 76 | + if event.key == pygame.K_DOWN or event.key == pygame.K_s: |
| 77 | + changeto = 'DOWN' |
| 78 | + if event.key == pygame.K_ESCAPE : |
| 79 | + pygame.event.post(pygame.event.Event(pygame.QUIT)) |
| 80 | + |
| 81 | + # Validate direction |
| 82 | + if changeto == 'RIGHT' and direction != 'LEFT' : |
| 83 | + direction = changeto |
| 84 | + if changeto == 'LEFT' and direction != 'RIGHT' : |
| 85 | + direction = changeto |
| 86 | + if changeto == 'UP' and direction != 'DOWN' : |
| 87 | + direction = changeto |
| 88 | + if changeto == 'DOWN' and direction != 'UP' : |
| 89 | + direction = changeto |
| 90 | + |
| 91 | + # Update snake position |
| 92 | + if direction == 'RIGHT' : |
| 93 | + snakePos[0] += delta |
| 94 | + if direction == 'LEFT' : |
| 95 | + snakePos[0] -= delta |
| 96 | + if direction == 'DOWN' : |
| 97 | + snakePos[1] += delta |
| 98 | + if direction == 'UP' : |
| 99 | + snakePos[1] -=delta |
| 100 | + |
| 101 | + # Snake body mechanism |
| 102 | + snakeBody.insert(0,list(snakePos)) |
| 103 | + if snakePos == foodPos : |
| 104 | + foodSpawn = False |
| 105 | + score += 1 |
| 106 | + else : |
| 107 | + snakeBody.pop() |
| 108 | + if foodSpawn == False : |
| 109 | + foodPos = [random.randrange(1, width//10)*delta, random.randrange(1, height//10)*delta] |
| 110 | + foodSpawn = True |
| 111 | + |
| 112 | + playSurface.fill(white) |
| 113 | + for pos in snakeBody : |
| 114 | + pygame.draw.rect(playSurface,green,pygame.Rect(pos[0],pos[1],delta,delta)) |
| 115 | + pygame.draw.rect(playSurface, brown, pygame.Rect(foodPos[0], foodPos[1], delta, delta)) |
| 116 | + |
| 117 | + # Bounds |
| 118 | + if snakePos[0] >= width or snakePos[0] < 0 : |
| 119 | + gameOver() |
| 120 | + if snakePos[1] >= height or snakePos[1] < 0 : |
| 121 | + gameOver() |
| 122 | + |
| 123 | + # Self hit |
| 124 | + for block in snakeBody[1:]: |
| 125 | + if snakePos == block : |
| 126 | + gameOver() |
| 127 | + showScore() |
| 128 | + pygame.display.flip() |
| 129 | + fpsController.tick(20) |
0 commit comments