diff options
Diffstat (limited to 'blocku.py')
-rw-r--r-- | blocku.py | 140 |
1 files changed, 114 insertions, 26 deletions
@@ -1,3 +1,11 @@ + +"""Main Blocku game logic class + +import every thing that the activity needs and run game code + +Authored by Fran Rogers and Ariel Zamparini +""" + #!/usr/bin/python import pygame, random, os.path from pygame.locals import * @@ -30,21 +38,48 @@ def load_images(*files): imgs.append(load_image(file)) return imgs +class dummysound: + def play(self): pass + +def load_sound(file): + if not pygame.mixer: return dummysound() + file = os.path.join('data', file) + try: + sound = pygame.mixer.Sound(file) + return sound + except pygame.error: + print 'Warning, unable to load,', file + return dummysound() + class Block(pygame.sprite.Sprite): images = [] - def __init__(self, north=None, east=None, south=None, west=None): + north='' + east='' + south='' + west='' + def __init__(self, n='', e='', s='', w='', x='', y=''): pygame.sprite.Sprite.__init__(self, self.containers) - self.image = self.images[0] - self.rect = self.image.get_rect().move(300, 300) - self.north = north - self.east = east - self.south = south - self.west = west + self.image = self.images[0].copy() + self.rect = pygame.Rect(x,y,64,64) + self.font = pygame.font.Font(None, 20) + self.font.set_italic(1) + self.color = Color('blue') + self.update() + self.north = n + self.east = e + self.south = s + self.west = w def update(self): - pass - #if (mouseMove) - # self.rect.move(mouseX-posRelX,mouseY-posRelY) + #keep the block on the screen + self.rect = self.rect.clamp(SCREENRECT) + self.image = self.images[0].copy() + self.image.blit(self.font.render(str(self.north), 0, self.color),(26,3)) + self.image.blit(self.font.render(str(self.east), 0, self.color),(47,25)) + self.image.blit(self.font.render(str(self.south), 0, self.color),(26,47)) + self.image.blit(self.font.render(str(self.west), 0, self.color),(5,25)) + # game logic here for snapping to grid ...? + # when the block is snapped to the grid clamp the rect there def move(self, direction): # up = 0, right = 1, down = 2, left = 3 if direction == 0: @@ -55,8 +90,22 @@ class Block(pygame.sprite.Sprite): self.rect.move_ip(0,KEYBOARDMOVESPEED) if direction == 3: self.rect.move_ip(-KEYBOARDMOVESPEED,0) - #keep the block on the screen - self.rect = self.rect.clamp(SCREENRECT) + + def grab(self, pos): + x, y = pos; + #print x , y + #print self.rect.left, self.rect.top + #self.rect = self.rect.move(x, y) + #remember the offset here is 32 as this will center the mouse in our 64pixel image + self.rect.left = x-32 + self.rect.top = y-32 + def rotate(self): + temp = self.north + self.north = self.east + self.east = self.south + self.south = self.west + self.west = temp + #print self.north, self.east, self.south, self.west class Puzzle: def __init__(self): @@ -96,6 +145,10 @@ class Game: def run(self): self.running = True + if pygame.mixer and not pygame.mixer.get_init(): + print 'Warning, no sound' + pygame.mixer = None + #set the screen up winstyle = 0 # |FULLSCREEN bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) @@ -111,6 +164,7 @@ class Game: # load images to pipe to the sprite classes blockImg = load_image('block.png') Block.images = [blockImg] + gridImg = load_image('grid.png') # the test will need rects and positions i sugest make some kind of default # this information can be held by each block as they are created but is made here @@ -120,6 +174,7 @@ class Game: else: background = background.convert() + background.blit(gridImg,(200,200)) screen.blit(background,(0,0)) pygame.display.flip() @@ -133,16 +188,24 @@ class Game: #main blocku code structs blocks = pygame.sprite.Group() - Block.containers = spriteBatch - aBlock = Block() + Block.containers = blocks,spriteBatch + #blocks are Block(n,s,e,w,x,y) xy= starting position + aBlock = Block(1,2,3,4,200,200) + bBlock = Block(5,6,7,8,300,300) + global debugText + debugText = '' #see if there is a sprite font if pygame.font: - spriteBatch.add(Text(text = 'helloworld')) + spriteBatch.add(Text('Drawing call test ')) spriteBatch.add(mouseUpdate()) + #if a key is down + global keyDown + keyDown = False # it is important to note that like xna the origin is 0,0 # the top left of the current window + # print is trace in console # and increases as you go down and to the right # pygame has a collision detector under pygame.sprite.spritecollide(group,group,dokill) # this will return a list of colliders, dokill will remove the colliders from the parrent group if true @@ -153,17 +216,22 @@ class Game: gtk.main_iteration() # Pump PyGame messages. - for event in pygame.event.get(): - if event.type == QUIT or \ - (event.type == KEYDOWN and event.key == K_ESCAPE): + for e in event.get(): + if e.type == QUIT or \ + (e.type == KEYDOWN and e.key == K_ESCAPE): return - elif event.type == pygame.VIDEORESIZE: - pygame.display.set_mode(event.size, pygame.RESIZABLE) + elif e.type == pygame.VIDEORESIZE: + pygame.display.set_mode(e.size, pygame.RESIZABLE) + if e.type == MOUSEBUTTONDOWN: + event.set_grab(1) + elif e.type == MOUSEBUTTONUP: + event.set_grab(0) # get the state of the keyboard for input keystate = pygame.key.get_pressed() + if not keystate[K_SPACE]: + keyDown = False # for key test use keystate[key] and a return of true will occur when key down - # clear/erase the last drawn sprites spriteBatch.clear(screen, background) # update all the sprites @@ -177,6 +245,23 @@ class Game: aBlock.move(2) if keystate[K_LEFT]: aBlock.move(3) + if keystate[K_SPACE] and not keyDown: + aBlock.rotate() + keyDown = True + + #for block in blocks: + x, y = mouse.get_pos() + + if event.get_grab(): + debugText = 'holding mouse button 1' + if aBlock.rect.collidepoint(x,y): + aBlock.grab(mouse.get_pos()) + debugText += 'grabed aBlock' + elif bBlock.rect.collidepoint(x,y): + bBlock.grab(mouse.get_pos()) + debugText += 'grabed bBlock' + else: + debugText = '' # note random here is random.random() # note foreach here is for object in @@ -184,21 +269,24 @@ class Game: draw = spriteBatch.draw(screen) pygame.display.update(draw) + # Try to stay at 30 FPS self.clock.tick(30) class Text(pygame.sprite.Sprite): - def __init__(self,text="blank"): + text = '' + def __init__(self,txt=''): pygame.sprite.Sprite.__init__(self) self.font = pygame.font.Font(None, 20) self.font.set_italic(1) self.color = Color('blue') self.update() - self.rect = self.image.get_rect().move(50, 200) - self.text = text + self.rect = self.image.get_rect().move(55, 80) + self.text = txt def update(self): - msg = '' # 'Drawing call test' + global debugText + msg = self.text + debugText self.image = self.font.render(msg, 0, self.color) class mouseUpdate(pygame.sprite.Sprite): @@ -211,7 +299,7 @@ class mouseUpdate(pygame.sprite.Sprite): self.rect = self.image.get_rect().move(50, 220) def update(self): - msg = '' # 'Mouse Position %s, %s' % mouse.get_pos() + msg = 'Mouse Position %s, %s' % mouse.get_pos() self.image = self.font.render(msg, 0, self.color) # This function is called when the game is run directly from the command line: |