Game Development Reference
In-Depth Information
18 - Collision Detection and Input
28. WINDOWHEIGHT = 400
29. windowSurface = pygame.display.set_mode((WINDOWWIDTH,
WINDOWHEIGHT), 0, 32)
30. pygame.display.set_caption('Collision Detection')
31.
32. # set up direction variables
33. DOWNLEFT = 1
34. DOWNRIGHT = 3
35. UPLEFT = 7
36. UPRIGHT = 9
37.
38. MOVESPEED = 4
39.
40. # set up the colors
41. BLACK = (0, 0, 0)
42. GREEN = (0, 255, 0)
43. WHITE = (255, 255, 255)
44.
45. # set up the bouncer and food data structures
46. foodCounter = 0
47. NEWFOOD = 40
48. FOODSIZE = 20
49. bouncer = {'rect':pygame.Rect(300, 100, 50, 50),
'dir':UPLEFT}
50. foods = []
51. for i in range(20):
52. foods.append(pygame.Rect(random.randint(0, WINDOWWIDTH
- FOODSIZE), random.randint(0, WINDOWHEIGHT - FOODSIZE),
FOODSIZE, FOODSIZE))
53.
54. # run the game loop
55. while True:
56. # check for the QUIT event
57. for event in pygame.event.get():
58. if event.type == QUIT:
59. pygame.quit()
60. sys.exit()
61.
62. foodCounter += 1
63. if foodCounter >= NEWFOOD:
64. # add new food
65. foodCounter = 0
66. foods.append(pygame.Rect(random.randint(0,
WINDOWWIDTH - FOODSIZE), random.randint(0, WINDOWHEIGHT -
FOODSIZE), FOODSIZE, FOODSIZE))
67.
68. # draw the black background onto the surface
69. windowSurface.fill(BLACK)
70.
71. # move the bouncer data structure
72. if bouncer['dir'] == DOWNLEFT:
73. bouncer['rect'].left -= MOVESPEED
74. bouncer['rect'].top += MOVESPEED
75. if bouncer['dir'] == DOWNRIGHT:
Search WWH ::




Custom Search