|
| 1 | +# Snake Game! |
| 2 | + |
| 3 | +# Game imports |
| 4 | +import pygame, sys, random, time |
| 5 | + |
| 6 | +check_errors = pygame.init() |
| 7 | +if check_errors[1] > 0: |
| 8 | + print("(!) Had {0} initializing errors, exiting...".format(check_errors[1])) |
| 9 | + sys.exit(-1) |
| 10 | +else: |
| 11 | + print("(+) PyGame successfully initialized") |
| 12 | + |
| 13 | + |
| 14 | +# Play Surface |
| 15 | +playSurface = pygame.display.set_mode((720, 700)) |
| 16 | +pygame.display.set_caption("Snake Game!") |
| 17 | +# time.sleep(5) |
| 18 | + |
| 19 | +# Colors |
| 20 | +red = pygame.Color(255, 0, 0) # gameover |
| 21 | +green = pygame.Color(0, 255, 0) # snake |
| 22 | +black = pygame.Color(0, 0, 0) # score |
| 23 | +white = pygame.Color(255, 255, 255) # background |
| 24 | +brown = pygame.Color(165, 42, 42) # food |
| 25 | + |
| 26 | +# FPS controller |
| 27 | +fpsController = pygame.time.Clock() |
| 28 | + |
| 29 | +# Position of the snake |
| 30 | +snakePos = [100, 50] |
| 31 | +snakeBody = [[100, 50], [90, 50], [80, 50]] |
| 32 | + |
| 33 | +foodPos = [random.randrange(1, 72) * 10, random.randrange(1, 70) * 10] |
| 34 | +foodSpawn = True |
| 35 | + |
| 36 | +direction = "RIGHT" |
| 37 | +changeto = direction |
| 38 | + |
| 39 | +score = 0 |
| 40 | + |
| 41 | + |
| 42 | +# Game over function |
| 43 | +def gameOver(): |
| 44 | + myFont = pygame.font.SysFont("monaco", 72) |
| 45 | + GOsurf = myFont.render("Game over!", False, red) |
| 46 | + GOrect = GOsurf.get_rect() |
| 47 | + GOrect.midtop = (360, 150) |
| 48 | + playSurface.blit(GOsurf, GOrect) |
| 49 | + showScore(0) |
| 50 | + pygame.display.flip() |
| 51 | + time.sleep(5) |
| 52 | + pygame.quit() # for exiting pygame |
| 53 | + sys.exit() # for exiting from the console |
| 54 | + |
| 55 | + |
| 56 | +def showScore(choice=1): |
| 57 | + scoreFont = pygame.font.SysFont("monaco", 25) |
| 58 | + scoreSurf = scoreFont.render("Score : {0}".format(score), True, black) |
| 59 | + scoreRect = scoreSurf.get_rect() |
| 60 | + if choice == 1: |
| 61 | + scoreRect.midtop = (80, 10) |
| 62 | + else: |
| 63 | + scoreRect.midtop = (360, 120) |
| 64 | + playSurface.blit(scoreSurf, scoreRect) |
| 65 | + |
| 66 | + |
| 67 | +# Main Logic of the game |
| 68 | +while True: |
| 69 | + for event in pygame.event.get(): |
| 70 | + if event.type == pygame.QUIT: |
| 71 | + pygame.quit() |
| 72 | + sys.exit() |
| 73 | + elif event.type == pygame.KEYDOWN: |
| 74 | + if event.key == pygame.K_RIGHT or event.key == ord("d"): |
| 75 | + changeto = "RIGHT" |
| 76 | + if event.key == pygame.K_LEFT or event.key == ord("a"): |
| 77 | + changeto = "LEFT" |
| 78 | + if event.key == pygame.K_UP or event.key == ord("w"): |
| 79 | + changeto = "UP" |
| 80 | + if event.key == pygame.K_DOWN or event.key == ord("s"): |
| 81 | + changeto = "DOWN" |
| 82 | + if event.key == pygame.K_ESCAPE: |
| 83 | + pygame.event.post(pygame.event.Event(pygame.QUIT)) |
| 84 | + |
| 85 | + # validation of direction |
| 86 | + if changeto == "RIGHT" and not direction == "LEFT": |
| 87 | + direction = "RIGHT" |
| 88 | + if changeto == "LEFT" and not direction == "RIGHT": |
| 89 | + direction = "LEFT" |
| 90 | + if changeto == "UP" and not direction == "DOWN": |
| 91 | + direction = "UP" |
| 92 | + if changeto == "DOWN" and not direction == "UP": |
| 93 | + direction = "DOWN" |
| 94 | + |
| 95 | + # Update snake position [x, y] |
| 96 | + if direction == "RIGHT": |
| 97 | + snakePos[0] += 10 |
| 98 | + if direction == "LEFT": |
| 99 | + snakePos[0] -= 10 |
| 100 | + if direction == "UP": |
| 101 | + snakePos[1] -= 10 |
| 102 | + if direction == "DOWN": |
| 103 | + snakePos[1] += 10 |
| 104 | + |
| 105 | + # Snake body mechanism |
| 106 | + snakeBody.insert(0, list(snakePos)) |
| 107 | + if snakePos[0] == foodPos[0] and snakePos[1] == foodPos[1]: |
| 108 | + score += 1 |
| 109 | + foodSpawn = False |
| 110 | + else: |
| 111 | + snakeBody.pop() |
| 112 | + |
| 113 | + if foodSpawn == False: |
| 114 | + foodPos = [random.randrange(1, 72) * 10, random.randrange(1, 70) * 10] |
| 115 | + foodSpawn = True |
| 116 | + |
| 117 | + playSurface.fill(white) |
| 118 | + for pos in snakeBody: |
| 119 | + pygame.draw.rect(playSurface, green, pygame.Rect(pos[0], pos[1], 10, 10)) |
| 120 | + |
| 121 | + pygame.draw.rect(playSurface, brown, pygame.Rect(foodPos[0], foodPos[1], 10, 10)) |
| 122 | + if snakePos[0] > 710 or snakePos[0] < 0: |
| 123 | + gameOver() |
| 124 | + if snakePos[1] > 690 or snakePos[1] < 0: |
| 125 | + gameOver() |
| 126 | + |
| 127 | + for block in snakeBody[1:]: |
| 128 | + if snakePos[0] == block[0] and snakePos[1] == block[1]: |
| 129 | + gameOver() |
| 130 | + showScore() |
| 131 | + pygame.display.flip() |
| 132 | + fpsController.tick(18) |
0 commit comments