-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.py
95 lines (81 loc) · 3.16 KB
/
game.py
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
import sys, pygame, env, characters, cockroach
pygame.init()
fpsClock = pygame.time.Clock()
size = width, height = 1024, 768
screen = pygame.display.set_mode(size)
global worldX, worldY
worldX = 0
worldY = 0
#env.Hero is the class for the player-controlled character
hero = characters.Hero()
#platforms is a sprite group that contains all platforms
platforms = pygame.sprite.Group()
#dynamics is the sprite group that contains all dynamic elements in the game world, including enemies, mobile objects, and collectibles
dynamics = pygame.sprite.Group()
#background is the current background image
def paint():
screen.fill(16777215)
#otherScreen = pygame.Surface((3000, 1000))
global worldX, worldY
#prevent the world from scrolling out of screen
if worldX < 0:
worldX = 0
#set the rendering area for the rendering buffer to the area currently
#visible on screen
screen.blit(background, pygame.rect.Rect(-worldX, -worldY, width, height))
#draw sprites
hero.draw(screen, (-worldX, -worldY))
for dynamic in dynamics:
dynamic.draw(screen, (-worldX, -worldY))
for platform in platforms:
platform.draw(screen, (-worldX, -worldY))
# render the game world to the screen
pygame.display.flip()
def getInput():
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
keys = pygame.key.get_pressed()
hero.move(keys)
def update():
hero.collide(platforms, dynamics)
hero.update()
dynamics.update(hero, platforms)
#camera mechanics:
global worldX, worldY
if (hero.rect.x - worldX > width - (width/3)):
worldX += hero.speed
if (hero.rect.x - worldX < width/3):
worldX -= hero.speed
if (hero.rect.y - worldY < (height/5)):
worldY -= 5
if (hero.rect.y - worldY > height - (height / 3)):
worldY += 5
def loadWorld(file):
f = open (file)
global background
background = pygame.image.load(f.readline().rstrip()).convert()
enemies = False
for line in f:
if (line[0] == 'E'):
enemies = True
elif line[0] != '#':
l = line.split(",")
if (enemies):
dynamics.add(cockroach.Cockroach(int(l[1]),
int(l[2])))
else:
coords = []
for x in l:
coords.append(int(x))
try:
platforms.add(env.Platform(coords[0],
coords[1],
coords[2], coords[3]))
except:
pass
loadWorld('room.lvl')
while 1:
getInput()
update()
paint()
fpsClock.tick(60)