platforms = [] for i in range(5): plt = StaticEntity(screen, setting, 'images/block.png') plt.start_position([ randint(0, setting.width - plt.rect.width), setting.height / 6 * i + plt.rect.height ]) platforms.append(plt) plt = StaticEntity(screen, setting, 'images/block.png') plt.start_position() platforms.append(plt) while True: gf.check_events(doodle) doodle.move() doodle.move_y(platforms) doodle.animation(platforms) screen.blit(setting.bg_image, (0, 0)) for plt in platforms: plt.blit() doodle.shoot() doodle.blit() pygame.display.flip() pygame.time.delay(24)
def main(): import pygame from labyrinth import Labyrinth from guardian import Gardian from hero import Hero from items import Items from constants import screen, black pygame.init() pygame.display.set_caption("SOS MacGyver") # create of labyrinth labyrinth = Labyrinth("labyrinth.txt") labyrinth.creat_labyrinth() labyrinth.blit_labyrinth("pictures/wall32.jpg") # creat and placement MacGyver MacGyver = Hero("pictures/MacGyver32.png", "MacGyver") MacGyver.placement_hero(labyrinth.position_start) print("position du hero", labyrinth.position_start) MacGyver.blit(screen) # creat and placement gardian gardian = Gardian("pictures/Gardien32.png", "Gardian") gardian.placement_gardian(labyrinth.position_arrival) print("position du méchant", gardian.position) gardian.blit(screen) # creat and placement items aiguille = Items("pictures/seringue32.png", "aiguille") aiguille.placement_item(screen, labyrinth.list_positions_empty) print("position de l'aiguille", aiguille.position) tube = Items("pictures/tube_plastique32.png", "tube") tube.placement_item(screen, labyrinth.list_positions_empty) print("position du tube", tube.position) ether = Items("pictures/ether32.png", "ether") ether.placement_item(screen, labyrinth.list_positions_empty) print("position de l'ether", ether.position) pygame.display.flip() # Loop of game end_game = False while not end_game: # loop evenements for event in pygame.event.get(): if event.type == pygame.QUIT: end_game = True pygame.quit() elif event.type == pygame.KEYDOWN: if event.key == pygame.K_LEFT: MacGyver.move("L", labyrinth) elif event.key == pygame.K_RIGHT: MacGyver.move("R", labyrinth) elif event.key == pygame.K_UP: MacGyver.move("U", labyrinth) elif event.key == pygame.K_DOWN: MacGyver.move("D", labyrinth) MacGyver.clear_hero(screen, black) MacGyver.blit(screen) print("MacGyver est en position: ", MacGyver.position) aiguille.colision_items(MacGyver.position, labyrinth.seringue) tube.colision_items(MacGyver.position, labyrinth.seringue) ether.colision_items(MacGyver.position, labyrinth.seringue) print("liste des éléments de la seringue", labyrinth.seringue) labyrinth.counter_items(black) labyrinth.end_game(MacGyver.position) pygame.display.flip()
class App: """Class used by PiGame""" effect = None worlds = (EARTH, MOON, JUPITER) def __init__(self): self._running = True self.clock = None # Select a world (EARTH, JUPITER, MOON): self.current_world = self.worlds[0] self.background = Background(640, 400) self.hero = Hero(self.current_world, self.background.boundaries) self.enemy = Enemy(self.hero, self.background) def on_init(self): """Loads up variables for the game to start""" pygame.init() self.background.load_from_file() self.hero.load_from_file() self.enemy.load_from_file() # Some music and sound fx # frequency, size, channels, buffersize # pygame.mixer.pre_init(44100, 16, 2, 4096) self.effect = pygame.mixer.Sound('sounds/bounce.wav') pygame.mixer.music.load('sounds/music.wav') pygame.mixer.music.play(-1) self.hero.screen = self.background.screen self.enemy.screen = self.background.screen self.clock = pygame.time.Clock() pygame.display.set_caption( 'Angry Floating Guy! World: {} (w to change world, arrows to move, Esc to quit).'.format(self.current_world.name)) self._running = True def on_event(self, event): """Check for key pressed events""" arrows = self.hero.moving.keys() if event.type == pygame.QUIT or (event.type == pygame.KEYDOWN and (event.key == pygame.K_q or event.key == pygame.K_ESCAPE)): self._running = False elif event.type == pygame.KEYDOWN and event.key == pygame.K_f: self.background.toggle_fullscreen() elif event.type == pygame.KEYDOWN and event.key == pygame.K_w: curr_world_index = self.worlds.index(self.current_world) next_index = (curr_world_index + 1) % 3 self.current_world = self.worlds[next_index] pygame.display.set_caption( 'Angry Floating Guy! World: {} (w to change world, arrows to move, Esc to quit).'.format(self.current_world.name)) elif event.type == pygame.KEYDOWN and event.key in arrows: self.hero.moving[event.key] = True elif event.type == pygame.KEYUP: self.hero.moving[event.key] = False def on_loop(self): """Handles the physics for the hero's movement.""" self.clock.tick(self.background.fps) self.hero.world = self.current_world if self.hero.is_colliding_with(self.enemy): self.hero.score += 1 self.hero.move() if self.hero.bounced: self.effect.play() self.enemy.move() def on_render(self): # Render the background self.background.pan() # Show the hero's score smallfont = pygame.font.SysFont("comicsansms", 25) text = smallfont.render( "Score: "+str(self.hero.score), True, (255, 0, 0)) self.background.screen.blit(text, [0, 0]) # now blit the hero on screen self.hero.blit() self.enemy.blit() # and update the screen (don't forget that!) pygame.display.flip() def on_cleanup(self): pygame.quit() def on_execute(self): if self.on_init() == False: self._running = False while(self._running): for event in pygame.event.get(): self.on_event(event) self.on_loop() self.on_render() self.on_cleanup()