-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
177 lines (149 loc) · 5.09 KB
/
main.py
File metadata and controls
177 lines (149 loc) · 5.09 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import pygame
import sys
import os
from pygame.locals import *
# Initialize pygame
pygame.init()
# Constants
SCREEN_WIDTH = 500
SCREEN_HEIGHT = 500
FPS = 60
# Colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
BLUE = (0, 0, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
# Set up the display
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("Platform Game")
clock = pygame.time.Clock()
# Load images
def load_image(name, colorkey=None):
fullname = os.path.join('assets', name)
try:
image = pygame.image.load(fullname)
except pygame.error as message:
print('Cannot load image:', name)
raise SystemExit(message)
image = image.convert_alpha()
if colorkey is not None:
if colorkey == -1:
colorkey = image.get_at((0, 0))
image.set_colorkey(colorkey, pygame.RLEACCEL)
return image, image.get_rect()
# Player class
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
# Temporary rectangle for the player
self.image = pygame.Surface((30, 50))
self.image.fill(BLUE)
self.rect = self.image.get_rect()
self.rect.x = 100
self.rect.y = 300
self.velocity_y = 0
self.jumping = False
self.on_ground = False
def update(self, platforms):
# Gravity
self.velocity_y += 0.5
if self.velocity_y > 10:
self.velocity_y = 10
# Move the player
self.rect.y += self.velocity_y
# Check for collisions with platforms
self.on_ground = False
for platform in platforms:
if self.rect.colliderect(platform.rect):
if self.velocity_y > 0: # Falling
self.rect.bottom = platform.rect.top
self.velocity_y = 0
self.on_ground = True
self.jumping = False
elif self.velocity_y < 0: # Jumping
self.rect.top = platform.rect.bottom
self.velocity_y = 0
def jump(self):
if self.on_ground and not self.jumping:
self.jumping = True
self.velocity_y = -12
self.on_ground = False
def move_left(self):
self.rect.x -= 5
if self.rect.left < 0:
self.rect.left = 0
def move_right(self):
self.rect.x += 5
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
# Platform class
class Platform(pygame.sprite.Sprite):
def __init__(self, x, y, width, height):
super().__init__()
self.image = pygame.Surface((width, height))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
# Game class
class Game:
def __init__(self):
self.player = Player()
self.all_sprites = pygame.sprite.Group()
self.platforms = pygame.sprite.Group()
# Add player to sprites
self.all_sprites.add(self.player)
# Create platforms
self.create_platforms()
def create_platforms(self):
# Ground
ground = Platform(0, SCREEN_HEIGHT - 40, SCREEN_WIDTH, 40)
self.all_sprites.add(ground)
self.platforms.add(ground)
# Platforms
platform_positions = [
(100, 400, 100, 20),
(300, 350, 100, 20),
(150, 300, 100, 20),
(400, 250, 100, 20),
(200, 200, 100, 20)
]
for x, y, width, height in platform_positions:
platform = Platform(x, y, width, height)
self.all_sprites.add(platform)
self.platforms.add(platform)
def run(self):
running = True
while running:
# Keep loop running at the right speed
clock.tick(FPS)
# Process input (events)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.player.jump()
# Check keys pressed
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.player.move_left()
if keys[pygame.K_RIGHT]:
self.player.move_right()
# Update
self.player.update(self.platforms)
# Draw / render
screen.fill(BLACK)
self.all_sprites.draw(screen)
# Flip the display
pygame.display.flip()
pygame.quit()
sys.exit()
# Create and run the game
if __name__ == "__main__":
# Create assets directory if it doesn't exist
if not os.path.exists('assets'):
os.makedirs('assets')
game = Game()
game.run()