From 6e9e66f48b146ff7380283c4b44cf65bfc027a05 Mon Sep 17 00:00:00 2001 From: Alan Aguiar Date: Fri, 25 May 2012 20:31:53 +0000 Subject: make the changes for sugargame --- diff --git a/NEWS b/NEWS deleted file mode 100644 index 06afeb9..0000000 --- a/NEWS +++ /dev/null @@ -1,5 +0,0 @@ -Still in development - -TODO: - Add sound ? - Add better images for food diff --git a/activity.py b/activity.py index 65f7177..c7de70e 100644 --- a/activity.py +++ b/activity.py @@ -1,9 +1,88 @@ -import olpcgames +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# HorseGame +# Copyright (C) 2008, ghopper +# Copyright (C) 2012 Alan Aguiar +# +# This program 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. +# +# This program 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 this program. If not, see . +# +# Contact information: +# Alan Aguiar + +import sys +import gtk +import pygame + +from sugar.activity import activity +from sugar.graphics.toolbarbox import ToolbarBox +from sugar.activity.widgets import ActivityToolbarButton +from sugar.graphics.toolbutton import ToolButton +from sugar.activity.widgets import StopButton + from gettext import gettext as _ -class Activity(olpcgames.PyGameActivity): - """Your Sugar activity""" - - game_name = 'run' - game_title = _('HorseGame') - game_size = None +import sugargame.canvas + +import horse + + +class Activity(activity.Activity): + + def __init__(self, handle): + activity.Activity.__init__(self, handle) + + self.game = horse.Game() + + self.build_toolbar() + + self._pygamecanvas = sugargame.canvas.PygameCanvas(self) + + self.set_canvas(self._pygamecanvas) + + self._pygamecanvas.grab_focus() + + self._pygamecanvas.run_pygame(self.game.run) + + def build_toolbar(self): + + self.max_participants = 1 + + toolbar_box = ToolbarBox() + self.set_toolbar_box(toolbar_box) + toolbar_box.show() + + activity_button = ActivityToolbarButton(self) + toolbar_box.toolbar.insert(activity_button, -1) + activity_button.show() + + separator = gtk.SeparatorToolItem() + separator.props.draw = False + separator.set_expand(True) + toolbar_box.toolbar.insert(separator, -1) + separator.show() + + stop_button = StopButton(self) + toolbar_box.toolbar.insert(stop_button, -1) + stop_button.show() + + + def read_file(self, file_path): + #self.game.read_file(file_path) + pass + + def write_file(self, file_path): + #self.game.write_file(file_path) + pass + diff --git a/horse.py b/horse.py index 50da427..5808351 100644 --- a/horse.py +++ b/horse.py @@ -1,39 +1,64 @@ -import pygame, logging -import math, random +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# HorseGame +# Copyright (C) 2008, ghopper +# Copyright (C) 2012 Alan Aguiar +# +# This program 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. +# +# This program 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 this program. If not, see . +# +# Contact information: +# Alan Aguiar + +import gtk +import pygame +import logging +import math +import random -log = logging.getLogger( 'horse.game' ) -log.setLevel( logging.DEBUG ) class Game(): - game_running = True - # tuple for horse location - horse_loc = (0,0) - # array of (image,location) for apple/carrot/etc locations - objects = [] - # keep track of the mouse pointer - mouse_pos = (200,200) - # tuple size - screen_size = (0,0) - grass_size = (0,0) - horse_size = (0,0) - apple_size = (0,0) - # images / (type pygame Surface) - grass_image = None - horse_image = None - horse_image_l = None - moving_left = False - apple_image = None - carrot_image = None - hay_image = None - # other parameters - horse_speed = 8 # pixels per tick; at 25 ticks/second, this is approx 200 pixels per second - horse_reach = 20 # pixels from cener of horse where he can reach - target_loc = None + + def __init__(self): + self.game_running = True + # tuple for horse location + self.horse_loc = (100,100) + # array of (image,location) for apple/carrot/etc locations + self.objects = [] + # keep track of the mouse pointer + self.mouse_pos = (300,300) + # tuple size + self.screen_size = (1200,900) + self.grass_size = (0,0) + self.horse_size = (0,0) + self.apple_size = (0,0) + # images / (type pygame Surface) + self.background = None + self.grass_image = None + self.horse_image = None + self.horse_image_l = None + self.moving_left = False + self.apple_image = None + self.carrot_image = None + self.hay_image = None + # other parameters + self.horse_speed = 8 # pixels per tick; at 25 ticks/second, this is approx 200 pixels per second + self.horse_reach = 20 # pixels from cener of horse where he can reach + self.target_loc = None def setup(self,screen): self.screen_size = screen.get_size() # tuple - # put the horse in the center of the screen - self.horse_loc = (100,100) # load the images and convert to screen format self.grass_image = pygame.image.load('images/grass.png','grass') self.grass_image.convert(screen) @@ -53,44 +78,42 @@ class Game(): self.hay_size = (20,20) self.hay_image = pygame.Surface(self.hay_size,0,screen) self.hay_image.fill((0x99,0x66,0x33)) - self.update(screen) - def update(self,screen): - """updates the screen image""" - self.screen_size = screen.get_size() # tuple - # paint background image - # TODO: there is probably a better way to tile the background image - # (and perhaps to tile it only once, and save the base image) + self.background = pygame.Surface((self.screen_size[0], self.screen_size[1]), 0, screen) + tilex=int(math.ceil(self.screen_size[0]/float(self.grass_size[0]))) tiley=int(math.ceil(self.screen_size[1]/float(self.grass_size[1]))) for x in range(0,tilex): for y in range(0,tiley): - screen.blit(self.grass_image,(x*self.grass_size[0],y*self.grass_size[1])) - # paint horse image on screen - # TODO: flip horse image left or right, depending on the direction she is moving - #screen.blit(self.horse_image,self.horse_loc) + self.background.blit(self.grass_image,(x*self.grass_size[0],y*self.grass_size[1])) + + self.update(screen) + + def update(self,screen): + """updates the screen image""" + screen.blit(self.background, (0,0)) if self.moving_left: self.drawObject(screen,(self.horse_image_l, self.horse_loc)) else: self.drawObject(screen,(self.horse_image, self.horse_loc)) # draw apples and other objects for o in self.objects: - self.drawObject(screen,o) + self.drawObject(screen, o) # flip display buffer pygame.display.flip() - def drawObject(self,screen,object): + def drawObject(self, screen, o): # unpack the object - (image, loc) = object + (image, loc) = o object_size = image.get_size() # adjust the upper left corner so that the center of object is at the recorded location adj_loc = (loc[0]-object_size[0]/2,loc[1]-object_size[1]/2) screen.blit(image, adj_loc) - def placeObject(self,image,location): + def placeObject(self, image, location): #adj_loc = self.adjust_loc(location, image.get_size()) adj_loc = location - self.objects.append((image,adj_loc)) + self.objects.append((image, adj_loc)) def adjust_loc(self,loc,object_size): """adjust the given location by half the object size. Thus the center of the object will be at loc""" @@ -101,10 +124,7 @@ class Game(): if event.type == pygame.QUIT: self.game_running = False elif event.type == pygame.KEYDOWN: - #log.debug("event keydown: %s", event) - # TODO: keys are not a localized if event.key in (27,113): # esc or q=quit - log.debug('quit key pressed') self.game_running = False elif event.key == 97: # a=apple self.placeObject(self.apple_image, self.mouse_pos) @@ -112,19 +132,12 @@ class Game(): self.placeObject(self.carrot_image, self.mouse_pos) elif event.key == 104: # h=hay self.placeObject(self.hay_image, self.mouse_pos) - elif event.type == pygame.KEYUP: - pass elif event.type == pygame.MOUSEBUTTONDOWN: - #log.debug("event mousedown: %s", event) # place apples self.placeObject(self.apple_image, self.mouse_pos) elif event.type == pygame.MOUSEMOTION: - #log.debug("event mousemove: %s", event) # Remember mouse location, because we need it in KEYDOWN events self.mouse_pos = event.pos - else: - #log.debug("event other: %s", event) - pass def tick(self,millis): """updates the game state for a tick""" @@ -180,3 +193,35 @@ class Game(): def isRunning(self): return self.game_running + + + def run(self): + #size = (1200,900) + #size = (16*75,11*75) + #if olpcgames.ACTIVITY: + # size = olpcgames.ACTIVITY.game_size + #screen = pygame.display.set_mode(size) + screen = pygame.display.get_surface() + clock = pygame.time.Clock() + #g = Game() + self.setup(screen) + self.mouse_pos = pygame.mouse.get_pos() + + while self.isRunning(): + #GTK events + while gtk.events_pending(): + gtk.main_iteration() + + # tick with wait 1/25th of a second + milliseconds = clock.tick(25) # maximum number of frames per second + self.tick(milliseconds) + self.update(screen) + + # Event processing loop + # not sure i want the pausescreen behavior + #events = pausescreen.get_events() + events = pygame.event.get() + for event in events: + self.handleEvent(event) + + diff --git a/run.py b/run.py deleted file mode 100644 index 5498cf3..0000000 --- a/run.py +++ /dev/null @@ -1,41 +0,0 @@ -#! /usr/bin/env python -"""Skeleton project file mainloop for new OLPCGames users""" -import olpcgames, pygame, logging -from olpcgames import pausescreen -import horse - -log = logging.getLogger( 'run' ) -log.setLevel( logging.DEBUG ) - -def main(): - """The mainloop which is specified in the activity.py file - - "main" is the assumed function name - """ - size = (800,600) - #size = (16*75,11*75) - if olpcgames.ACTIVITY: - size = olpcgames.ACTIVITY.game_size - screen = pygame.display.set_mode(size) - clock = pygame.time.Clock() - game = horse.Game() - game.setup(screen) - - running = True - while game.isRunning(): - # tick with wait 1/25th of a second - milliseconds = clock.tick(25) # maximum number of frames per second - game.tick(milliseconds) - game.update(screen) - - # Event processing loop - # not sure i want the pausescreen behavior - #events = pausescreen.get_events() - events = pygame.event.get() - if events: - for event in events: - game.handleEvent(event) - -if __name__ == "__main__": - logging.basicConfig() - main() -- cgit v0.9.1