MCPcopy Create free account
hub / github.com/ndleah/python-mini-project / gameLoop

Function gameLoop

Snake_game/main.py:52–159  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

50
51
52def 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

Callers 1

main.pyFile · 0.85

Calls 3

messageFunction · 0.85
scoreFunction · 0.85
snakeFunction · 0.85

Tested by

no test coverage detected