def initGame(): # Create "main menu" selectable menu items mMenu = menu.Selectable(Game.font, Game.screen) mMenuItem = menu.SelectableItem("New Game", constants.ACTION_NEWGAME) mMenu.addItem(mMenuItem) mMenuItem = menu.SelectableItem("Quit", constants.ACTION_QUIT) mMenu.addItem(mMenuItem) Game.menus.append(mMenu) # Create "choose mode" selectable menu items mMenu = menu.Selectable(Game.font, Game.screen) mMenuItem = menu.SelectableItem("1 Player", constants.ACTION_MODE1P) mMenu.addItem(mMenuItem) mMenuItem = menu.SelectableItem("2 Players", constants.ACTION_MODE2P) mMenu.addItem(mMenuItem) Game.menus.append(mMenu) # Create "score" text mText = menu.Text(Game.font, Game.screen) mTextItem = menu.TextItem( Game.font, str(Game.score), (constants.WINDOW_WIDTH / 2.0), ((constants.WINDOW_HEIGHT - constants.GAMEAREA_HEIGHT) / 2.0) - (constants.WALL_THICKNESS / 2.0)) mText.addItem(mTextItem) Game.texts.append(mText) # Create pause text mText = menu.Text(Game.font, Game.screen) mTextItem = menu.TextItem(Game.font, "P Unpauses", (constants.WINDOW_WIDTH / 2.0), (constants.WINDOW_HEIGHT / 3)) mText.addItem(mTextItem) mTextItem = menu.TextItem(Game.font, "ESC Returns to Main Menu", (constants.WINDOW_WIDTH / 2.0), (constants.WINDOW_HEIGHT / 3)) mTextItem.y += mTextItem.h mText.addItem(mTextItem) Game.texts.append(mText) # Create game over text mText = menu.Text(Game.font, Game.screen) mTextItem = menu.TextItem(Game.font, "Game Over!", (constants.WINDOW_WIDTH / 2.0), (constants.WINDOW_HEIGHT / 3)) mText.addItem(mTextItem) mTextItem = menu.TextItem(Game.font, "N Starts a New Game", (constants.WINDOW_WIDTH / 2.0), (constants.WINDOW_HEIGHT / 3)) mTextItem.y += mTextItem.h mText.addItem(mTextItem) mTextItem = menu.TextItem(Game.font, "ESC Returns to Main Menu", (constants.WINDOW_WIDTH / 2.0), (constants.WINDOW_HEIGHT / 3)) mTextItem.y += (mTextItem.h * 2.0) mText.addItem(mTextItem) Game.texts.append(mText)
def initGame(): # Create "main menu" selectable menu items mMenu = menu.Selectable(Game.font, Game.screen) mMenuItem = menu.SelectableItem("New Game", constants.ACTION_NEWGAME) mMenu.addItem(mMenuItem) mMenuItem = menu.SelectableItem("Quit", constants.ACTION_QUIT) mMenu.addItem(mMenuItem) Game.menus.append(mMenu) # Create "choose mode" selectable menu items mMenu = menu.Selectable(Game.font, Game.screen) mMenuItem = menu.SelectableItem("1 Player", constants.ACTION_MODE1P) mMenu.addItem(mMenuItem) mMenuItem = menu.SelectableItem("2 Players", constants.ACTION_MODE2P) mMenu.addItem(mMenuItem) Game.menus.append(mMenu) # Create "score" texts mText = menu.Text(Game.font, Game.screen) mTextItem = menu.TextItem( Game.font, str(Game.score[constants.PLAYER_ONE]), (constants.WINDOW_WIDTH / 2.0) + (constants.WINDOW_WIDTH / 6), ((constants.WINDOW_HEIGHT - constants.GAMEAREA_HEIGHT) / 2.0) - (constants.WALL_THICKNESS / 2.0)) mText.addItem(mTextItem) mTextItem = menu.TextItem( Game.font, str(Game.score[constants.PLAYER_TWO]), (constants.WINDOW_WIDTH / 2.0) - (constants.WINDOW_WIDTH / 6), ((constants.WINDOW_HEIGHT - constants.GAMEAREA_HEIGHT) / 2.0) - (constants.WALL_THICKNESS / 2.0)) mText.addItem(mTextItem) Game.texts.append(mText) # Create pause text mText = menu.Text(Game.font, Game.screen) mTextItem = menu.TextItem(Game.font, "'P' Unpauses 'Esc' Quits", (constants.WINDOW_WIDTH / 2.0), (constants.WINDOW_HEIGHT / 3)) mText.addItem(mTextItem) Game.texts.append(mText) # Player 1 paddle mGameObject = gameobject.Paddle(Game.screen, constants.PLAYER_ONE) Game.paddles.append(mGameObject) # Player 2 paddle mGameObject = gameobject.Paddle(Game.screen, constants.PLAYER_TWO) Game.paddles.append(mGameObject) # Ball mGameObject = gameobject.Ball(Game.screen) Game.balls.append(mGameObject) # Game area mGameObject = gameobject.GameObject( Game.screen, constants.GAMEOBJECT_WALL, constants.GAMEAREA_WIDTH, constants.GAMEAREA_HEIGHT, constants.GAMEAREA_X, constants.GAMEAREA_Y, constants.WALL_THICKNESS, constants.WALL_COLOR) Game.wall = mGameObject
def __init__(self, size, pos, color, speed, lives, screen): self.size = size (self.x, self.y) = pos self.dx, self.dy = 0, 0 self.speed = speed self.lives = lives self.mLives = lives self.age = 0 self.hitTimer = 0 self.color = color # Health bar color self.secCol = DARKGREEN # Object's color self.useSprite = True if self.useSprite: self.spritescale = 0.05 self.image = pygame.transform.scale( pygame.image.load('heart-red.png'), (int(w * self.spritescale), int(h * self.spritescale))) self.screen = screen self.healthMeter = menu.Text(screen=self.screen, coords=(0.02 * h, 0.02 * h, 0.12 * h, 0.12 * h), text=self._healthStr(), border=False, textSize=18, color=self.color, backGround=True) self.particle = particle.ParticleManager(screen=self.screen)
def mainloop(self): background = pygame.image.load('img/Background/' + self.getRandBackground()).convert() self.t0 = pygame.time.get_ticks() t1punch = t2punch = t1kick = t2kick = t1jump = t2jump = 0 while True: if self.round_rollin: keys = pygame.key.get_pressed() if not (self.p1.jumping or self.p1.punch_animation or self.p1.kick_animation or self.p1.hit_animation or self.p1.power_animation): self.p1.velocity = 0 if not (keys[K_d] and keys[K_a]): self.p1.set_state(0) self.p1.idle() if keys[K_a]: self.p1.set_state(1) self.p1.left(self.p2.hitbox) if keys[K_d]: self.p1.set_state(2) self.p1.right(self.p2.hitbox) if keys[K_w]: if not self.p1.jumping: self.p1.set_state(3) t1jump = pygame.time.get_ticks() if keys[K_e]: if not self.p1.punch_animation and pygame.time.get_ticks( ) - t1punch > 300: self.p1.set_state(4) t1punch = pygame.time.get_ticks() if keys[K_r]: if not self.p1.kick_animation and pygame.time.get_ticks( ) - t1kick > 300: self.p1.set_state(5) t1kick = pygame.time.get_ticks() if keys[K_t]: if not self.p1.superpower.active and not self.p1.jumping: self.p1.set_state(7) self.p1.launch_superpower(self.p2) if not (self.p2.jumping or self.p2.punch_animation or self.p2.kick_animation or self.p2.hit_animation or self.p2.power_animation): self.p2.velocity = 0 if not (keys[K_RIGHT] and keys[K_LEFT]): self.p2.set_state(0) self.p2.idle() if keys[K_LEFT]: self.p2.set_state(1) self.p2.left(self.p1.hitbox) if keys[K_RIGHT]: self.p2.set_state(2) self.p2.right(self.p1.hitbox) if keys[K_UP]: if not self.p2.jumping: self.p2.set_state(3) t2jump = pygame.time.get_ticks() if keys[K_j]: if not self.p2.punch_animation and pygame.time.get_ticks( ) - t2punch > 300: self.p2.set_state(4) t2punch = pygame.time.get_ticks() if keys[K_k]: if not self.p2.kick_animation and pygame.time.get_ticks( ) - t2kick > 300: self.p2.set_state(5) t2kick = pygame.time.get_ticks() if keys[K_l]: if not self.p2.superpower.active and not self.p2.jumping: self.p2.set_state(7) self.p2.launch_superpower(self.p1) for event in pygame.event.get(): if event.type == KEYDOWN: if event.key == K_ESCAPE: self.soundManager.play_sound('menucancel.wav') return 0 elif event.type == QUIT: exit() time = pygame.time.get_ticks() self.p1.refresh_movement_animation(time) self.p2.refresh_movement_animation(time) if self.p1.jumping: self.p1.jump(time - t1jump, self.p2.hitbox) if self.p1.punch_animation: self.p1.punch(self.p2) if self.p1.kick_animation: self.p1.kick(self.p2) if self.p1.superpower.active: self.p1.superpower.launch(self.p1, self.p2, 20) if self.p2.jumping: self.p2.jump(time - t2jump, self.p1.hitbox) if self.p2.punch_animation: self.p2.punch(self.p1) if self.p2.kick_animation: self.p2.kick(self.p1) if self.p2.superpower.active: self.p2.superpower.launch(self.p2, self.p1, 20) self.p1.refresh_combat_animation(time) self.p2.refresh_combat_animation(time) self.clock.update(time - self.t0) self.screen.fill((0, 0, 0)) self.screen.blit(background, (0, 0)) self.print_me() self.screenManager.display_update(self.screen) self.round_status() else: if self.p1.win_animation or self.p2.win_animation or self.p1.tie: time = pygame.time.get_ticks() if self.p1.win_animation: text = "P1 WINS!" pos = Point(96, 108) if self.p2.win_animation: text = "P2 WINS!" pos = Point(96, 108) if self.p1.tie: text = "TIE!" pos = Point(136, 108) self.p1.roundend_animation(time) self.p2.roundend_animation(time) self.screen.blit(background, (0, 0)) self.print_me() menu.Text(text, pos).print_me(self.screen) self.screenManager.display_update(self.screen) else: self.round_rollin = True self.p1.reset(True) self.p2.reset(False) self.t0 = pygame.time.get_ticks() if self.p1_wins or self.p2_wins: return 0
def __init__(self): self.time_left = 90 self.has_changed = True self.text = menu.Text('', Point(144, 8))