def main(winstyle=0): # Initialize pygame pygame.init() if pygame.mixer and not pygame.mixer.get_init(): print 'Warning, no sound' pygame.mixer = None # Set the display mode if store._preferences.fullscreen: winstyle = FULLSCREEN else: winstyle = 0 bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth) #Load images, assign to sprite classes #(do this before the classes are used, after screen setup) horizontal = load_image('horizontal.png') #0 vertical = load_image('vertical.png') #1 ende = load_image('ende.png') #2 endn = load_image('endn.png') #3 ends = load_image('ends.png') #4 endw = load_image('endw.png') #5 bendes = load_image('bendes.png') #6 bendne = load_image('bendne.png') #7 bendsw = load_image('bendsw.png') #8 bendwn = load_image('bendwn.png') #9 threewaye = load_image('threewaye.png') #10 threewayn = load_image('threewayn.png') #11 threeways = load_image('threeways.png') #12 threewayw = load_image('threewayw.png') #13 fourway = load_image('fourway.png') #14 empty = load_image('empty.png') #15 left = load_image('playerleft.gif') up = load_image('playerup.gif') down = load_image('playerdown.gif') Player.images = [left, pygame.transform.flip(left, 1, 0), up, down] Tile.images = [ horizontal, vertical, ende, endn, ends, endw, bendes, bendne, bendsw, bendwn, threewaye, threewayn, threeways, threewayw, fourway, empty ] Hat.images = [load_image('hat.gif')] #decorate the game window #icon = pygame.transform.scale(Alien.images[0], (32, 32)) #pygame.display.set_icon(icon) #pygame.display.set_caption('MAZE') pygame.mouse.set_visible(1) #create the background, tile the bgd image bgdtile = load_image('empty.png') background = pygame.Surface(SCREENRECT.size) for x in range(0, SCREENRECT.width, bgdtile.get_width()): background.blit(bgdtile, (x, 0)) screen.blit(background, (0, 0)) #myfont = pygame.font.SysFont("monospace", 15) #label = myfont.render("Time left: 80", 1, (255,255,0)) #screen.blit(label, (100, 100)) pygame.display.flip() #load the sound effects #boom_sound = load_sound('boom.wav') #shoot_sound = load_sound('car_door.wav') #if pygame.mixer: # music = os_path_join('data', 'house_lo.wav') # pygame.mixer.music.load(music) # pygame.mixer.music.play(-1) # Initialize Game Groups tiles = pygame.sprite.Group() all = pygame.sprite.OrderedUpdates() #assign default groups to each sprite class Timer.containers = all Player.containers = all Hat.containers = all Tile.containers = all #Create Some Starting Values #global# score clock = pygame.time.Clock() global timeleft timeleft = 120 #initialize our starting sprites #Tile() #note, this 'lives' because it goes into a sprite group #add tiles to world for x in range(0, WORLDWIDTH): for y in range(0, WORLDHEIGHT): Tile([x, y]) Hat() player = Player() #timer = Timer() if pygame.font: all.add(Timer()) while timeleft > 0: milliseconds = clock.tick(40) # milliseconds passed since last frame seconds = milliseconds / 1000.0 # seconds passed since last frame (float) timeleft = timeleft - seconds #get input for event in pygame.event.get(): if event.type == QUIT or \ (event.type == KEYDOWN and event.key == K_ESCAPE): return -1 keystate = pygame.key.get_pressed() # clear/erase the last drawn sprites all.clear(screen, background) #update all the sprites all.update() #handle player input player.move(getInputDirectionX(), getInputDirectionY()) if player.pos == HATLOC: return 1 #draw the scene dirty = all.draw(screen) pygame.display.update(dirty) if pygame.mixer: pygame.mixer.music.fadeout(1000) pygame.time.wait(1000) return 0
def main(winstyle = 0): # Initialize pygame pygame.init() if pygame.mixer and not pygame.mixer.get_init(): print 'Warning, no sound' pygame.mixer = None # Set the display mode if store._preferences.fullscreen: winstyle = FULLSCREEN else: winstyle = 0 bestdepth = pygame.display.mode_ok(SCREENRECT.size, winstyle, 32) screen = pygame.display.set_mode(SCREENRECT.size, winstyle, bestdepth) #Load images, assign to sprite classes #(do this before the classes are used, after screen setup) img = load_image('player1.gif') Player.images = [img, pygame.transform.flip(img, 1, 0)] img = load_image('explosion1.gif') Explosion.images = [img, pygame.transform.flip(img, 1, 1)] Alien.images = load_images('alien1.gif', 'alien2.gif', 'alien3.gif') Bomb.images = [load_image('bomb.gif')] Shot.images = [load_image('shot.gif')] #decorate the game window icon = pygame.transform.scale(Alien.images[0], (32, 32)) pygame.display.set_icon(icon) pygame.display.set_caption('Pygame Aliens') pygame.mouse.set_visible(0) #create the background, tile the bgd image bgdtile = load_image('background.gif') background = pygame.Surface(SCREENRECT.size) for x in range(0, SCREENRECT.width, bgdtile.get_width()): background.blit(bgdtile, (x, 0)) screen.blit(background, (0,0)) pygame.display.flip() #load the sound effects boom_sound = load_sound('boom.wav') shoot_sound = load_sound('car_door.wav') if pygame.mixer: music = os_path_join('data', 'house_lo.wav') pygame.mixer.music.load(music) pygame.mixer.music.play(-1) # Initialize Game Groups aliens = pygame.sprite.Group() shots = pygame.sprite.Group() bombs = pygame.sprite.Group() all = pygame.sprite.RenderUpdates() lastalien = pygame.sprite.GroupSingle() #assign default groups to each sprite class Player.containers = all Alien.containers = aliens, all, lastalien Shot.containers = shots, all Bomb.containers = bombs, all Explosion.containers = all Score.containers = all #Create Some Starting Values global score alienreload = ALIEN_RELOAD kills = 0 clock = pygame.time.Clock() global SCORE SCORE = 0 #initialize our starting sprites player = Player() Alien() #note, this 'lives' because it goes into a sprite group #if pygame.font: #all.add(Score()) while player.alive(): #get input for event in pygame.event.get(): if event.type == QUIT or \ (event.type == KEYDOWN and event.key == K_ESCAPE): return SCORE keystate = pygame.key.get_pressed() # clear/erase the last drawn sprites all.clear(screen, background) #update all the sprites all.update() #handle player input direction = keystate[K_RIGHT] - keystate[K_LEFT] player.move(direction) firing = keystate[K_SPACE] if not player.reloading and firing and len(shots) < MAX_SHOTS: Shot(player.gunpos()) shoot_sound.play() player.reloading = firing # Create new alien if alienreload: alienreload = alienreload - 1 elif not int(random.random() * ALIEN_ODDS): Alien() alienreload = ALIEN_RELOAD # Drop bombs if lastalien and not int(random.random() * BOMB_ODDS): Bomb(lastalien.sprite) # Detect collisions for alien in pygame.sprite.spritecollide(player, aliens, 1): boom_sound.play() Explosion(alien) Explosion(player) SCORE = SCORE + 1 player.kill() for alien in pygame.sprite.groupcollide(shots, aliens, 1, 1).keys(): boom_sound.play() Explosion(alien) SCORE = SCORE + 1 for bomb in pygame.sprite.spritecollide(player, bombs, 1): boom_sound.play() Explosion(player) Explosion(bomb) player.kill() #draw the scene dirty = all.draw(screen) pygame.display.update(dirty) #cap the framerate clock.tick(40) if pygame.mixer: pygame.mixer.music.fadeout(1000) pygame.time.wait(1000) return SCORE