Added player and level and world objects.
This commit is contained in:
parent
c14c5e248e
commit
dc5acdf4c6
|
@ -67,7 +67,7 @@ class Game(object):
|
||||||
t = pygame.time.get_ticks()
|
t = pygame.time.get_ticks()
|
||||||
dt = 0
|
dt = 0
|
||||||
while self.running:
|
while self.running:
|
||||||
dt = t - pygame.time.get_ticks()
|
dt = float(pygame.time.get_ticks() - t) / 1000
|
||||||
t = pygame.time.get_ticks()
|
t = pygame.time.get_ticks()
|
||||||
self.update(t, dt)
|
self.update(t, dt)
|
||||||
self.draw()
|
self.draw()
|
||||||
|
@ -76,6 +76,7 @@ class Game(object):
|
||||||
|
|
||||||
def goto_level(self, level):
|
def goto_level(self, level):
|
||||||
self.level = level
|
self.level = level
|
||||||
|
self.objs.append(self.level)
|
||||||
|
|
||||||
|
|
||||||
def update(self, t, dt):
|
def update(self, t, dt):
|
||||||
|
|
|
@ -31,14 +31,14 @@ class GameMenu(object):
|
||||||
def __init__(self, game, img_dir, active=False, selection=0):
|
def __init__(self, game, img_dir, active=False, selection=0):
|
||||||
self.__dict__.update(locals())
|
self.__dict__.update(locals())
|
||||||
|
|
||||||
self.menu_order = ['restart_level', 'quit']
|
self.menu = ['restart_level', 'quit']
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
|
|
||||||
def load(self):
|
def load(self):
|
||||||
screen_size = self.game.window.get_size()
|
screen_size = self.game.window.get_size()
|
||||||
|
|
||||||
for item in self.menu_order:
|
for item in self.menu:
|
||||||
setattr(self, '%s_imgs' % item, [
|
setattr(self, '%s_imgs' % item, [
|
||||||
pygame.image.load(os.path.join(self.img_dir,
|
pygame.image.load(os.path.join(self.img_dir,
|
||||||
'%s-%s.png' % (item, end)))
|
'%s-%s.png' % (item, end)))
|
||||||
|
@ -59,26 +59,26 @@ class GameMenu(object):
|
||||||
if event.key == pygame.K_ESCAPE:
|
if event.key == pygame.K_ESCAPE:
|
||||||
self.active = not self.active
|
self.active = not self.active
|
||||||
if self.active:
|
if self.active:
|
||||||
if event.key == pygame.K_SPACE:
|
if event.key in [pygame.K_SPACE, pygame.K_RETURN]:
|
||||||
if self.menu_order[self.selection] == 'restart_level':
|
if self.menu[self.selection] == 'restart_level':
|
||||||
pass
|
pass
|
||||||
if self.menu_order[self.selection] == 'quit':
|
if self.menu[self.selection] == 'quit':
|
||||||
self.game.stop()
|
self.game.stop()
|
||||||
if event.key == pygame.K_UP:
|
if event.key == pygame.K_UP:
|
||||||
self.selection = max(self.selection - 1, 0)
|
self.selection = max(self.selection - 1, 0)
|
||||||
if event.key == pygame.K_DOWN:
|
if event.key == pygame.K_DOWN:
|
||||||
self.selection = min(self.selection + 1,
|
self.selection = min(self.selection + 1,
|
||||||
len(self.menu_order) - 1)
|
len(self.menu) - 1)
|
||||||
|
|
||||||
def draw(self, window):
|
def draw(self, window):
|
||||||
if self.active:
|
if self.active:
|
||||||
screen_size = self.game.window.get_size()
|
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
|
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,
|
window.blit(img,
|
||||||
(int((screen_size[0] - img.get_size()[0]) / 2),
|
(int((screen_size[0] - img.get_size()[0]) / 2),
|
||||||
int(screen_size[1] / 2)
|
int(screen_size[1] / 2)
|
||||||
- (int(screen_size[1]*0.13)
|
- (int(screen_size[1]*0.13)
|
||||||
* (len(self.menu_order) / 2 - i))))
|
* (len(self.menu) / 2 - i))))
|
||||||
|
|
|
@ -25,5 +25,8 @@ A generic level.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
class Level(object):
|
class Level(object):
|
||||||
def __init__(self, level_name):
|
def update(self, e, t, dt):
|
||||||
self.__dict__.update(locals())
|
pass
|
||||||
|
|
||||||
|
def draw(self, window):
|
||||||
|
pass
|
||||||
|
|
|
@ -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)
|
|
@ -27,7 +27,7 @@ The main menu.
|
||||||
import os
|
import os
|
||||||
import pygame
|
import pygame
|
||||||
|
|
||||||
import level
|
import level1
|
||||||
import game_menu
|
import game_menu
|
||||||
|
|
||||||
class MainMenu(object):
|
class MainMenu(object):
|
||||||
|
@ -59,7 +59,7 @@ class MainMenu(object):
|
||||||
for event in e:
|
for event in e:
|
||||||
if event.type == pygame.KEYDOWN:
|
if event.type == pygame.KEYDOWN:
|
||||||
if event.key == pygame.K_SPACE:
|
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.remove(self)
|
||||||
self.game.objs.append(game_menu.GameMenu(self.game,
|
self.game.objs.append(game_menu.GameMenu(self.game,
|
||||||
self.img_dir))
|
self.img_dir))
|
||||||
|
|
|
@ -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)
|
|
@ -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)
|
Loading…
Reference in New Issue