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