def __init__(self): super().__init__() self.background = urwid.AttrMap(urwid.SolidFill(' '), 'bg') self.player = Player(Config.ARENA_WIDTH, Config.ARENA_HEIGHT) self.entities = [ Entity(Config.ARENA_WIDTH, Config.ARENA_HEIGHT) for _ in range(Config.ENEMIES) ] self.loop = urwid.MainLoop(self.__get_screen(), palette=Config.PALLETE, unhandled_input=self.handle_input)
def main(): # Set window size windowSize = [600, 600] # Create variable keep_going keep_going = True # Create pygame Clock class instance timer = pygame.time.Clock() # Create display screen = pygame.display.set_mode(windowSize) # Create instances of local classes sound = Sound() sound.playMusic(sound.soundtrack) object = Object(screen, windowSize[0] // 2, windowSize[1] // 2, "#e00000") player = Player(2, screen, windowSize[0] // 2, windowSize[1] // 2) background = Background(screen, -windowSize[0], -windowSize[1]) # Play soundtrack in sound class sound.playMusic(sound.soundtrack) # Game loop while keep_going: # Fill screen with blue screen.fill("#4287f5") # Update entities background.update() player.update() object.update() # Flip display pygame.display.flip() # Check if quit for event in pygame.event.get(): if event.type == pygame.QUIT: # Set keep_going to false keep_going = False # Call tick method in timer instance timer.tick(720)
def __init__(self, block_positions): Player.__init__(self, img=c.enemy_img[0]) list_block_positions = block_positions + c.not_block_positions self.appear = 0 count = 0 while count != len(list_block_positions): count = 0 for i in list_block_positions: if i[0] == self.rect.x and i[1] == self.rect.y: self.rect.x = randrange(c.BLOCK[0], c.WINDOW[0] - c.BLOCK[0], c.BLOCK[0]) self.rect.y = randrange(c.BLOCK[1], c.WINDOW[1] - c.BLOCK[1], c.BLOCK[1]) else: count += 1
class Game: def __init__(self): super().__init__() self.background = urwid.AttrMap(urwid.SolidFill(' '), 'bg') self.player = Player(Config.ARENA_WIDTH, Config.ARENA_HEIGHT) self.entities = [ Entity(Config.ARENA_WIDTH, Config.ARENA_HEIGHT) for _ in range(Config.ENEMIES) ] self.loop = urwid.MainLoop(self.__get_screen(), palette=Config.PALLETE, unhandled_input=self.handle_input) def __get_screen(self): arena = urwid.AttrMap(urwid.SolidFill(' '), 'arena') arena = self.player.render(arena) for e in self.entities: arena = e.render(arena) return urwid.Overlay(arena, self.background, 'center', Config.ARENA_WIDTH, 'middle', Config.ARENA_HEIGHT) def refresh(self): for e in self.entities: e.update() self.loop.widget = self.__get_screen() def start(self): self.loop.run() def handle_input(self, key): if key in ('q', 'Q', 'esc'): raise urwid.ExitMainLoop() if key in ('w', 'W', 'up'): self.player.move("up") self.refresh() if key in ('s', 'S', 'down'): self.player.move("down") self.refresh() if key in ('a', 'A', 'left'): self.player.move("left") self.refresh() if key in ('d', 'D', 'right'): self.player.move("right") self.refresh()
def __init__(self, camera, luObserver, fxObserver, lvl): self.mSwitch = True self.mTimer = 0.0 self.mLevelDone = False self.mCamera = camera self.mLuObs = luObserver self.mFxObs = fxObserver self.contactListener = ContactListener() self.gravity = Gravity() self.physWorld = b2World(gravity=(0,0),doSleep=True, contactListener=self.contactListener) self.level = Level(self.physWorld, self.gravity, lvl) self.player = Player(self.level.mStartPos, self.physWorld, self.gravity) self.mEntityToFollow = self.player
def __init__(self, width: int, height: int, freedom: float, player: Player): self.area = [[(Empty() if random() < freedom else Mine()) for _ in range(width)] for _ in range(height)] for index in [-2, -1, 0, 1]: for cell_index in range(width): if isinstance(self.area[index][cell_index], Mine): self.area[index][cell_index] = Empty() for index in range(height): self.area[index][0] = self.area[index][-1] = Wall() for index in range(width): self.area[0][index] = self.area[-1][index] = Wall() self.area[0][width // 2] = Exit() self.area[-1][width // 2] = Empty() self.area[0][-1 + width // 2] = Exit() self.area[-1][-1 + width // 2] = Empty() if width % 2 == 1: self.area[0][1 + width // 2] = Exit() self.area[-1][1 + width // 2] = Empty() self.player = player player.area = self player.position = [len(self.area[0]) // 2, len(self.area) - 1]
def create_player(self, player_id: str, spaceship_id: str, player_config: PlayerConfig, status_x=15, status_y=15): status = PlayerStatus(screen=self.screen, x=status_x, y=status_y, color=player_config.bullet_color) return Player(player_id=player_id, spaceship_id=spaceship_id, player_config=player_config, player_status=status)
def __init__(self): self.window = Window() self.hud = Hud(self.window.screen, self.window.size) self.statistics = Statistic() self.clock = pygame.time.Clock() pygame.key.set_repeat(40, 30) self.size = [80, 80] self.player = Player(self.size, "entities/bb.png") self.enemies_buffer = 15 self.knifeAnimation = load_image('entities/attack/a0.png'), load_image('entities/attack/a1.png'),load_image('entities/attack/a2.png'),\ load_image('entities/attack/a3.png'), load_image('entities/attack/a4.png'),load_image('entities/attack/a5.png'),\ load_image('entities/attack/a6.png'), load_image('entities/attack/a7.png'),load_image('entities/attack/a8.png'),\ load_image('entities/attack/a9.png'), load_image('entities/attack/a10.png'),load_image('entities/attack/a11.png'),\ load_image('entities/attack/a12.png'), load_image('entities/attack/a13.png'),load_image('entities/attack/a14.png') self.entities = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.bullets = pygame.sprite.Group() self.entities.add(self.player) self.old_time = pygame.time.get_ticks() self.new_time = pygame.time.get_ticks()
def play(): player = Player() os.system('cls') print('Select complexity:') options = [ f'{name} (freedom: {int(complexity * 100)}%)' for name, complexity in complexities ] area = Area(120, 20, complexities[select_option(options)][1], player) while player.area is not None: area.draw() act(area) if player.won: print("You won") else: print("Game over")
def __init__(self): pygame.init() pygame.display.set_caption("Bomberman") self.BLOCKS_DESTRUCTIBLE = 100 self.ENEMIES = 10 self.appear = 0 self.display = pygame.display.set_mode(c.WINDOW) self.clock = pygame.time.Clock() self.player = Player() self.block_positions = [] self.gr_player = Group() self.gr_enemies = Group() self.gr_block = Group() self.gr_block_destructible = Group() self.gr_bomb = Group() self.gr_bomb_collide = Group() self.start()
def add_player(self, name, color, type_of_player): self.__check_for_too_many_players() self.__check_if_color_is_already_been_used(color) self.players.append(Player(name, color, type_of_player))
class Game: def __init__(self): self.window = Window() self.hud = Hud(self.window.screen, self.window.size) self.statistics = Statistic() self.clock = pygame.time.Clock() pygame.key.set_repeat(40, 30) self.size = [80, 80] self.player = Player(self.size, "entities/bb.png") self.enemies_buffer = 15 self.knifeAnimation = load_image('entities/attack/a0.png'), load_image('entities/attack/a1.png'),load_image('entities/attack/a2.png'),\ load_image('entities/attack/a3.png'), load_image('entities/attack/a4.png'),load_image('entities/attack/a5.png'),\ load_image('entities/attack/a6.png'), load_image('entities/attack/a7.png'),load_image('entities/attack/a8.png'),\ load_image('entities/attack/a9.png'), load_image('entities/attack/a10.png'),load_image('entities/attack/a11.png'),\ load_image('entities/attack/a12.png'), load_image('entities/attack/a13.png'),load_image('entities/attack/a14.png') self.entities = pygame.sprite.Group() self.enemies = pygame.sprite.Group() self.bullets = pygame.sprite.Group() self.entities.add(self.player) self.old_time = pygame.time.get_ticks() self.new_time = pygame.time.get_ticks() def gameUpdate(self): if len(self.enemies.sprites()) == 0: self.statistics.plusRound() round = self.statistics.getRound() self.player.buffDamage() for amount in range(round % self.enemies_buffer): if (round % 15 == 0): self.enemies_buffer += 5 dice = random.randrange(0, 4) spawn = None if (dice == 0): spawn = random.randrange(-100, 1400), random.randrange(-100, 0) elif (dice == 1): spawn = random.randrange(-100, 0), random.randrange(0, 800) elif (dice == 2): spawn = random.randrange(0, 1400), random.randrange(800, 900) else: spawn = random.randrange(1400, 1500), random.randrange(0, 800) self.enemies.add(Enemy(self.size, self.knifeAnimation, spawn)) def collisions(self, group1, group2, kill1, kill2): collision = pygame.sprite.groupcollide(group1, group2, kill1, kill2) for unit in collision: for second_unit in collision[unit]: self.statistics.plusKill( unit.getDamage(second_unit.dealDamage())) def enemies_collision(self, enemies): for enemy in enemies: for second_enemy in enemies: if pygame.sprite.collide_rect(enemy, second_enemy): if (enemy == second_enemy): enemy.speed = 0.01 continue enemy.speed = 0 else: enemy.speed = 0.01 def loop(self): pygame.mixer.Channel(0).set_volume(0.25) pygame.mixer.Channel(0).play(pygame.mixer.Sound('music.wav'), 99999) running = True paused = False while running: self.gameUpdate() for event in pygame.event.get(): if event.type == pygame.QUIT or pygame.key.get_pressed()[ pygame.K_ESCAPE]: sys.exit() self.player.move() if pygame.mouse.get_pressed()[0]: self.new_time = pygame.time.get_ticks() if (self.new_time - self.old_time >= 200): pygame.mixer.music.load("shot.mp3") self.bullets.add(self.player.fire()) pygame.mixer.music.set_volume(0.4) pygame.mixer.music.play() self.old_time = self.new_time if self.player.isAlive and not paused: self.collisions(self.enemies, self.bullets, False, True) self.collisions(self.entities, self.enemies, False, False) self.enemies_collision(self.enemies) self.window.update() self.entities.update() self.enemies.update(self.player.position()) self.bullets.update() self.entities.draw(self.window.screen) self.enemies.draw(self.window.screen) self.bullets.draw(self.window.screen) self.hud.update(self.statistics, self.player) self.clock.tick(60) pygame.display.update()
def __init__(self, singlemulti, level_number, parent=None): QGraphicsScene.__init__(self, parent) self.key_notifier = KeyNotifier() self.key_notifier.key_signal.connect(self.do_key_press) self.key_notifier.start() self.numberOfPlayer = singlemulti self.level_numberrr = level_number # Postavljanje pozadine self.set_background() #Postavljanje Glavnog igraca if (self.numberOfPlayer == 1): self.player = Player() self.player.setPos(400, 525) self.addItem(self.player) self.flag_playerOneDead = False elif (self.numberOfPlayer == 2): self.player = Player() self.player.setPos(400, 525) self.addItem(self.player) self.flag_playerOneDead = False self.player2 = Player2() self.player2.setPos(100, 525) self.addItem(self.player2) self.flag_playerTwoDead = False # Postavljanje neprijatelja self.enemies = [] self.enemies.append(Enemy()) self.enemies[0].setPos(100, 50) for i in range(0, 55): self.enemies.append(Enemy()) if i == 11: self.enemies[i].setPos(self.enemies[0].x(), self.enemies[0].y() + 60) continue if i == 22: self.enemies[i].setPos(self.enemies[11].x(), self.enemies[11].y() + 60) continue if i == 33: self.enemies[i].setPos(self.enemies[22].x(), self.enemies[22].y() + 60) continue if i == 44: self.enemies[i].setPos(self.enemies[33].x(), self.enemies[33].y() + 60) continue self.enemies[i].setPos(self.enemies[i - 1].x() + 60, self.enemies[i - 1].y()) for i in range(0, 55): self.addItem(self.enemies[i]) # Pomeranje neprijatelja self.moveEnemy = MoveEnemy() self.moveEnemy.calc_done.connect(self.move_enemy) self.moveEnemy.start() # Pucanje igraca self.shootLaser = PlayerShoot() self.shootLaser.calc_done.connect(self.move_laser_up) self.shootLaser.collision_detected.connect( self.player_laser_enemy_collide) self.shootLaser.start() self.playerOneCanShoot = True self.playerTwoCanShoot = True # Pucanje protivnika self.enemyShoot = EnemyShoot() self.enemyShoot.can_shoot.connect(self.enemy_shoot_laser) self.enemyShoot.move_down.connect(self.move_enemy_laser) self.enemyShoot.collision_detected.connect(self.enemy_hit_player) self.enemyShoot.collision_detected_with_shield.connect( self.enemy_laser_shield_collide) #self.enemyShoot.next_level.connect(self.next_level) if self.numberOfPlayer == 1: self.enemyShoot.add_player(self.player) else: self.enemyShoot.add_player(self.player) self.enemyShoot.add_player(self.player2) self.enemyShoot.start() for i in range(0, 55): self.moveEnemy.add_enemy(self.enemies[i]) self.shootLaser.add_enemy(self.enemies[i]) self.enemyShoot.add_enemy(self.enemies[i]) #Dodavanje stitova self.shields = [] self.shields.append(Shield()) self.shields[0].setPos(50, 350) self.shields.append(Shield()) self.shields[1].setPos(375, 350) self.shields.append(Shield()) self.shields[2].setPos(700, 350) for i in range(0, 3): self.addItem(self.shields[i]) self.enemyShoot.add_shield(self.shields[i]) self.view = QGraphicsView(self) self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.view.show() self.view.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT) self.setSceneRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) self.initUI(self.numberOfPlayer, self.level_numberrr)
class Window(QGraphicsScene): next_level = pyqtSignal(int) next_level2 = pyqtSignal(int) sound_invaderkilled = QtMultimedia.QSound( 'assets/sounds/invaderkilled.wav') sound_shipexplosion = QtMultimedia.QSound( 'assets/sounds/shipexplosion.wav') def __init__(self, singlemulti, level_number, parent=None): QGraphicsScene.__init__(self, parent) self.key_notifier = KeyNotifier() self.key_notifier.key_signal.connect(self.do_key_press) self.key_notifier.start() self.numberOfPlayer = singlemulti self.level_numberrr = level_number # Postavljanje pozadine self.set_background() #Postavljanje Glavnog igraca if (self.numberOfPlayer == 1): self.player = Player() self.player.setPos(400, 525) self.addItem(self.player) self.flag_playerOneDead = False elif (self.numberOfPlayer == 2): self.player = Player() self.player.setPos(400, 525) self.addItem(self.player) self.flag_playerOneDead = False self.player2 = Player2() self.player2.setPos(100, 525) self.addItem(self.player2) self.flag_playerTwoDead = False # Postavljanje neprijatelja self.enemies = [] self.enemies.append(Enemy()) self.enemies[0].setPos(100, 50) for i in range(0, 55): self.enemies.append(Enemy()) if i == 11: self.enemies[i].setPos(self.enemies[0].x(), self.enemies[0].y() + 60) continue if i == 22: self.enemies[i].setPos(self.enemies[11].x(), self.enemies[11].y() + 60) continue if i == 33: self.enemies[i].setPos(self.enemies[22].x(), self.enemies[22].y() + 60) continue if i == 44: self.enemies[i].setPos(self.enemies[33].x(), self.enemies[33].y() + 60) continue self.enemies[i].setPos(self.enemies[i - 1].x() + 60, self.enemies[i - 1].y()) for i in range(0, 55): self.addItem(self.enemies[i]) # Pomeranje neprijatelja self.moveEnemy = MoveEnemy() self.moveEnemy.calc_done.connect(self.move_enemy) self.moveEnemy.start() # Pucanje igraca self.shootLaser = PlayerShoot() self.shootLaser.calc_done.connect(self.move_laser_up) self.shootLaser.collision_detected.connect( self.player_laser_enemy_collide) self.shootLaser.start() self.playerOneCanShoot = True self.playerTwoCanShoot = True # Pucanje protivnika self.enemyShoot = EnemyShoot() self.enemyShoot.can_shoot.connect(self.enemy_shoot_laser) self.enemyShoot.move_down.connect(self.move_enemy_laser) self.enemyShoot.collision_detected.connect(self.enemy_hit_player) self.enemyShoot.collision_detected_with_shield.connect( self.enemy_laser_shield_collide) #self.enemyShoot.next_level.connect(self.next_level) if self.numberOfPlayer == 1: self.enemyShoot.add_player(self.player) else: self.enemyShoot.add_player(self.player) self.enemyShoot.add_player(self.player2) self.enemyShoot.start() for i in range(0, 55): self.moveEnemy.add_enemy(self.enemies[i]) self.shootLaser.add_enemy(self.enemies[i]) self.enemyShoot.add_enemy(self.enemies[i]) #Dodavanje stitova self.shields = [] self.shields.append(Shield()) self.shields[0].setPos(50, 350) self.shields.append(Shield()) self.shields[1].setPos(375, 350) self.shields.append(Shield()) self.shields[2].setPos(700, 350) for i in range(0, 3): self.addItem(self.shields[i]) self.enemyShoot.add_shield(self.shields[i]) self.view = QGraphicsView(self) self.view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.view.show() self.view.setFixedSize(WINDOW_WIDTH, WINDOW_HEIGHT) self.setSceneRect(0, 0, WINDOW_WIDTH, WINDOW_HEIGHT) self.initUI(self.numberOfPlayer, self.level_numberrr) def move_enemy(self, enemyPixMap: QGraphicsPixmapItem, newX, newY): enemyPixMap.setPos(newX, newY) def remove_enemy_label(self, enemy: QGraphicsPixmapItem): if enemy in self.enemies: self.enemies.remove(enemy) def remove_laser(self, laserLabel: QGraphicsPixmapItem): if laser in self.enemies: self.enemies.remove(enemy) self.enemyShoot.remove_enemy( enemy) #Da ne pucaju kad su vec pogodjeni def keyPressEvent(self, event): self.key_notifier.add_key(event.key()) def keyReleaseEvent(self, event): if (self.key_notifier.exists_key(event.key())): self.key_notifier.rem_key(event.key()) # POKUSAJ PROCESA def do_key_press(self, key): try: # proc = multiprocessing.Process(target = self.__update_position__, args = [key]) # proc.start() # proc.join() #(Process(target=__update_position__, args=[key])).start() self.__update_position__(key) except Exception as e: print('Exception: {}'.format(str(e))) def set_background(self): loadedPicture = QImage('assets/background.png') brushBackground = QBrush(loadedPicture) self.setBackgroundBrush(brushBackground) def player_laser_enemy_collide(self, enemyLabel: QGraphicsPixmapItem, laserLabel: QGraphicsPixmapItem): try: self.sound_invaderkilled.play() enemyLabel.hide() laserLabel.hide() self.shootLaser.remove_laser(laserLabel) self.shootLaser.remove_enemy(enemyLabel) self.remove_enemy_label(enemyLabel) self.enemyShoot.remove_enemy(enemyLabel) self.moveEnemy.remove_enemy(enemyLabel) if len(self.enemies) == 1: if (self.numberOfPlayer == 1): # Gasenje threadova self.shootLaser.die() self.moveEnemy.die() self.enemyShoot.die() # Unistavanje postojecih projektila na screenu for laser in self.shootLaser.laserLabels: self.removeItem(laser) for laser in self.enemyShoot.lasers: self.removeItem(laser) for shield in self.shields: self.removeItem(shield) self.shields.clear() # Povecavanje nivoa za jedan i restart neprijatelja update gui self.level_numberrr += 1 self.level_advance() self.Widget.setZValue(50) elif (self.numberOfPlayer == 2): # Gasenje threadova self.shootLaser.die() self.moveEnemy.die() self.enemyShoot.die() # Unistavanje postojecih projektila na screenu for laser in self.shootLaser.laserLabels: self.removeItem(laser) for laser in self.enemyShoot.lasers: self.removeItem(laser) for shield in self.shields: self.removeItem(shield) self.shields.clear() # Povecavanje nivoa za jedan i restart neprijatelja update gui self.level_numberrr += 1 self.level_advance() self.Widget.setZValue(50) except Exception as e: print( 'Exception in Main_Thread/player_laser_enemy_collide method: ', str(e)) def enemy_laser_shield_collide(self, laserLabel: QGraphicsPixmapItem, shieldLabel: QGraphicsPixmapItem): try: self.sound_shipexplosion.play() self.enemyShoot.remove_laser(laserLabel) laserLabel.hide() for shield in self.shields: if shield == shieldLabel: shield.makeDamage() if shield.health <= 0: self.enemyShoot.remove_shield(shield) self.shields.remove(shield) shield.update_shield(shield) except Exception as e: print(str(e)) def player_shoot_laser(self, laserLabel: QGraphicsPixmapItem, startX, startY): laserLabel.setPos(startX + PLAYER_BULLET_X_OFFSETS[0], startY - PLAYER_BULLET_Y) self.addItem(laserLabel) self.shootLaser.add_laser(laserLabel) def move_laser_up(self, laserLabel: QGraphicsPixmapItem, newX, newY): if newY > 0: laserLabel.setPos(newX, newY) else: laserLabel.hide() self.shootLaser.remove_laser(laserLabel) def enemy_shoot_laser(self, startX, startY): enemyLaserLabel = BulletEnemy() enemyLaserLabel.setPos(startX, startY) self.addItem(enemyLaserLabel) # dodamo laser da moze da se krece ka dole self.enemyShoot.add_laser(enemyLaserLabel) def move_enemy_laser(self, enemyLaser: QGraphicsPixmapItem, newX, newY): if newY < WINDOW_HEIGHT - 50: enemyLaser.setPos(newX, newY) else: enemyLaser.hide() self.enemyShoot.remove_laser(enemyLaser) def enemy_hit_player(self, laserLabel: QGraphicsPixmapItem, playerLabel: QGraphicsPixmapItem): self.sound_shipexplosion.play() laserLabel.hide() if self.player == playerLabel: self.player.loseLife() if self.numberOfPlayer == 2: if self.player2 == playerLabel: self.player2.loseLife() self.update_GUI_lives(self.numberOfPlayer) def __update_position__(self, key): if self.player: playerPos = self.player.pos() dx = 0 # Closing program if key == Qt.Key_Escape: self.shootLaser.die() self.moveEnemy.die() self.enemyShoot.die() self.key_notifier.die() self.view.hide() if playerPos.x() + dx <= 0: if key == Qt.Key_D: dx += PLAYER_SPEED elif playerPos.x() + dx >= 845: if key == Qt.Key_A: dx -= PLAYER_SPEED else: if key == Qt.Key_D: dx += PLAYER_SPEED if key == Qt.Key_A: dx -= PLAYER_SPEED self.player.setPos(playerPos.x() + dx, playerPos.y()) if key == Qt.Key_Space: if self.playerOneCanShoot: laserLabel = Bullet() self.player_shoot_laser(laserLabel, playerPos.x(), playerPos.y()) ## player 2 ## if self.numberOfPlayer == 2 and self.player2: playerPos2 = self.player2.pos() dx2 = 0 if playerPos2.x() + dx2 <= 0: if key == Qt.Key_Right: dx2 += PLAYER_SPEED elif playerPos2.x() + dx2 >= 845: if key == Qt.Key_Left: dx2 -= PLAYER_SPEED else: if key == Qt.Key_Right: dx2 += PLAYER_SPEED if key == Qt.Key_Left: dx2 -= PLAYER_SPEED self.player2.setPos(playerPos2.x() + dx2, playerPos2.y()) if key == Qt.Key_Control and self.player2: if self.playerTwoCanShoot: laserLabel2 = Bullet() self.player_shoot_laser(laserLabel2, playerPos2.x(), playerPos2.y()) def initUI(self, numberOfPlayer, level_number): self.horizontalLayoutWidget = QWidget() self.horizontalLayoutWidget.setGeometry(QtCore.QRect(13, 10, 871, 10)) self.horizontalLayoutWidget.setObjectName("horizontalLayoutWidget") self.horizontalLayout = QHBoxLayout(self.horizontalLayoutWidget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setSpacing(230) self.horizontalLayout.setObjectName("horizontalLayout") self.horizontalLayout.setAlignment(Qt.AlignLeft) #Zivoti prvog igraca self.lab_lives1 = QtWidgets.QLabel(self.horizontalLayoutWidget) self.lab_lives1.setEnabled(True) font = QtGui.QFont() font.setPointSize(16) font.setBold(True) font.setWeight(QFont.Bold) self.lab_lives1.setFont(font) self.lab_lives1.setObjectName("lab_lives1") self.lab_lives1.setStyleSheet("color:yellow") self.horizontalLayout.addWidget(self.lab_lives1) self.lab_lives1.setText("Player1 Lives: 3") #Level self.lab_level = QtWidgets.QLabel(self.horizontalLayoutWidget) self.lab_level.setEnabled(True) font = QtGui.QFont() font.setPointSize(16) font.setBold(True) font.setWeight(75) self.lab_level.setFont(font) self.lab_level.setObjectName("lab_level") self.lab_level.setStyleSheet("color:yellow") self.horizontalLayout.addWidget(self.lab_level) self.lab_level.setText("Level: " + str(level_number)) #Zivoti drugog igraca if self.numberOfPlayer == 2: self.lab_lives2 = QtWidgets.QLabel(self.horizontalLayoutWidget) self.lab_lives2.setEnabled(True) font = QtGui.QFont() font.setPointSize(16) font.setBold(True) font.setWeight(75) self.lab_lives2.setFont(font) self.lab_lives2.setObjectName("lab_lives2") self.lab_lives2.setStyleSheet("color:yellow") self.horizontalLayout.addWidget(self.lab_lives2) self.lab_lives2.setText("Player2 Lives: 3") self.horizontalLayoutWidget.show() self.horizontalLayoutWidget.setAttribute( QtCore.Qt.WA_TranslucentBackground) self.Widget = self.addWidget(self.horizontalLayoutWidget) def update_GUI_lives(self, playerNo): #if playerNo==1: lives = self.player.lives if lives == 3: self.lab_lives1Text = "Player1 Lives: 3" self.lab_lives1.setText(self.lab_lives1Text) elif lives == 2: self.lab_lives1Text = "Player1 Lives: 2" self.lab_lives1.setText(self.lab_lives1Text) elif lives == 1: self.lab_lives1Text = "Player1 Lives: 1" self.lab_lives1.setText(self.lab_lives1Text) elif lives <= 0: self.lab_lives1Text = "Player1 Lives: 0" self.lab_lives1.setText(self.lab_lives1Text) self.flag_playerOneDead = True self.player.hide() self.playerOneCanShoot = False #self.player = None if playerNo == 2: lives = self.player2.lives if lives == 3: self.lab_lives2Text = "Player2 Lives: 3" self.lab_lives2.setText(self.lab_lives2Text) elif lives == 2: self.lab_lives2Text = "Player2 Lives: 2" self.lab_lives2.setText(self.lab_lives2Text) elif lives == 1: self.lab_lives2Text = "Player2 Lives: 1" self.lab_lives2.setText(self.lab_lives2Text) elif lives <= 0: self.lab_lives2Text = "Player2 Lives: 0" self.lab_lives2.setText(self.lab_lives2Text) self.flag_playerTwoDead = True self.player2.hide() self.playerTwoCanShoot = False #self.player2 = None self.lab_levelText = "Level: {}".format(self.level_numberrr) self.lab_level.setText(self.lab_levelText) if self.numberOfPlayer == 1: if self.flag_playerOneDead == True: self.gameOver() elif self.numberOfPlayer == 2: if self.flag_playerOneDead == True and self.flag_playerTwoDead == True: self.gameOver() def gameOver(self): self.tempWidget = QWidget() self.tempWidget.setGeometry(QtCore.QRect(0, 0, 900, 600)) self.tempWidget.setObjectName("tempWidget") self.horizontalLayout = QHBoxLayout(self.tempWidget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setSpacing(230) self.horizontalLayout.setObjectName("horizontalLayout") self.horizontalLayout.setAlignment(Qt.AlignCenter) #Labela game over self.lab_gameOver = QtWidgets.QLabel(self.tempWidget) self.lab_gameOver.setEnabled(True) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(60) font.setBold(True) font.setWeight(QFont.Bold) self.lab_gameOver.setFont(font) self.lab_gameOver.setObjectName("lab_gameOver") self.lab_gameOver.setStyleSheet( "color: red; background-color: transparent;") self.horizontalLayout.addWidget(self.lab_gameOver) self.lab_gameOver.setText("GAME OVER") self.tempWidget.setStyleSheet( "background-color: rgba(255,255,255,70);") self.Widget = self.addWidget(self.tempWidget) self.moveEnemy.die() self.shootLaser.die() self.enemyShoot.die() self.key_notifier.die() def level_advance(self): self.levelUP() self.player.lives = 3 if (self.numberOfPlayer == 2): self.player2.lives = 3 self.player.show() self.player2.show() self.playerOneCanShoot = True self.playerTwoCanShoot = True self.flag_playerOneDead = False self.flag_playerTwoDead = False self.update_GUI_lives(self.numberOfPlayer) # Postavljanje neprijatelja self.enemies = [] self.enemies.append(Enemy()) self.enemies[0].setPos(100, 50) for i in range(0, 55): self.enemies.append(Enemy()) if i == 11: self.enemies[i].setPos(self.enemies[0].x(), self.enemies[0].y() + 60) continue if i == 22: self.enemies[i].setPos(self.enemies[11].x(), self.enemies[11].y() + 60) continue if i == 33: self.enemies[i].setPos(self.enemies[22].x(), self.enemies[22].y() + 60) continue if i == 44: self.enemies[i].setPos(self.enemies[33].x(), self.enemies[33].y() + 60) continue self.enemies[i].setPos(self.enemies[i - 1].x() + 60, self.enemies[i - 1].y()) for i in range(0, 55): self.addItem(self.enemies[i]) self.moveEnemy = MoveEnemy() self.moveEnemy.calc_done.connect(self.move_enemy) self.moveEnemy.start() self.enemyShoot = EnemyShoot() self.enemyShoot.can_shoot.connect(self.enemy_shoot_laser) self.enemyShoot.move_down.connect(self.move_enemy_laser) self.enemyShoot.collision_detected.connect(self.enemy_hit_player) self.enemyShoot.collision_detected_with_shield.connect( self.enemy_laser_shield_collide) # Ubrzavanje lasera igraca i enemya self.enemyShoot.enemyLaserSpeed += self.level_numberrr self.moveEnemy.enemyMoveSpeed += self.level_numberrr #print('Enemy move speed: {}'.format(self.moveEnemy.enemyMoveSpeed)) #print('Enemy laser speed: {}'.format(self.enemyShoot.enemyLaserSpeed)) if self.numberOfPlayer == 1: self.enemyShoot.add_player(self.player) else: # PROVERA AKO JE MRTAV NE IGRA VISE / A USTVARI IGRA PROCITANO IZ PROJEKTA, NEMA VEZE if self.player != None: self.enemyShoot.add_player(self.player) if self.player2 != None: self.enemyShoot.add_player(self.player2) self.enemyShoot.start() self.shootLaser = PlayerShoot() self.shootLaser.calc_done.connect(self.move_laser_up) self.shootLaser.collision_detected.connect( self.player_laser_enemy_collide) self.shootLaser.start() for i in range(0, 55): self.moveEnemy.add_enemy(self.enemies[i]) self.shootLaser.add_enemy(self.enemies[i]) self.enemyShoot.add_enemy(self.enemies[i]) #Dodavanje stitova self.shields = [] self.shields.append(Shield()) self.shields[0].setPos(50, 350) self.shields.append(Shield()) self.shields[1].setPos(375, 350) self.shields.append(Shield()) self.shields[2].setPos(700, 350) for i in range(0, 3): self.addItem(self.shields[i]) self.enemyShoot.add_shield(self.shields[i]) def levelUP(self): self.tempWidget = QWidget() self.tempWidget.setGeometry(QtCore.QRect(0, 0, 900, 600)) self.tempWidget.setObjectName("tempWidget") self.horizontalLayout = QHBoxLayout(self.tempWidget) self.horizontalLayout.setContentsMargins(0, 0, 0, 0) self.horizontalLayout.setSpacing(230) self.horizontalLayout.setObjectName("horizontalLayout") self.horizontalLayout.setAlignment(Qt.AlignCenter) #Labela next level self.lab_nextLevel = QtWidgets.QLabel(self.tempWidget) self.lab_nextLevel.setEnabled(True) font = QtGui.QFont() font.setFamily("Calibri") font.setPointSize(30) font.setBold(True) font.setWeight(QFont.Bold) self.lab_nextLevel.setFont(font) self.lab_nextLevel.setObjectName("lab_nextLevel") self.lab_nextLevel.setStyleSheet( "color: GREEN; background-color: transparent;") self.horizontalLayout.addWidget(self.lab_nextLevel) self.lab_nextLevel.setText("NEXT LEVEL") self.tempWidget.setStyleSheet("background-color: rgba(255,255,255,0)") self.WidgetLEVEL = self.addWidget(self.tempWidget) self.WidgetLEVEL.setZValue(20) self.timer = QTimer() self.timer.timeout.connect(self.levelUPdelete) self.timer.start(2000) def levelUPdelete(self): self.WidgetLEVEL.hide()
def main(): sdl2.ext.init() # Create the window window = sdl2.ext.Window("Foo", size=(960, 640)) window.show() # Create the spirte factory and the sprite for the player factory = sdl2.ext.SpriteFactory(sdl2.ext.SOFTWARE) # Create the worl and spriterenderer system world = sdl2.ext.World() spriterenderer = SoftwareRenderer(window) movement = MovementSystem(0, 0, 960, 640) playerMovement = PlayerMovementSystem() collision = CollisionSystem() battleSystem = BattleSystem() # Add all systems to the world world.add_system(playerMovement) world.add_system(collision) world.add_system(movement) world.add_system(spriterenderer) world.add_system(battleSystem) # Test map generation grid = mapGen.buildMap(world, factory, 4) grid.build(world, factory) enemyTile = random.choice(grid.tiles) enemy = Enemy(world, factory, enemyTile.position.x, enemyTile.position.y) # Pick random location for player playerTile = random.choice(grid.tiles) player = Player(world, factory, playerTile.position.x, playerTile.position.y) player_speed = 1 battleSystem.player = player playerMovement.player = player playerMovement.grid = grid gridSystem = GridSystem(960, 640, player) world.add_system(gridSystem) # Colliders for said items collision.player = player collision.grid = grid # Main event loop running = True key_down = False while(running): events = sdl2.ext.get_events() for event in events: # Hittin' dat X button doe if event.type == sdl2.SDL_QUIT: running = False break if event.type == sdl2.SDL_KEYUP: key_down = False # Movement if event.type == sdl2.SDL_KEYDOWN: d = None if(not key_down): if event.key.keysym.sym == sdl2.SDLK_UP: d = Direction.NORTH key_down = True elif event.key.keysym.sym == sdl2.SDLK_e: print("I'm attacking with", player.get_damage(), "!") key_down = True elif event.key.keysym.sym == sdl2.SDLK_f: print("Consuming") player.consume() print("Player health now at,", player.playerdata.health) elif event.key.keysym.sym == sdl2.SDLK_DOWN: d = Direction.SOUTH key_down = True elif event.key.keysym.sym == sdl2.SDLK_LEFT: d = Direction.EAST key_down = True elif event.key.keysym.sym == sdl2.SDLK_RIGHT: d = Direction.WEST key_down = True if d != None: collision.player_dir = d enemy.random_move(grid) playerMovement.walk(d) sdl2.SDL_Delay(10) world.process() return 0
from KeyHandler import KeyHandler from InteractionHandler import InteractionHandler from terrain.Terrain import * from Utils import * """ Testing Assets: """ pygame.init() screen = pygame.display.set_mode((600, 600), RESIZABLE) pygame.display.set_caption("Game") player = Player(0, 0, 0) interactionHandler = InteractionHandler(player) keyHandler = KeyHandler(player) entityHandler = EntityHandler(player) entityHandler.add_entity(Rock(100, 100, 0)) entityHandler.add_entity(LittleRock(100, 200, 0)) entityHandler.add_entity(Rick(100, 0, 0)) tileHandler = TileHandler() chunkHandler = ChunkHandler() def tick(): # interaction handler for player interaction
def get_players(): players = Player("Sebbe") players1 = Player('Linus') players2 = Player('Fredrik') return [players, players1, players2]
class WorldModel(object): DEBUG = False physWorld = None vel_iters, pos_iters = 6, 2 debugRender = None; player = None body = None dynamic_enities = [] mFirstUpdate = True mEntityToFollow = None mTimer = None mDeathTimer = 1.0 mSwitch = None def __init__(self, camera, luObserver, fxObserver, lvl): self.mSwitch = True self.mTimer = 0.0 self.mLevelDone = False self.mCamera = camera self.mLuObs = luObserver self.mFxObs = fxObserver self.contactListener = ContactListener() self.gravity = Gravity() self.physWorld = b2World(gravity=(0,0),doSleep=True, contactListener=self.contactListener) self.level = Level(self.physWorld, self.gravity, lvl) self.player = Player(self.level.mStartPos, self.physWorld, self.gravity) self.mEntityToFollow = self.player def __resetWorld(self): self.mSwitch = True self.mDeathTimer = 1.0 self.mTimer = 0.0 self.dynamic_enities = [] self.mFirstUpdate = True self.gravity.reset() self.player.reset(b2Vec2(self.level.mStartPos.x + self.player.size.x/3, self.level.mStartPos.y + self.player.size.y/2)) self.mLuObs.levelChanged(self.level) def restart(self): self.level.retryLevel() self.__resetWorld() def update(self, delta): self.mTimer += delta #step the physicsworld self.physWorld.Step(delta, self.vel_iters, self.pos_iters) self.physWorld.ClearForces() for body in self.physWorld.bodies: #dynamic body if body.type == b2_dynamicBody: if self.mFirstUpdate: if isinstance(body.userData, Player): self.dynamic_enities.append(body) if isinstance(body.userData, Player): body.userData.update(delta) elif isinstance(body.userData, Enemy): body.userData.update(delta) elif isinstance(body.userData, PickableObject): body.userData.update(delta) elif isinstance(body.userData, Particle): body.userData.update(delta) if self.mFirstUpdate == True: self.mFirstUpdate = False #update level, if level is done, returns true if self.level.update(delta, self.player.position): self.mLevelDone = True #is player dead? if not self.player.alive: if self.mSwitch: SoundManager.getInstance().playSound(SoundID.FLESHEXPLOSION) self.mFxObs.addFx(BloodSplatter(self.physWorld, self.gravity.get(), self.player.position)) self.mSwitch = False self.player.stopMovement() self.mDeathTimer -= delta if self.mDeathTimer < 0: self.level.retryLevel() self.__resetWorld() def changeGravity(self, gravitydirection): if self.player.isOnGround() == True: self.gravity.set(gravitydirection) for body in self.dynamic_enities: if not body.userData.isInGravityZone(): body.userData.flip(gravitydirection)
def add_player(id_): Entities.players.append( Player(id_, Entities.colors[id_], Constants.map_width_screen / 2, Constants.map_height_screen / 2))
turn = 0 while True: # TURN START # Update the map for the new turn and get the latest version game_map = game.update_map() # map game data to states and entities allPlayers = game_map.all_players() playerList = [] shipList = [] planetList = [] playerString = "" for p in allPlayers: playerList.append(Player(p.id)) playerString += str(p.id) + " : " for s in game_map.get_player(p.id).all_ships(): shipList.append(s) for p in game_map.all_planets(): planetList.append(p) logging.info(playerString) states.addPlayer(playerList) states.addPlanets(planetList) states.addShips(shipList) # Here we define the set of commands to be sent to the Halite engine at the end of the turn command_queue = [] # For every ship that I control for ship in game_map.get_me().all_ships():
from states.HaliteStates import HaliteStates from persistence.Files import Files from entities.Player import Player file = "2018-07-28 18:39:14.334851_1" state = HaliteStates() persistence = Files("/home/fstuck/repros/hunger-games/haliteChallenge/data/") p = Player(4) ''' playerList =[] playerList.append(Player(3)) playerList.append(Player(4)) playerList.append(Player(6)) playerList.append(Player(60)) state.addPlayer(playerList) ''' persistence.file = file #persistence.saveStateRandom(state) stateBot = persistence.accessFile(file) n = 0
def on_hover(self, player: Player): player.destroy()