-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrun_game.py
146 lines (138 loc) · 6 KB
/
run_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
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
'''
run_game.py
Implements the game logic, providing the Game class
'''
import pygame
from globalvalues import Renderable,Options,GlobalObjects,Input,Collison
"""
Takes apart a level object and renders the necessary components for
playing the game
"""
class Game(Renderable):
"""
Sets up the object
"""
def __init__(self,level):
Renderable.__init__(self)
self.world=level.world
self.background=level.background
self.bkd_pos = 0
self.objectdict=level.objectdict
self.complete = level.complete
self.size = level.size
self.keyinput = 0
self.tile_bkd()
if self.objectdict:
for i in self.objectdict.iterkeys():
self.objectdict[i].draw_on(self.background,i)
self.player = GlobalObjects.playercharacters[self.world]
self.is_jumping = False
"""
Tiles the background so that it encompasses the entire level, or just paints
black if the backgrounds option is false
"""
def tile_bkd(self):
levelbkgd = pygame.Surface(self.size)
if Options.backgrounds:
print "tiling"
import math
print self.background.get_width()
htiles=int(math.ceil(float(self.size[0])/float(self.background.get_width())))
print htiles
vtiles=int(math.ceil(self.size[1]/self.background.get_height()))
for htile in range(htiles):
print htile
levelbkgd.blit(self.background,[htile*self.background.get_width(),0])
for vtile in range(vtiles):
levelbkgd.blit(self.background,(0,vtile*self.background.get_height()))
self.background = levelbkgd
"""
Processes and filters keyboard input.
The filter assumes that the events will be processed in order, and
behaves according to these rules:
if KEYDOWN : KEYDOWN
if KEYUP : KEYUP
if KEYDOWN+KEYUP : KEYDOWN FOR 1 FRAME
"""
def process_events(self,events):
keydowns=0
for event in events:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.keyinput |= Input.up
keydowns |= Input.up
elif event.key == pygame.K_DOWN:
self.keyinput |= Input.down
keydowns |= Input.down
elif event.key == pygame.K_LEFT:
self.keyinput |= Input.left
keydowns |= Input.left
elif event.key == pygame.K_RIGHT:
self.keyinput |= Input.right
keydowns |= Input.right
elif event.type == pygame.KEYUP:
if event.key == pygame.K_UP:
self.keyinput &= ~Input.up
elif event.key == pygame.K_DOWN:
self.keyinput &= ~Input.down
elif event.key == pygame.K_LEFT:
self.keyinput &= ~Input.left
elif event.key == pygame.K_RIGHT:
self.keyinput &= ~Input.right
return self.keyinput | keydowns
"""
Draws the level, processing input and checking collisions
"""
def draw(self, events):
self.screen.blit(self.background,[self.bkd_pos,0])
input=self.process_events(events)
for i in self.objectdict.iterkeys():
self.player.collide(self.objectdict[i])
#collisions =0
#for i in self.objectdict.iterkeys():
#collisions |= self.player.collides_dir(self.objectdict[i])
#if collisions & Collison.collides == Collison.collides:
#if collisions & Collison.bottom == Collison.bottom:
##print "collide"
#is_jumping = False
#if self.player.velocity[1] < 0: self.player.velocity[1] = 0
#if self.player.acceleration[1] < 0: self.player.velocity[1] = 0
#if collisions & Collison.top == Collison.top:
#if self.player.velocity[1] > 0: self.player.velocity[1] = 0
#if self.player.acceleration[1] > 0: self.player.velocity[1] = 0
#Gotta deal with the 2-keys pressed scenario somehow.
if self.player.position[0] < self.screen.get_width()/2 or self.player.velocity[0] < 0:
self.player.draw_on(self.screen, (self.player.position[0] +
(self.player.acceleration[0]*self.player.velocity[0]),
self.player.position[1] +
(self.player.acceleration[1]*self.player.velocity[1])))
else:
#scrolls background if player is moving past the midpoint on the screen
self.player.draw_on(self.screen, (self.player.position[0], self.player.position[1] + (self.player.acceleration[1]*self.player.velocity[1])))
self.bkd_pos = self.bkd_pos - (self.player.acceleration[0]*self.player.velocity[0])
if self.is_jumping:
time = pygame.time.get_ticks() - self.start_jump
if time < 500:
self.player.draw_on(self.screen, (self.player.position[0], self.start_jump_pos - ((-10*(2*(time/500.0) - 1)*(2*(time/500.0) - 1) + 10)*20)))
else:
self.player.draw_on(self.screen, (self.player.position[0], self.start_jump_pos))
self.is_jumping = False
if input & Input.up == Input.up:
if not self.is_jumping:
self.is_jumping = True
self.start_jump = pygame.time.get_ticks()
self.start_jump_pos = self.player.position[1]
pass
if input & Input.left == Input.left:
self.player.velocity[0] = -1
self.player.acceleration[0] = 10
pass
if input & Input.down == Input.down:
#crouch?
pass
if input & Input.right == Input.right:
self.player.velocity[0] = 1
self.player.acceleration[0] = 10
pass
if input & Input.left != Input.left and input & Input.right != Input.right:
self.player.velocity[0] = 0