#!/usr/bin/env python # -*- coding: utf-8 -*- # filename: ogre.py # # Ceibal Rol # Copyright (C) 2008,2009 Gabriel Balbuena # # 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: # Ceibal Jam http://ceibaljam.org """ Ogre in Rol Ceibal """ import pygame from pygame import * pygame.init() #const direccionales EAST = 0 NORTHEAST = 1 NORTH = 2 NORTHWEST = 3 WEST = 4 SOUTHWEST = 5 SOUTH = 6 SOUTHEAST = 7 class Ogre(pygame.sprite.Sprite): def __init__(self, screen): pygame.sprite.Sprite.__init__(self) self.screen = screen self.image = pygame.image.load("./data/entity/ogre/stopped 0000.png") #self.image = self.image.convert_alpha() tranColor = self.image.get_at((1, 1)) self.image.set_colorkey(tranColor) self.rect = self.image.get_rect() self.rect.center = (320, 240) # - - - - - - - - - - - - - - - - self.pos_xi = 0 # posicion x self.pos_yi = 0 # posicion y # - - - - - - - - - - - - - - - - self.imgList = [] # lista de frames self.loadPics() self.dir = EAST self.frame = 0 self.delay = 3 self.pause = self.delay self.speed = 0 self.dxVals = (1, .7, 0, -.7, -1, -.7, 0, .7) self.dyVals = (0, -.7, -1, -.7, 0, .7, 1, .7) self.moving = True def loadPics(self): fileBase = [ "./data/entity/ogre/walking e000", "./data/entity/ogre/walking ne000", "./data/entity/ogre/walking n000", "./data/entity/ogre/walking nw000", "./data/entity/ogre/walking w000", "./data/entity/ogre/walking sw000", "./data/entity/ogre/walking s000", "./data/entity/ogre/walking se000"] for dir in range(8): tempList = [] tempFile = fileBase[dir] for frame in range(8): imgName = "%s%d.png" % (tempFile, frame) tmpImg = pygame.image.load(imgName) #tmpImg.convert_alpha() tmpImg.convert() tranColor = tmpImg.get_at((0, 0)) tmpImg.set_colorkey(tranColor) tempList.append(tmpImg) self.imgList.append(tempList) def update(self): self.pause -= 1 if self.pause <= 0: self.pause = self.delay self.frame += 1 if self.frame > 7: self.frame = 0 self.calcVector() if self.moving == False: if self.dir == NORTH: self.image = pygame.image.load("./data/entity/ogre/stopped 0003.png") if self.dir == WEST: self.image = pygame.image.load("./data/entity/ogre/stopped 0002.png") if self.dir == EAST: self.image = pygame.image.load("./data/entity/ogre/stopped 0001.png") if self.dir == SOUTH: self.image = pygame.image.load("./data/entity/ogre/stopped 0000.png") else: self.image = self.imgList[self.dir][self.frame] self.rect.centerx += self.dx self.rect.centery += self.dy self.screen.mirar_a(self.rect) def calcVector(self): # calculo vector segun direccion. self.dx = self.dxVals[self.dir] self.dy = self.dyVals[self.dir] self.dx *= self.speed /(3)*2 # sacar /2 self.dy *= self.speed /(3)*2 def turnUp(self): self.dir = NORTH def turnLeft(self): self.dir = WEST def turnRight(self): self.dir = EAST def turnDown(self): self.dir = SOUTH def _print(self, screen): # maallll no respeta la camara "Imprime el personaje en la pantalla respetando la camara." #x = self.rect.x - self.screen.camara_x #y = self.rect.y - self.screen.camara_y #screen.blit(self.image, (x, y)) def main(): print "screen init" #screen = pygame.display.set_mode((640, 480)) screen = pygame.display.set_mode((640,480),FULLSCREEN) pygame.display.set_caption(".... TESTING ....") background = pygame.Surface(screen.get_size()) background.fill((200, 255, 200)) screen.blit(background, (0, 0)) ogre = Ogre(screen) # Probando grupos de sprites para manejo de coliciones. allSprites = pygame.sprite.Group(ogre) # probando time clock = pygame.time.Clock() run = True while run: clock.tick(30) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False elif event.type == pygame.KEYDOWN: if event.key == pygame.K_UP: ogre.moving = True ogre.speed += 10 ogre.turnUp() if event.key == pygame.K_DOWN: ogre.moving = True ogre.speed += 10 ogre.turnDown() if event.key == pygame.K_LEFT: ogre.moving = True ogre.speed += 10 ogre.turnLeft() if event.key == pygame.K_RIGHT: ogre.moving = True ogre.speed += 10 ogre.turnRight() print "ogre speed:", ogre.speed if ogre.speed > 0: if ogre.speed >= 35: ogre.speed = 35 ogre.speed = ogre.speed - 1 if ogre.speed == 0: ogre.moving = False allSprites.clear(screen, background) allSprites.update() allSprites.draw(screen) pygame.display.flip() if __name__ == "__main__": main()