Added player and level and world objects.

This commit is contained in:
Sakse Dalum 2012-08-07 19:59:52 +02:00
parent c14c5e248e
commit dc5acdf4c6
7 changed files with 155 additions and 14 deletions

View File

@ -67,7 +67,7 @@ class Game(object):
t = pygame.time.get_ticks()
dt = 0
while self.running:
dt = t - pygame.time.get_ticks()
dt = float(pygame.time.get_ticks() - t) / 1000
t = pygame.time.get_ticks()
self.update(t, dt)
self.draw()
@ -76,6 +76,7 @@ class Game(object):
def goto_level(self, level):
self.level = level
self.objs.append(self.level)
def update(self, t, dt):

View File

@ -31,14 +31,14 @@ class GameMenu(object):
def __init__(self, game, img_dir, active=False, selection=0):
self.__dict__.update(locals())
self.menu_order = ['restart_level', 'quit']
self.menu = ['restart_level', 'quit']
self.load()
def load(self):
screen_size = self.game.window.get_size()
for item in self.menu_order:
for item in self.menu:
setattr(self, '%s_imgs' % item, [
pygame.image.load(os.path.join(self.img_dir,
'%s-%s.png' % (item, end)))
@ -59,26 +59,26 @@ class GameMenu(object):
if event.key == pygame.K_ESCAPE:
self.active = not self.active
if self.active:
if event.key == pygame.K_SPACE:
if self.menu_order[self.selection] == 'restart_level':
if event.key in [pygame.K_SPACE, pygame.K_RETURN]:
if self.menu[self.selection] == 'restart_level':
pass
if self.menu_order[self.selection] == 'quit':
if self.menu[self.selection] == 'quit':
self.game.stop()
if event.key == pygame.K_UP:
self.selection = max(self.selection - 1, 0)
if event.key == pygame.K_DOWN:
self.selection = min(self.selection + 1,
len(self.menu_order) - 1)
len(self.menu) - 1)
def draw(self, window):
if self.active:
screen_size = self.game.window.get_size()
for i in range(len(self.menu_order)):
for i in range(len(self.menu)):
s = i == self.selection
img = getattr(self, '%s_imgs' % self.menu_order[i])[s]
img = getattr(self, '%s_imgs' % self.menu[i])[s]
window.blit(img,
(int((screen_size[0] - img.get_size()[0]) / 2),
int(screen_size[1] / 2)
- (int(screen_size[1]*0.13)
* (len(self.menu_order) / 2 - i))))
* (len(self.menu) / 2 - i))))

View File

@ -25,5 +25,8 @@ A generic level.
"""
class Level(object):
def __init__(self, level_name):
self.__dict__.update(locals())
def update(self, e, t, dt):
pass
def draw(self, window):
pass

38
robotgame/level1.py Normal file
View File

@ -0,0 +1,38 @@
# This file is part of ROBOTGAME
#
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ROBOTGAME. If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# level1.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
The first level.
"""
import level
import player
class Level1(level.Level):
def __init__(self):
self.player = player.Player(200, 200)
def update(self, e, t, dt):
self.player.update(e, t, dt)
def draw(self, window):
self.player.draw(window)

View File

@ -27,7 +27,7 @@ The main menu.
import os
import pygame
import level
import level1
import game_menu
class MainMenu(object):
@ -59,7 +59,7 @@ class MainMenu(object):
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.game.goto_level(level.Level("landing"))
self.game.goto_level(level1.Level1())
self.game.objs.remove(self)
self.game.objs.append(game_menu.GameMenu(self.game,
self.img_dir))

47
robotgame/player.py Normal file
View File

@ -0,0 +1,47 @@
# This file is part of ROBOTGAME
#
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ROBOTGAME. If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# player.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
A generic level.
"""
import pygame
import worldobject
class Player(worldobject.WorldObject):
def update(self, e, t, dt):
worldobject.WorldObject.update(self, e, t, dt)
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
self.move(0, -1)
if event.key == pygame.K_DOWN:
self.move(0, 1)
if event.key == pygame.K_RIGHT:
self.move(1, 0)
if event.key == pygame.K_LEFT:
self.move(-1, 0)
def draw(self, window):
pygame.draw.circle(window, (255, 255, 255), (self.x, self.y), 20)

52
robotgame/worldobject.py Normal file
View File

@ -0,0 +1,52 @@
# This file is part of ROBOTGAME
#
# ROBOTGAME is free software: you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation, either version 3 of the License, or (at your option) any later
# version.
#
# ROBOTGAME is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# ROBOTGAME. If not, see <http://www.gnu.org/licenses/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# worldobject.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
A generic world object.
"""
class WorldObject(object):
def __init__(self, x, y, speed=5, tile_x=64, tile_y=48):
self.__dict__.update(locals())
self.move_x, self.move_y = self.x, self.y
def move(self, move_x, move_y):
if self.move_x == self.x and self.move_y == self.y:
self.move_x += move_x * self.tile_x
self.move_y += move_y * self.tile_y
def update(self, e, t, dt):
if self.x > self.move_x:
self.x -= min(self.speed * dt * self.tile_x,
abs(self.x - self.move_x))
if self.x < self.move_x:
self.x += min(self.speed * dt * self.tile_x,
abs(self.x - self.move_x))
if self.y > self.move_y:
self.y -= min(self.speed * dt * self.tile_y,
abs(self.y - self.move_y))
if self.y < self.move_y:
self.y += min(self.speed * dt * self.tile_y,
abs(self.y - self.move_y))
self.x, self.y = int(self.x), int(self.y)