|
1 | 1 | import pygame |
2 | 2 |
|
| 3 | +# dimensions of the screen |
3 | 4 | screenWidth = 1000 |
4 | 5 | screenHeight = 600 |
| 6 | +screenBuffer = 60 |
| 7 | + |
5 | 8 |
|
6 | 9 | class Player(pygame.sprite.Sprite): |
7 | | - def __init__(self, x, y): |
| 10 | + def __init__(self, x, y): # position will be declared later when making the player object |
8 | 11 | super().__init__() |
| 12 | + # variables to calculate how far the player is moving |
9 | 13 | self.changeX = 0 |
10 | 14 | self.changeY = 0 |
11 | 15 | self.speed = 9 |
12 | | - self.width = 30 |
13 | | - self.height = 50 |
| 16 | + |
| 17 | + # here's where we set the sprite's image |
14 | 18 | self.image = pygame.image.load("resources/frogRight.png") |
15 | | - # self.image.fill((255, 0, 0)) |
16 | | - self.rect = self.image.get_rect() |
| 19 | + self.rect = self.image.get_rect() # player's 'hitbox' |
| 20 | + |
| 21 | + # setting the position of the player |
17 | 22 | self.rect.x = x |
18 | 23 | self.rect.y = y |
19 | | - self.level = None |
| 24 | + self.shifted = 0 |
20 | 25 |
|
21 | 26 | def right(self): |
22 | 27 | self.image = pygame.image.load("resources/frogRight.png") |
@@ -31,56 +36,93 @@ def stop(self): |
31 | 36 | # self.changeY = 0 |
32 | 37 |
|
33 | 38 | def jump(self): |
34 | | - if self.rect.bottom >= screenHeight: |
35 | | - self.changeY = -15 |
| 39 | + if self.rect.bottom >= screenHeight: # check if player is at the bottom of the screen |
| 40 | + self.changeY = -15 # if so, player is able to jump |
36 | 41 |
|
37 | 42 | def gravity(self): |
38 | | - self.changeY += 1 |
| 43 | + self.changeY += 1 # constantly add to player's downward speed |
39 | 44 |
|
40 | | - if self.rect.y >= screenHeight - self.rect.height and self.changeY >= 0: |
41 | | - self.changeY = 0 |
42 | | - self.rect.y = screenHeight - self.rect.height |
| 45 | + if self.rect.bottom >= screenHeight and self.changeY >= 0: # if player is at botto mof screen |
| 46 | + self.changeY = 0 # stop falling |
| 47 | + self.rect.bottom = screenHeight # set player position to bottom of screen in case it fell too far |
43 | 48 |
|
44 | 49 | def update(self): |
45 | | - self.gravity() |
| 50 | + self.gravity() # does everything that the gravity function does |
| 51 | + # update the player's position based on the player's speed |
46 | 52 | self.rect.x += self.changeX |
47 | 53 | self.rect.y += self.changeY |
48 | 54 |
|
49 | 55 |
|
50 | 56 | class Platform(pygame.sprite.Sprite): |
51 | | - def __init__(self, x, y, w, h): |
| 57 | + def __init__(self, x, y, w, h): # platform position and dimensions must be declared upon making the object |
52 | 58 | super().__init__() |
| 59 | + # setting the appearance of the platform |
53 | 60 | self.width = w |
54 | 61 | self.height = h |
55 | 62 | self.image = pygame.Surface([self.width, self.height]) |
56 | | - self.image.fill((255, 0, 0)) |
| 63 | + self.image.fill((255, 0, 0)) # it will be a generic block unless you change its image and fill |
57 | 64 | self.rect = self.image.get_rect() |
| 65 | + # setting the platform's position |
58 | 66 | self.rect.x = x |
59 | 67 | self.rect.y = y |
60 | 68 |
|
61 | 69 |
|
62 | | -class Level(): |
63 | | - def __init__(self): |
64 | | - self.platformList = pygame.sprite.Group() |
65 | | - self.shifted = 0 |
| 70 | +class Background(pygame.sprite.Sprite): |
| 71 | + def __init__(self, image_file): # image file name will be declared later when making the object |
| 72 | + super().__init__() |
| 73 | + self.image = pygame.image.load(image_file) |
| 74 | + self.rect = self.image.get_rect() |
| 75 | + self.rect.left = 0 |
| 76 | + self.rect.top = 0 |
| 77 | + |
| 78 | + |
| 79 | +class Cloud(pygame.sprite.Sprite): |
| 80 | + def __init__(self, image_file, x, y): # image file name will be declared later when making the object |
| 81 | + super().__init__() |
| 82 | + self.image = pygame.image.load(image_file) |
| 83 | + self.rect = self.image.get_rect() |
| 84 | + self.scaleFactor = 1.0/6 # the cloud images were huge, but we can scale them down |
| 85 | + self.image = pygame.transform.scale(self.image, ((int)(self.scaleFactor*self.rect.width), (int)(self.scaleFactor*self.rect.height))) |
| 86 | + # setting the cloud's position |
| 87 | + self.rect.left = x |
| 88 | + self.rect.top = y |
66 | 89 |
|
67 | 90 |
|
68 | 91 | def main(): |
69 | 92 | pygame.init() |
70 | 93 |
|
71 | | - #setting up the window |
| 94 | + # setting up the window |
72 | 95 | screen = pygame.display.set_mode((screenWidth, screenHeight)) |
73 | 96 | clock = pygame.time.Clock() |
74 | 97 | pygame.display.set_caption("Platformer") |
75 | 98 |
|
76 | | - level = 1 |
77 | | - player = Player(50, 50) |
| 99 | + # group of all the static cosmetic aspects such as the background |
| 100 | + artList = pygame.sprite.Group() |
| 101 | + background = Background("resources/sky.png") |
| 102 | + artList.add(background) |
| 103 | + |
| 104 | + # make a group to hold the clouds |
| 105 | + cloudList = pygame.sprite.Group() |
| 106 | + # make all the clouds |
| 107 | + cloud1 = Cloud("resources/cloud1.png", 145, 200) |
| 108 | + cloud2 = Cloud("resources/cloud2.png", 700, 80) |
| 109 | + cloud3 = Cloud("resources/cloud3.png", 600, 250) |
| 110 | + # add them to the group |
| 111 | + cloudList.add(cloud1) |
| 112 | + cloudList.add(cloud2) |
| 113 | + cloudList.add(cloud3) |
| 114 | + |
| 115 | + # group that contains the player's sprite |
78 | 116 | spriteList = pygame.sprite.Group() |
| 117 | + player = Player(50, 50) |
79 | 118 | spriteList.add(player) |
| 119 | + |
| 120 | + # group of all the platforms |
80 | 121 | platformList = pygame.sprite.Group() |
81 | 122 |
|
82 | 123 | done = False |
83 | 124 | while not done: |
| 125 | + preshift = player.shifted |
84 | 126 | for event in pygame.event.get(): |
85 | 127 | if event.type == pygame.QUIT: |
86 | 128 | done = True |
@@ -98,17 +140,38 @@ def main(): |
98 | 140 | player.stop() |
99 | 141 | if event.key == pygame.K_RIGHT and player.changeX > 0: |
100 | 142 | player.stop() |
101 | | - screen.fill((0,0,0)) |
| 143 | + |
| 144 | + if event.type == pygame.MOUSEBUTTONDOWN: |
| 145 | + print(pygame.mouse.get_pos()) |
| 146 | + |
| 147 | + # draw all the things |
| 148 | + # (NOTE: Order matters! The last thing drawn will appear in front of all the things before it) |
| 149 | + screen.fill((209, 255, 214)) |
| 150 | + artList.draw(screen) |
| 151 | + cloudList.draw(screen) |
102 | 152 | spriteList.update() |
103 | 153 | spriteList.draw(screen) |
104 | 154 | platformList.draw(screen) |
105 | 155 |
|
| 156 | + # scrolling effect |
| 157 | + if player.rect.right >= screenWidth-screenBuffer: # if player reaches right side of screen |
| 158 | + diff = player.rect.right - (screenWidth-screenBuffer) # calculate how far past screen buffer player has gone |
| 159 | + player.rect.right = (screenWidth-screenBuffer) # send em back |
| 160 | + for cloud in cloudList: # for all clouds in the group |
| 161 | + cloud.rect.x -= diff # shift them left |
| 162 | + if cloud.rect.right < 0: # if cloud is off screen |
| 163 | + cloud.rect.left = screenWidth # put it in a place where it will appear again |
| 164 | + if player.rect.left <= screenBuffer: # if player reaches left side of screen |
| 165 | + diff = screenBuffer - player.rect.left # calculate how far past screen buffer player has gone |
| 166 | + player.rect.left = screenBuffer # send em back |
| 167 | + for cloud in cloudList: # for all clouds in the group |
| 168 | + cloud.rect.x += diff # shift them right |
| 169 | + if cloud.rect.left > screenWidth: # if cloud is off screen |
| 170 | + cloud.rect.right = 0 # pt it in a place where it will appear again |
| 171 | + |
106 | 172 | clock.tick(60) |
107 | 173 | pygame.display.flip() |
108 | 174 |
|
109 | | - |
110 | 175 | pygame.quit() |
111 | 176 |
|
112 | | - |
113 | | -if __name__ == "__main__": |
114 | | - main() |
| 177 | +main() |