Game Development Reference
In-Depth Information
18 - Collision Detection and Input
15. BLACK = (0, 0, 0)
16. GREEN = (0, 255, 0)
17. WHITE = (255, 255, 255)
18.
19. # set up the player and food data structure
20. foodCounter = 0
21. NEWFOOD = 40
22. FOODSIZE = 20
23. player = pygame.Rect(300, 100, 50, 50)
24. foods = []
25. for i in range(20):
26. foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH
- FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE),
FOODSIZE, FOODSIZE))
27.
28. # set up movement variables
29. moveLeft = False
30. moveRight = False
31. moveUp = False
32. moveDown = False
33.
34. MOVESPEED = 6
35.
36.
37. # run the game loop
38. while True:
39. # check for events
40. for event in pygame.event.get():
41. if event.type == QUIT:
42. pygame.quit()
43. sys.exit()
44. if event.type == KEYDOWN:
45. # change the keyboard variables
46. if event.key == K_LEFT or event.key == ord
('a'):
47. moveRight = False
48. moveLeft = True
49. if event.key == K_RIGHT or event.key == ord
('d'):
50. moveLeft = False
51. moveRight = True
52. if event.key == K_UP or event.key == ord('w'):
53. moveDown = False
54. moveUp = True
55. if event.key == K_DOWN or event.key == ord
('s'):
56. moveUp = False
57. moveDown = True
58. if event.type == KEYUP:
59. if event.key == K_ESCAPE:
60. pygame.quit()
61. sys.exit()
62. if event.key == K_LEFT or event.key == ord
('a'):
Search WWH ::




Custom Search