-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonGame.py
More file actions
135 lines (95 loc) · 2.8 KB
/
PythonGame.py
File metadata and controls
135 lines (95 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import pygame
import sys
pygame.init()
WIDTH, HEIGHT = 800, 600
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Platformer Game")
clock= pygame.time.Clock()
#colors
WHITE = (255,255,255)
BLACK= (0,0,0)
BLUE = (50,150,255)
GREEN = (50,200,50)
player = pygame.Rect(50,HEIGHT-70,50,50)
player_speed = 5
velocity_y = 0
gravity = .5
jump_strength = 12
on_ground = False
levels = [
[ # Level 1 platforms
pygame.Rect(0, HEIGHT - 20, WIDTH, 20),
pygame.Rect(200, HEIGHT - 120, 150, 20),
pygame.Rect(450, HEIGHT - 220, 150, 20),
pygame.Rect(650, HEIGHT - 320, 100, 20),
],
[ # Level 2 platforms
pygame.Rect(0, HEIGHT - 20, WIDTH, 20),
pygame.Rect(100, HEIGHT - 150, 120, 20),
pygame.Rect(350, HEIGHT - 250, 180, 20),
pygame.Rect(600, HEIGHT - 180, 120, 20),
pygame.Rect(700, HEIGHT - 350, 50, 20),
]
]
current_level = 0
platforms = levels[current_level]
def move_player(keys):
global velocity_y,on_ground,current_level,platforms
dx = 0
if keys[pygame.K_LEFT]or keys[pygame.K_a]:
dx = -player_speed
if keys[pygame.K_RIGHT]or keys[pygame.K_d]:
dx = player_speed
player.x += dx
for platform in platforms:
if player.colliderect(platform):
if dx > 0:
player.right=platform.left
if dx<0:
player.left = platform.right
if player.left >WIDTH:
if current_level < len(levels)-1:
current_level +=1
platforms = levels[current_level]
player.left = 0
elif player.right <0:
if current_level>0:
current_level -=1
platforms = levels[current_level]
player.right = WIDTH
velocity_y += gravity
player.y += velocity_y
on_ground = False
for platform in platforms:
if player.colliderect(platform):
if velocity_y>0:
player.bottom=platform.top
velocity_y = 0
on_ground = True
elif velocity_y <0:
player.top = platform.bottom
velocity_y = 0
def jump():
global velocity_y,on_ground
if on_ground:
velocity_y = -jump_strength
def draw():
screen.fill(WHITE)
for platform in platforms:
pygame.draw.rect(screen,GREEN, platform)
pygame.draw.rect(screen, BLUE, player)
pygame.display.flip()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP or event.key == pygame.K_w:
jump()
keys =pygame.key.get_pressed()
move_player(keys)
draw()
clock.tick(60)
pygame.quit()
sys.exit()