View difference between Paste ID: LyKTh5vu and ycaAApvU
SHOW: | | - or go back to the newest paste.
1
import pygame
2
import random
3
4
GRAVITY = 1
5
PLAYER_VELOCITY = 5
6
JUMP_POWER = 20
7
HEIGHT = 901
8
WIDTH = 500
9
backgroundColour = (0,215,62)
10
platformColour = (0,255,0)
11
# self.image = substitute for actual image
12
class Platform(pygame.sprite.Sprite):
13
    def __init__(self, y,pl = 100):
14
        super().__init__()
15
        platheight = 10
16
        platlength = pl
17
        self.image = pygame.Surface((platlength,platheight))
18
        self.image.fill(platformColour)
19
        self.rect = self.image.get_rect()  # <-- makes rectangle 
20
        self.rect.left = (random.randrange(0,WIDTH - platlength))
21
        self.rect.centery = y
22
23
class player(pygame.sprite.Sprite):
24
    def __init__(self):
25
        super().__init__()
26
27
        self.image = pygame.Surface((50, 90))
28
        self.image.fill((130, 230, 99))
29
        self.rect = self.image.get_rect()
30
        self.rect.center = (485,550)
31
        self.dx = 0
32
        self.dy = 0
33
        self.in_air = True
34
        self.gravity = GRAVITY
35
36
    def update(self):
37
        self.dx = 0
38
        keys = pygame.key.get_pressed()
39
        if keys [pygame.K_a]:
40
            self.dx = -PLAYER_VELOCITY
41
        if keys [pygame.K_d]:
42
            self.dx = PLAYER_VELOCITY
43
        if keys [pygame.K_SPACE] and not self.in_air:
44
            self.jump()
45
        self.dy += self.gravity
46
        self.rect.move_ip(self.dx,self.dy)
47
48
    def jump(self):
49
        keys = pygame.key.get_pressed() #makes the key function
50
        if keys [pygame.K_SPACE]:
51
            self.dy = -JUMP_POWER
52
            self.in_air = True
53
            self.gravity = GRAVITY          
54
55
        
56
57
58
pygame.init()
59
window = pygame.display.set_mode((WIDTH,HEIGHT)) #makes window 
60
run = True
61
clock = pygame.time.Clock() # <-- makes a clock
62
elements = pygame.sprite.Group() #group where all objects are stored
63
platforms = pygame.sprite.Group() 
64
for x in range(5): #value of x will be 0 1 2 3 4 5
65
    p = Platform((x+1)*150)
66
    elements.add(p)
67
    platforms.add(p)
68
base = Platform(HEIGHT-20, WIDTH - 1)
69
elements.add(base) #adds base to element group
70
platforms.add(base) #adds to platforms
71
layer = player()
72
elements.add(layer)
73
while run:
74
    window.fill(backgroundColour)
75
    clock.tick(60) # <-- sets FPS to 60
76
    for event in pygame.event.get(): # <-- gives you all events
77
        if event.type == pygame.QUIT: # <-- if the event is that you close the program
78
            run = False # <--shuts down windowplatforms, F
79
    plat = pygame.sprite.spritecollideany(layer, platforms)
80
    if plat and layer.dy > 0 and layer.rect.centery <= plat.rect.top:
81
        layer.in_air = False
82
        layer.gravity = 0 # accleration
83
        layer.dy = 0 # velocity
84
        layer.rect.bottom = plat.rect.top
85
    else:
86
        layer.gravity = GRAVITY
87
    elements.update() # updates all objects in element group
88
    elements.draw(window) #places objects from elements group into window
89
    pygame.display.update()