Implemented basic gameplay. Assorted fixes to Omni...Actor.

This commit is contained in:
2013-01-21 20:35:30 -04:30
parent 25f27ed5e9
commit 1f9271dcac
3 changed files with 111 additions and 36 deletions

View File

@@ -83,7 +83,7 @@ class BaseActor(pygame.sprite.Sprite):
self.color = (red, green, blue)
def get_position(self):
return self.position
return list(self.position)
def set_position(self, new_pos):
self.position = list(new_pos)
@@ -244,6 +244,9 @@ class OmnidirectionalActor(BaseActor):
self.constraint_max_x = 1024
self.constraint_max_y = 768
self.acc_fract_x = ((0.9 * float(pygame.display.Info().current_w)) / 1024.0)
self.acc_fract_y = ((0.9 * float(pygame.display.Info().current_h)) / 768.0)
def is_moving(self):
return self.moving
@@ -277,8 +280,8 @@ class OmnidirectionalActor(BaseActor):
if self.moving:
direction = math_utils.angle_to_vector(self.angle)
self.velocity[0] += direction[0] * 0.9
self.velocity[1] += direction[1] * 0.9
self.velocity[0] += direction[0] * self.acc_fract_x
self.velocity[1] += direction[1] * self.acc_fract_y
self.position[0] += (self.velocity[0] * delta_t) * (self.frame_rate / 1000)
self.position[1] += (self.velocity[1] * delta_t) * (self.frame_rate / 1000)

115
ingame.py
View File

@@ -27,6 +27,11 @@ class InGameState(BaseState):
self.cursor_y = 0
self.user_click = False
self.time_left = 190
self.then = pygame.time.get_ticks()
self.wave = 0
self.done = False
self.bckg_x = 0
self.bckg_y = 0
@@ -43,7 +48,7 @@ class InGameState(BaseState):
# Center the player.
self.player.set_position([bg_w // 2, bg_h // 2])
constraints = [int((95.0 * float(pygame.display.Info().current_w)) / 1024.0),
self.constraints = [int((95.0 * float(pygame.display.Info().current_w)) / 1024.0),
bg_w - int((95.0 * float(pygame.display.Info().current_w)) / 1024.0),
int((155.0 * float(pygame.display.Info().current_h)) / 768.0),
bg_h - int((155.0 * float(pygame.display.Info().current_h)) / 768.0)]
@@ -54,7 +59,7 @@ class InGameState(BaseState):
# Create the walls for the top and the bottom (the same for both).
bg_h = int((80.0 * float(pygame.display.Info().current_h)) / 768.0)
walls_top = background.TiledBackground(bg_w, bg_h, 'gfx/Pared.png')
bg_y = 1024 - int((80.0 * float(pygame.display.Info().current_h)) / 768.0)
bg_y = self.background.get_height() - int((80.0 * float(pygame.display.Info().current_h)) / 768.0)
# Create the left walls.
bg_h = int(float(pygame.display.Info().current_h * 1024) / 768.0)
@@ -64,7 +69,7 @@ class InGameState(BaseState):
# Create the right walls.
walls_right = background.TiledBackground(-1, bg_h, 'gfx/Pared3.png')
_x = 1280 - int((40.0 * float(pygame.display.Info().current_w)) / 1024.0)
_x = self.background.get_width() - int((40.0 * float(pygame.display.Info().current_w)) / 1024.0)
walls_right.set_position((_x, _y))
# Build the background image.
@@ -82,13 +87,51 @@ class InGameState(BaseState):
self.bckg_x -= dist_x
self.bckg_y -= dist_y
self.player.set_constraints(constraints)
self.player.set_constraints(self.constraints)
self.cursor_x = self.screen_center[0]
self.cursor_y = self.screen_center[1]
self.vec_1 = (float(pygame.display.Info().current_w) - float(self.screen_center[0]), 0.0)
self.vec_1 = math_utils.normalize_vector_2D(self.vec_1)
font_size = 22
screen_prop = (float(font_size) / 768.0)
screen_fract = (float(pygame.display.Info().current_h) * screen_prop) / 768.0
scale_factor = screen_fract / screen_prop
self.font = pygame.font.Font('font/ProfaisalEliteRiqa/Profaisal-EliteRiqaV1.0.ttf', int(font_size * scale_factor))
self.score_text = self.font.render("Puntos: " + str(1000), True, (0, 0, 0))
self.time_text = self.font.render("Tiempo: " + str(190), True, (0, 0, 0))
self.wave_text = self.font.render("Oleada: " + str(999), True, (0, 0, 0))
text_w = max(self.font.size("Puntos: " + str(1000))[0],
max(self.font.size("Tiempo: " + str(190))[0], self.font.size("Oleada: " + str(999))[0])) + 10
self.text_h = (3 * self.font.size("Puntos: " + str(1000))[1]) + 20
self.text_box = pygame.Surface((text_w, self.text_h))
self.text_h = self.font.size("Puntos: " + str(1000))[1]
self.text_box.set_alpha(128 + 64 + 32)
def recenter_view(self):
# Reset the view.
self.bckg_x = 0
self.bckg_y = 0
# Get the manhattan distance between the screen center and the player.
p_pos = self.player.get_position()
(dist_x, dist_y) = (self.screen_center[0] - p_pos[0], self.screen_center[1] - p_pos[1])
# Center the view on the player.
self.bckg_x += dist_x
self.bckg_y += dist_y
# Make sure the background is always inside the screen.
if self.bckg_y > 0:
self.bckg_y = 0
if self.bckg_x > 0:
self.bckg_x = 0
if self.bckg_y + self.background.get_height() < pygame.display.Info().current_h:
self.bckg_y += pygame.display.Info().current_h - (self.bckg_y + self.background.get_height())
if self.bckg_x + self.background.get_width() < pygame.display.Info().current_w:
self.bckg_x += pygame.display.Info().current_w - (self.bckg_x + self.background.get_width())
def input(self):
for event in pygame.event.get():
if android:
@@ -118,33 +161,53 @@ class InGameState(BaseState):
if self.next_transition != VALID_STATES['STAY']:
self.next_transition = VALID_STATES['STAY']
self.player.reset_then()
self.then = pygame.time.get_ticks()
self.done = not player.PLAYERS[1].is_alive()
now = pygame.time.get_ticks()
delta_t = now - self.then
if delta_t >= 1000:
self.time_left -= delta_t // 1000
self.then = now
if self.time_left <= 0:
player.PLAYERS[1].kill()
if not self.done:
if self.cursor_x != self.screen_center[0] or self.cursor_y != self.screen_center[1]:
#p_pos = self.player.get_position()
#(dist_x, dist_y) = (self.screen_center[0] - p_pos[0], self.screen_center[1] - p_pos[1])
#self.cursor_x += dist_x
#self.cursor_y += dist_y
#vec_2 = (float(self.cursor_x) - float(p_pos[0]), float(self.cursor_y) - float(p_pos[1]))
vec_2 = (float(self.cursor_x) - float(self.screen_center[0]), float(self.cursor_y) - float(self.screen_center[1]))
vec_2 = math_utils.normalize_vector_2D(vec_2)
self.player.set_angle(math_utils.angle_vectors_2D(self.vec_1, vec_2))
self.player.update()
# Reset the view.
self.bckg_x = 0
self.bckg_y = 0
# Get the manhattan distance between the screen center and the player.
p_pos = self.player.get_position()
(dist_x, dist_y) = (math.fabs(self.screen_center[0] - p_pos[0]), math.fabs(self.screen_center[1] - p_pos[1]))
# Center the view on the player.
self.bckg_x -= dist_x
self.bckg_y -= dist_y
self.score_text = self.font.render("Puntos: " + str(player.PLAYERS[1].get_score()), True, (0, 0, 0))
if self.time_left > 30:
self.time_text = self.font.render("Tiempo: " + str(self.time_left), True, (0, 0, 0))
else:
self.time_text = self.font.render("Tiempo: " + str(max(self.time_left, 0)), True, (255, 0, 0))
self.wave_text = self.font.render("Oleada: " + str(self.wave), True, (0, 0, 0))
# Make sure the background is always inside the screen.
if self.bckg_y > 0:
self.bckg_y = 0
if self.bckg_x > 0:
self.bckg_x = 0
if self.bckg_y + self.background.get_height() < pygame.display.Info().current_h:
self.bckg_y += pygame.display.Info().current_h - (self.bckg_y + self.background.get_height())
if self.bckg_x + self.background.get_width() < pygame.display.Info().current_w:
self.bckg_x += pygame.display.Info().current_w - (self.bckg_x + self.background.get_width())
self.recenter_view()
elif self.time_left < -3:
self.time_left = 190
self.wave = 0
self.player.set_angle(90)
self.player.set_velocity([0, 0])
self.user_click = False
self.done = False
self.player.stop()
bg_w = int(float(pygame.display.Info().current_w * 1280) / 1024.0)
bg_h = int(float(pygame.display.Info().current_h * 1024) / 768.0)
self.player.set_position([bg_w // 2, bg_h // 2])
player.PLAYERS[1].revive()
self.next_transition = VALID_STATES['SCORE']
self.cursor_x = self.screen_center[0]
self.cursor_y = self.screen_center[1]
@@ -152,9 +215,17 @@ class InGameState(BaseState):
return self.next_transition
def render(self, canvas):
#canvas.fill(self.background_color)
self.game_area.blit(self.background, (0, 0))
# Blit everything to the bacground.
if player.PLAYERS[1].is_alive():
self.player.draw(self.game_area)
self.text_box.fill((128, 128, 128))
self.text_box.blit(self.score_text, (5, 5))
self.text_box.blit(self.time_text, (5, 10 + self.text_h))
self.text_box.blit(self.wave_text, (5, 15 + (2 * self.text_h)))
canvas.blit(self.game_area, (self.bckg_x, self.bckg_y))
canvas.blit(self.text_box, (5, 5))

View File

@@ -122,6 +122,7 @@ class ScoreState(BaseState):
score = (str(self.player_init[0] + self.player_init[1] + self.player_init[2]), player.PLAYERS[1].get_score())
database.cursor.execute('UPDATE score SET player_name = ?, score = ? WHERE _id IN (SELECT _id FROM score WHERE score IN (SELECT MIN(score) FROM score))', score)
database.scores.commit()
player.PLAYERS[1].reset_score()
# Don't forget to reset the initials list.
self.player_init = []
self.letter_index = 0