|
| 1 | +#!/usr/bin/env python |
| 2 | +# coding: utf-8 |
| 3 | + |
| 4 | +# In[ ]: |
| 5 | + |
| 6 | + |
| 7 | +import pygame,sys |
| 8 | +import time |
| 9 | +import random |
| 10 | + |
| 11 | +pygame.init() |
| 12 | + |
| 13 | +white = (255,255,255) |
| 14 | +black = (100,0,0) |
| 15 | +red = (255,0,0) |
| 16 | +window_width = 800 |
| 17 | +window_height = 600 |
| 18 | + |
| 19 | +gameDisplay = pygame.display.set_mode((window_width,window_height)) |
| 20 | +pygame.display.set_caption('slither') |
| 21 | + |
| 22 | +clock = pygame.time.Clock() |
| 23 | +FPS = 5 |
| 24 | +blockSize = 20 |
| 25 | +noPixel = 0 |
| 26 | +''' |
| 27 | +sizeGrd = window_width // blockSize |
| 28 | +row = 0 |
| 29 | +col = 0 |
| 30 | +for nextline in range(sizeGrd): |
| 31 | +''' |
| 32 | +def myquit(): |
| 33 | + ''' Self explanatory ''' |
| 34 | + pygame.quit() |
| 35 | + sys.exit(0) |
| 36 | + |
| 37 | +font = pygame.font.SysFont(None, 25, bold=True) |
| 38 | +def drawGrid(): |
| 39 | + sizeGrd = window_width // blockSize |
| 40 | +def snake(blockSize, snakelist): |
| 41 | + #x = 250 - (segment_width + segment_margin) * i |
| 42 | + for size in snakelist: |
| 43 | + pygame.draw.rect(gameDisplay, black,[size[0]+5,size[1],blockSize,blockSize],2) |
| 44 | + |
| 45 | +def message_to_screen(msg, color): |
| 46 | + screen_text = font.render(msg, True, color) |
| 47 | + gameDisplay.blit(screen_text, [window_width/2, window_height/2]) |
| 48 | + |
| 49 | +def gameLoop(): |
| 50 | + gameExit = False |
| 51 | + gameOver = False |
| 52 | + |
| 53 | + lead_x = window_width/2 |
| 54 | + lead_y = window_height/2 |
| 55 | + |
| 56 | + change_pixels_of_x = 0 |
| 57 | + change_pixels_of_y = 0 |
| 58 | + |
| 59 | + snakelist = [] |
| 60 | + snakeLength = 1 |
| 61 | + |
| 62 | + randomAppleX = round(random.randrange(0, window_width-blockSize)/10.0)*10.0 |
| 63 | + randomAppleY = round(random.randrange(0, window_height-blockSize)/10.0)*10.0 |
| 64 | + |
| 65 | + while not gameExit: |
| 66 | + |
| 67 | + while gameOver == True: |
| 68 | + gameDisplay.fill(white) |
| 69 | + message_to_screen("Game over, press c to play again or Q to quit", red) |
| 70 | + pygame.display.update() |
| 71 | + |
| 72 | + for event in pygame.event.get(): |
| 73 | + if event.type == pygame.QUIT: |
| 74 | + gameOver = False |
| 75 | + gameExit = True |
| 76 | + |
| 77 | + if event.type == pygame.KEYDOWN: |
| 78 | + if event.key == pygame.K_q: |
| 79 | + gameExit = True |
| 80 | + gameOver = False |
| 81 | + if event.key == pygame.K_c: |
| 82 | + gameLoop() |
| 83 | + |
| 84 | + for event in pygame.event.get(): |
| 85 | + if event.type == pygame.QUIT: |
| 86 | + gameExit = True |
| 87 | + |
| 88 | + if event.type == pygame.KEYDOWN: |
| 89 | + if event.key == pygame.K_ESCAPE: |
| 90 | + myquit() |
| 91 | + leftArrow = event.key == pygame.K_LEFT |
| 92 | + rightArrow = event.key == pygame.K_RIGHT |
| 93 | + upArrow = event.key == pygame.K_UP |
| 94 | + downArrow = event.key == pygame.K_DOWN |
| 95 | + |
| 96 | + if leftArrow: |
| 97 | + change_pixels_of_x = -blockSize |
| 98 | + change_pixels_of_y = noPixel |
| 99 | + elif rightArrow: |
| 100 | + change_pixels_of_x = blockSize |
| 101 | + change_pixels_of_y = noPixel |
| 102 | + elif upArrow: |
| 103 | + change_pixels_of_y = -blockSize |
| 104 | + change_pixels_of_x = noPixel |
| 105 | + elif downArrow: |
| 106 | + change_pixels_of_y = blockSize |
| 107 | + change_pixels_of_x = noPixel |
| 108 | + |
| 109 | + if lead_x >= window_width or lead_x < 0 or lead_y >= window_height or lead_y < 0: |
| 110 | + gameOver = True |
| 111 | + |
| 112 | + lead_x += change_pixels_of_x |
| 113 | + lead_y += change_pixels_of_y |
| 114 | + |
| 115 | + gameDisplay.fill(white) |
| 116 | + |
| 117 | + AppleThickness = 20 |
| 118 | + |
| 119 | + print([int(randomAppleX),int(randomAppleY),AppleThickness,AppleThickness]) |
| 120 | + pygame.draw.rect(gameDisplay, red, [randomAppleX,randomAppleY,AppleThickness,AppleThickness]) |
| 121 | + |
| 122 | + allspriteslist = [] |
| 123 | + allspriteslist.append(lead_x) |
| 124 | + allspriteslist.append(lead_y) |
| 125 | + snakelist.append(allspriteslist) |
| 126 | + |
| 127 | + if len(snakelist) > snakeLength: |
| 128 | + del snakelist[0] |
| 129 | + |
| 130 | + for eachSegment in snakelist [:-1]: |
| 131 | + if eachSegment == allspriteslist: |
| 132 | + gameOver = True |
| 133 | + |
| 134 | + snake(blockSize, snakelist) |
| 135 | + |
| 136 | + pygame.display.update() |
| 137 | + |
| 138 | + if lead_x >= randomAppleX and lead_x <= randomAppleX + AppleThickness: |
| 139 | + if lead_y >= randomAppleY and lead_y <= randomAppleY + AppleThickness: |
| 140 | + randomAppleX = round(random.randrange(0, window_width-blockSize)/10.0)*10.0 |
| 141 | + randomAppleY = round(random.randrange(0, window_height-blockSize)/10.0)*10.0 |
| 142 | + snakeLength += 1 |
| 143 | + |
| 144 | + |
| 145 | + clock.tick(FPS) |
| 146 | + |
| 147 | + pygame.quit() |
| 148 | + quit() |
| 149 | +gameLoop() |
| 150 | + |
0 commit comments