| 50 | |
| 51 | |
| 52 | def gameLoop(): |
| 53 | game_over = False |
| 54 | game_close = False |
| 55 | |
| 56 | x1 = display_width / 2 |
| 57 | y1 = display_height / 2 |
| 58 | |
| 59 | x1_change = 0 |
| 60 | y1_change = 0 |
| 61 | |
| 62 | snake_List = [] |
| 63 | Length_of_snake = 1 |
| 64 | |
| 65 | foodx = round(random.randrange( |
| 66 | 0, display_width - snake_block) / 10.0) * 10.0 |
| 67 | |
| 68 | foody = round(random.randrange( |
| 69 | 0, display_height - snake_block) / 10.0) * 10.0 |
| 70 | |
| 71 | while not game_over: |
| 72 | |
| 73 | while game_close == True: |
| 74 | display.fill(black) # Background color |
| 75 | message("You Lost! Press Space Again or Q to Quit", red, 35) |
| 76 | |
| 77 | score(Length_of_snake - 1) |
| 78 | pygame.display.update() |
| 79 | |
| 80 | # If Q pressed |
| 81 | # The game will close |
| 82 | |
| 83 | # If Space is pressed |
| 84 | # Are start new game |
| 85 | for event in pygame.event.get(): |
| 86 | if event.type == pygame.KEYDOWN: |
| 87 | if event.key == pygame.K_q: |
| 88 | game_over = True |
| 89 | game_close = False |
| 90 | |
| 91 | if event.key == pygame.K_SPACE: |
| 92 | gameLoop() |
| 93 | |
| 94 | # Keys to move the snake (W A S D) |
| 95 | for event in pygame.event.get(): |
| 96 | if event.type == pygame.QUIT: |
| 97 | game_over = True |
| 98 | |
| 99 | if event.type == pygame.KEYDOWN: |
| 100 | if event.key == pygame.K_a: |
| 101 | x1_change = -snake_block |
| 102 | y1_change = 0 |
| 103 | print("Left") |
| 104 | |
| 105 | elif event.key == pygame.K_d: |
| 106 | x1_change = snake_block |
| 107 | y1_change = 0 |
| 108 | print("Right") |
| 109 | |