GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — added scrolling feature · codingandcommunity/hackathon@ed982f6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • platformer
    • platformer.py
    • resources
      • cloud1.png
      • cloud2.png
      • cloud3.png
      • sky.png
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎platformer/platformer.py‎

Copy file name to clipboard
+90-27Lines changed: 90 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import pygame
22

3+
# dimensions of the screen
34
screenWidth = 1000
45
screenHeight = 600
6+
screenBuffer = 60
7+
58

69
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
811
super().__init__()
12+
# variables to calculate how far the player is moving
913
self.changeX = 0
1014
self.changeY = 0
1115
self.speed = 9
12-
self.width = 30
13-
self.height = 50
16+
17+
# here's where we set the sprite's image
1418
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
1722
self.rect.x = x
1823
self.rect.y = y
19-
self.level = None
24+
self.shifted = 0
2025

2126
def right(self):
2227
self.image = pygame.image.load("resources/frogRight.png")
@@ -31,56 +36,93 @@ def stop(self):
3136
# self.changeY = 0
3237

3338
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
3641

3742
def gravity(self):
38-
self.changeY += 1
43+
self.changeY += 1 # constantly add to player's downward speed
3944

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
4348

4449
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
4652
self.rect.x += self.changeX
4753
self.rect.y += self.changeY
4854

4955

5056
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
5258
super().__init__()
59+
# setting the appearance of the platform
5360
self.width = w
5461
self.height = h
5562
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
5764
self.rect = self.image.get_rect()
65+
# setting the platform's position
5866
self.rect.x = x
5967
self.rect.y = y
6068

6169

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
6689

6790

6891
def main():
6992
pygame.init()
7093

71-
#setting up the window
94+
# setting up the window
7295
screen = pygame.display.set_mode((screenWidth, screenHeight))
7396
clock = pygame.time.Clock()
7497
pygame.display.set_caption("Platformer")
7598

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
78116
spriteList = pygame.sprite.Group()
117+
player = Player(50, 50)
79118
spriteList.add(player)
119+
120+
# group of all the platforms
80121
platformList = pygame.sprite.Group()
81122

82123
done = False
83124
while not done:
125+
preshift = player.shifted
84126
for event in pygame.event.get():
85127
if event.type == pygame.QUIT:
86128
done = True
@@ -98,17 +140,38 @@ def main():
98140
player.stop()
99141
if event.key == pygame.K_RIGHT and player.changeX > 0:
100142
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)
102152
spriteList.update()
103153
spriteList.draw(screen)
104154
platformList.draw(screen)
105155

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+
106172
clock.tick(60)
107173
pygame.display.flip()
108174

109-
110175
pygame.quit()
111176

112-
113-
if __name__ == "__main__":
114-
main()
177+
main()
Collapse file

‎platformer/resources/cloud1.png‎

Copy file name to clipboard
15.9 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎platformer/resources/cloud2.png‎

Copy file name to clipboard
21.8 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎platformer/resources/cloud3.png‎

Copy file name to clipboard
28.6 KB
  • Display the source diff
  • Display the rich diff
Loading
Collapse file

‎platformer/resources/sky.png‎

Copy file name to clipboard
142 KB
  • Display the source diff
  • Display the rich diff
Loading

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.