-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.py
More file actions
72 lines (50 loc) · 2.18 KB
/
game.py
File metadata and controls
72 lines (50 loc) · 2.18 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
import pygame
from sys import exit
from os import listdir
from scripts.entities import PhysicsEntity
from scripts.utils import load_image, load_images
from scripts.tilemap import Tilemap
class Game:
def __init__(self, WIDTH = 640, HEIGHT = 480, CAPTION = "Ninja Game") -> None:
pygame.init()
self.WIDTH = WIDTH
self.HEIGHT = HEIGHT
pygame.display.set_caption(CAPTION)
self.screen = pygame.display.set_mode((WIDTH, HEIGHT))
self.display = pygame.Surface((self.WIDTH/2, self.HEIGHT/2))
self.clock = pygame.time.Clock()
self.movement = [False, False]
self.assets = {
'player' : load_image("entities/player.png")
}
for folder in listdir("data\images\\tiles"):
self.assets[folder] = load_images("tiles\\" + folder)
self.player = PhysicsEntity(self, 'player', (50, 50), (8, 15))
self.tilemap = Tilemap(self, 16)
def run(self) -> None:
while True:
self.display.fill((14,219,248))
self.tilemap.render(self.display)
self.player.update(self.tilemap, (self.movement[1] - self.movement[0], 0))
self.player.render(self.display)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.movement[0] = True
if event.key == pygame.K_d:
self.movement[1] = True
if event.key == pygame.K_w:
self.player.velocity[1] = -3
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
self.movement[0] = False
if event.key == pygame.K_d:
self.movement[1] = False
self.screen.blit(pygame.transform.scale(self.display, self.screen.get_size()), (0,0))
pygame.display.update()
self.clock.tick(60)
if __name__ == "__main__":
Game(640, 480, "Ninja Game").run()