Game Development Reference
In-Depth Information
We want to update the position of each block, so we must loop through the
rectangles list and perform the same code on each block's data structure. Inside the
loop, we will refer to the current block as simply r so it will be easy to type.
45. # move the block data structure
46. if b['dir'] == DOWNLEFT:
47. b['rect'].left -= MOVESPEED
48. b['rect'].top += MOVESPEED
49. if b['dir'] == DOWNRIGHT:
50. b['rect'].left += MOVESPEED
51. b['rect'].top += MOVESPEED
52. if b['dir'] == UPLEFT:
53. b['rect'].left -= MOVESPEED
54. b['rect'].top -= MOVESPEED
55. if b['dir'] == UPRIGHT:
56. b['rect'].left += MOVESPEED
57. b['rect'].top -= MOVESPEED
The new value that we want to set the left and top attributes to depends on the
direction the block is moving. Remember that the X-coordinates start at 0 on the very left
edge of the window, and increase as you go right. The Y-coordinates start at 0 on the very
top of the window, and increase as you go down. So if the direction of the block (which,
remember, is stored in the 'dir' key) is either DOWNLEFT or DOWNRIGHT , we want to
increase the top attribute. If the direction is UPLEFT or UPRIGHT , we want to decrease
the top attribute.
If the direction of the block is DOWNRIGHT or UPRIGHT , we want to increase the left
attribute. If the direction is DOWNLEFT or UPLEFT , we want to decrease the left
attribute.
We could have also modified right instead of the left attribute, or the bottom
attribute instead of the top attribute, because Pygame will update the Rect object either
way. Either way, we want to change the value of these attributes by the integer stored in
MOVESPEED , which stores how many pixels over we will move the block.
Checking if the Block has Bounced
59. # check if the block has move out of the window
60. if b['rect'].top < 0:
61. # block has moved past the top
62. if b['dir'] == UPLEFT:
63. b['dir'] = DOWNLEFT
64. if b['dir'] == UPRIGHT:
65. b['dir'] = DOWNRIGHT
After we have moved the block, we want to check if the block has gone past the edge of
Search WWH ::




Custom Search