Game Development Reference
In-Depth Information
63. moveLeft = False
64. if event.key == K_RIGHT or event.key == ord
('d'):
65. moveRight = False
66. if event.key == K_UP or event.key == ord('w'):
67. moveUp = False
68. if event.key == K_DOWN or event.key == ord
('s'):
69. moveDown = False
70. if event.key == ord('x'):
71. player.top = random.randint(0,
WINDOWHEIGHT - player.height)
72. player.left = random.randint(0,
WINDOWWIDTH - player.width)
73.
74. if event.type == MOUSEBUTTONUP:
75. foods.append(pygame.Rect(event.pos[0],
event.pos[1], FOODSIZE, FOODSIZE))
76.
77. foodCounter += 1
78. if foodCounter >= NEWFOOD:
79. # add new food
80. foodCounter = 0
81. foods.append(pygame.Rect(random.randint(0,
WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT -
FOODSIZE), FOODSIZE, FOODSIZE))
82.
83. # draw the black background onto the surface
84. windowSurface.fill(BLACK)
85.
86. # move the player
87. if moveDown and player.bottom < WINDOWHEIGHT:
88. player.top += MOVESPEED
89. if moveUp and player.top > 0:
90. player.top -= MOVESPEED
91. if moveLeft and player.left > 0:
92. player.left -= MOVESPEED
93. if moveRight and player.right < WINDOWWIDTH:
94. player.right += MOVESPEED
95.
96. # draw the player onto the surface
97. pygame.draw.rect(windowSurface, WHITE, player)
98.
99. # check if the player has intersected with any food
squares.
100. for food in foods[:]:
101. if player.colliderect(food):
102. foods.remove(food)
103.
104. # draw the food
105. for i in range(len(foods)):
106. pygame.draw.rect(windowSurface, GREEN, foods[i])
107.
108. # draw the window onto the screen
Search WWH ::




Custom Search