|
| 1 | +import sys,random,time |
| 2 | +import pygame |
| 3 | + |
| 4 | +pygame.init() |
| 5 | + |
| 6 | +WIN_X = 800 |
| 7 | +WIN_Y = 600 |
| 8 | +WIN = pygame.display.set_mode((WIN_X,WIN_Y)) |
| 9 | +pygame.display.set_caption('snake game') |
| 10 | +font=pygame.font.SysFont('comicsans',40) |
| 11 | + |
| 12 | +#main function |
| 13 | +def main_menu(): |
| 14 | + while 1: |
| 15 | + for event in pygame.event.get(): |
| 16 | + if event.type == pygame.QUIT: |
| 17 | + pygame.quit() |
| 18 | + sys.exit() |
| 19 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 20 | + main() |
| 21 | + WIN.fill((0,0,0)) |
| 22 | + main_menu_message = font.render('Press anywhere to start the game' , True , (255,255,255)) |
| 23 | + font_pos = main_menu_message.get_rect(center=(WIN_X//2, WIN_Y//2)) |
| 24 | + WIN.blit(main_menu_message , font_pos) |
| 25 | + pygame.display.update() |
| 26 | + |
| 27 | + |
| 28 | +def game_over(score): |
| 29 | + while 1: |
| 30 | + for event in pygame.event.get(): |
| 31 | + if event.type == pygame.QUIT: |
| 32 | + pygame.quit() |
| 33 | + sys.exit() |
| 34 | + WIN.fill((0,0,0)) |
| 35 | + game_over_message = font.render('You Lost' , True , (255,0,0)) |
| 36 | + game_over_score = font.render(f'Your Score was {score}' , True , (255,255,255)) |
| 37 | + |
| 38 | + font_pos_message = game_over_message.get_rect(center=(WIN_X//2, WIN_Y//2)) |
| 39 | + font_pos_score = game_over_score.get_rect(center=(WIN_X//2, WIN_Y//2+40)) |
| 40 | + WIN.blit(game_over_message , font_pos_message) |
| 41 | + WIN.blit(game_over_score , font_pos_score) |
| 42 | + pygame.display.update() |
| 43 | + time.sleep(3) |
| 44 | + main_menu() |
| 45 | + |
| 46 | + |
| 47 | +def main(): |
| 48 | + CLOCK = pygame.time.Clock() |
| 49 | + snake_pos=[200,70] |
| 50 | + snake_body=[[200,70] , [200-10 , 70] , [200-(2*10),70]] |
| 51 | + fruit_pos = [0,0] |
| 52 | + fruit_spawn = True |
| 53 | + direction = 'right' |
| 54 | + score=0 |
| 55 | + CLOCK = pygame.time.Clock() |
| 56 | + #game loop |
| 57 | + while 1: |
| 58 | + for event in pygame.event.get(): |
| 59 | + if event.type == pygame.QUIT: |
| 60 | + pygame.quit() |
| 61 | + sys.exit() |
| 62 | + |
| 63 | + keys= pygame.key.get_pressed() |
| 64 | + if (keys[pygame.K_w] or keys[pygame.K_UP]) and direction != 'down': |
| 65 | + direction = 'up' |
| 66 | + if (keys[pygame.K_s] or keys[pygame.K_DOWN]) and direction != 'up': |
| 67 | + direction = 'down' |
| 68 | + if (keys[pygame.K_d] or keys[pygame.K_RIGHT]) and direction != 'left': |
| 69 | + direction = 'right' |
| 70 | + if (keys[pygame.K_a] or keys[pygame.K_LEFT]) and direction != 'right': |
| 71 | + direction = 'left' |
| 72 | + |
| 73 | + WIN.fill((0,0,0)) |
| 74 | + for square in snake_body: |
| 75 | + pygame.draw.rect(WIN ,(255, 255, 0), (square[0],square[1],10,10)) |
| 76 | + if direction == 'right': |
| 77 | + snake_pos[0] += 10 |
| 78 | + elif direction == 'left': |
| 79 | + snake_pos[0] -= 10 |
| 80 | + elif direction == 'up': |
| 81 | + snake_pos[1] -= 10 |
| 82 | + elif direction == 'down': |
| 83 | + snake_pos[1] += 10 |
| 84 | + |
| 85 | + snake_body.append(list(snake_pos)) |
| 86 | + |
| 87 | + if fruit_spawn: |
| 88 | + fruit_pos = [random.randrange(40,WIN_X-40),random.randrange(40,WIN_Y-40)] |
| 89 | + fruit_spawn = False |
| 90 | + |
| 91 | + pygame.draw.rect(WIN ,(138,43,226),(fruit_pos[0],fruit_pos[1],10,10)) |
| 92 | + |
| 93 | + score_font = font.render(f'{score}' , True , (255,255,255)) |
| 94 | + font_pos = score_font.get_rect(center=(WIN_X//2-40 , 30)) |
| 95 | + WIN.blit(score_font , font_pos) |
| 96 | + |
| 97 | + if pygame.Rect(snake_pos[0],snake_pos[1],10,10).colliderect(pygame.Rect(fruit_pos[0],fruit_pos[1],10,10)): |
| 98 | + fruit_spawn=True |
| 99 | + score += 5 |
| 100 | + else: |
| 101 | + snake_body.pop(0) |
| 102 | + |
| 103 | + for square in snake_body[:-1]: |
| 104 | + if pygame.Rect(square[0],square[1],10,10).colliderect(pygame.Rect(snake_pos[0],snake_pos[1],10,10)): |
| 105 | + game_over(score) |
| 106 | + |
| 107 | + if snake_pos[0]+10 <=0 or snake_pos[0] >= WIN_X: |
| 108 | + game_over(score) |
| 109 | + if snake_pos[1]+10 <=0 or snake_pos[1] >= WIN_Y: |
| 110 | + game_over(score) |
| 111 | + pygame.display.update() |
| 112 | + |
| 113 | + CLOCK.tick(25) |
| 114 | + |
| 115 | +#caliing the main function |
| 116 | +main_menu() |
0 commit comments