Added a background image to the menu.

This commit is contained in:
Sakse Dalum 2012-08-07 15:40:42 +02:00
parent 775e4a72a8
commit c579838f91
7 changed files with 102 additions and 13 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 282 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 284 KiB

View File

@ -40,6 +40,9 @@ for module_name in module_names:
if not module.get_init():
print("Failed to initialise %s module." % module_name)
sys.exit()
if not pygame.image.get_extended():
print("Pygame does not support extended image support.")
sys.exit()
import argparse

View File

@ -28,6 +28,8 @@ import os
import pygame
import jukebox
import level
import main_menu
class Game(object):
"""Create an object to handle the game."""
@ -38,10 +40,16 @@ class Game(object):
self.clock = pygame.time.Clock()
self.jukebox = jukebox.Jukebox(os.path.abspath(os.path.join("resources",
"music")),
self.jukebox = jukebox.Jukebox(
os.path.abspath(os.path.join("resources", "music")),
["basshit.ogg"])
# Add main menu object to list of objects.
self.objs.append(main_menu.MainMenu(self, os.path.abspath(os.path.join(
"resources", "graphics", "main_menu.png"))))
self.level = None
self.ticks = self.prev_ticks = pygame.time.get_ticks()
def start(self):
@ -61,14 +69,19 @@ class Game(object):
self.draw()
self.clock.tick(self.speed)
def goto_level(self, level):
self.level = level
def update(self, t, dt):
"""
Update all game objects.
"""
# Retrieve and flush all events since last update call (this prevents
# event "bottlenecking"/lock-ups)
events = pygame.event.get()
for event in events:
e = pygame.event.get()
for event in e:
# Stop the game when closing the window
if event.type == pygame.QUIT:
self.stop()
@ -80,12 +93,11 @@ class Game(object):
# Update all objects
for obj in self.objs:
if hasattr(obj, 'update'):
obj.update(t, dt)
self.prev_ticks = pygame.time.get_ticks()
obj.update(e, t, dt)
def draw(self):
"""
Update all game objects.
Draw all game objects.
"""
self.window.fill((0, 0, 0))
for obj in self.objs:

View File

@ -26,7 +26,6 @@ The jukebox. Handles playback of background music.
import os
import pygame
import random
class Jukebox(object):
"""Create an object to handle music playback."""
@ -37,11 +36,9 @@ class Jukebox(object):
def play(self):
pygame.mixer.music.load(
os.path.join(self.music_dir,
self.music_files[self.iterator
% len(self.music_files)]))
os.path.join(self.music_dir, self.music_files[self.iterator]))
pygame.mixer.music.play()
self.iterator += 1
self.iterator = (self.iterator + 1) % len(self.music_files)
def stop(self):
pygame.mixer.music.stop()

29
robotgame/level.py Normal file
View File

@ -0,0 +1,29 @@
# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# level.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
A generic level.
"""
class Level(object):
def __init__(self, level_name):
pass

48
robotgame/main_menu.py Normal file
View File

@ -0,0 +1,48 @@
# 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/>.
#
# ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' ' '
#
# main_menu.py
# --------------------
# date created : Tue Aug 7 2012
# copyright : (C) 2012 Sakse Dalum
# maintained by : Sakse Dalum <don_s@hongabar.org>
"""
The main menu.
"""
import pygame
import level
class MainMenu(object):
def __init__(self, game, background_img_path):
self.__dict__.update(locals())
self.load()
def load(self):
self.background_img = pygame.image.load(self.background_img_path)
self.background_img = pygame.transform.smoothscale(
self.background_img, self.game.window.get_size())
def update(self, e, t, dt):
for event in e:
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
self.game.goto_level(level.Level("landing"))
def draw(self, window):
window.blit(self.background_img, (0, 0))