def enemy_shot(self): x = SoundThread("shotLaserHeight.wav") x.start() self.ui.centralwidget.close() x = EnemyLaser(self.ui.centralwidget, cause_geo=self.geometry(), bullet_width=7, bullet_height=25) # aby poprawnie dodać element do główneg widgetu należy go wyłączyć a następnie pokazać, czli zresetować self.ui.centralwidget.show()
def player_death(self): self.window.main_game = False self.make_aftergame() self.check_score() self.music.stop() x = SoundThread("shotError.wav") x.start()
def return_btn_click(self): x = SoundThread("shotClick.wav") x.start() if self.etimer.isActive(): self.etimer.stop() self.core.reload_main_menu()
def select_skin(self, path): x = SoundThread("shotClick.wav") x.start() JsonConnector.save_key_to_config("skin", path) self.centralwidget.close() self.load_skins() self.core.window.resize_tlo() self.centralwidget.show()
def can_not_buy_anim(self, btn_instance): x = SoundThread("shotError.wav") x.start() if btn_instance.text() != "Can't buy": old_txt = btn_instance.text() btn_instance.setText("Can't buy") self.etimer.timeout.connect(lambda: self.restore_btn_text( old_txt, btn_instance, self.etimer)) self.etimer.start(1500)
def shot(self): # tworzy nowy strzał if self.can_shot: x = SoundThread("shotLaser.wav") x.start() self.ui.centralwidget.close() x = PlayerLaser(self.ui.centralwidget, cause_geo=self.geometry(), can_penetrate=self.laser_penetrate, bullet_width=self.ammo_size[0], bullet_height=self.ammo_size[1], bullet_distance=self.ammo_dist) # aby poprawnie dodać element do główneg widgetu należy go wyłączyć a następnie pokazać, czli zresetować self.ui.centralwidget.show() self.shot_accuracy_block()
def one_shot(self): x = SoundThread("shotLaserEnemy.wav") x.start() self.ui.centralwidget.close() x = EnemyLaser(self.ui.centralwidget, cause_geo=self.geometry(), bullet_width=12, bullet_height=55) # aby poprawnie dodać element do główneg widgetu należy go wyłączyć a następnie pokazać, czli zresetować self.ui.centralwidget.show() self.iter_shot -= 1 if self.iter_shot == 0: self.sstimer.stop()
def select_skill(self, index_): x = SoundThread("shotClick.wav") x.start() self.skill_list[index_]["selected"] = True JsonConnector.save_skills_dict(self.skill_list) self.load_skill_to_config(self.skill_list[index_]["name"]) self.centralwidget.close() self.load_skills() self.core.window.resize_tlo() self.centralwidget.show()
def open_chest(self): x = SoundThread("shotFanfar.wav") x.start() coins = Chest().open(Postac.score) self.coin_label.setText("Coins: " + str(JsonConnector.get_from_config("coins"))) self.chest_label.setText("Award: " + str(coins) + " coins") self.start_chest_anim() self.chest_btn.setParent(None) self.chest_btn.deleteLater() self.chest_btn = None
def buy_skill(self, skill_dict_index, btn_instance): coins = JsonConnector.get_from_config("coins") if coins >= self.skill_list[skill_dict_index]["price"]: self.skill_list[skill_dict_index]["available"] = True JsonConnector.save_skills_dict(self.skill_list) coins -= self.skill_list[skill_dict_index]["price"] JsonConnector.save_key_to_config("coins", coins) self.make_coin_label() x = SoundThread("shotFanfar.wav") x.start() self.centralwidget.close() self.load_skills() self.core.window.resize_tlo() self.centralwidget.show() else: self.can_not_buy_anim(btn_instance)
def skills_btn_click(self): x = SoundThread("shotClick.wav") x.start() self.core.load_skills_menu()
class GameCore(object): ui = None window = None def start(self): self.window = MyMainWindow() self.show_main_menu() self.window.showFullScreen() self.start_menu_music() # self.window.show() def show_main_menu(self): self.ui = Ui_MainMenu() self.set_menu_config() self.ui.setupUi(self.window) def reload_main_menu(self): self.ui.centralwidget.close() del self.ui self.show_main_menu() self.window.resize_tlo() self.ui.centralwidget.show() def set_menu_config(self): self.cls_game_config(self.ui, self.window) Ui_MainMenu.core = self self.window.ui = self.ui @classmethod def cls_game_config(cls, ui, window): cls.ui = ui cls.window = window def set_game_config(self, ui, window): self.cls_game_config(ui, window) Postac.set_parrent_objects(window, ui) Postac.core = self self.window.ui = ui self.ui.okno = window def play_main_game(self): self.reload_main_game() self.stop_menu_music() def reload_main_game(self): self.ui.centralwidget.close() del self.ui self.ui = Ui_MainWindow() self.set_game_config(self.ui, self.window) self.ui.setupUi(self.window) self.window.resize_tlo() Postac.score = 0 self.ui.centralwidget.show() self.window.main_game = True Postac.start_enemy_clock() self.music = SoundThread("shotMainSoundtrack.wav", True) self.music.start() def make_aftergame(self): self.ui.centralwidget.close() del self.ui self.load_aftergame() def load_aftergame(self): self.ui = UiAfterGame() self.set_aftergame_config() self.ui.setupUi(self.window) self.window.resize_tlo() self.ui.centralwidget.show() def set_aftergame_config(self): self.cls_game_config(self.ui, self.window) self.ui.core = self self.window.ui = self.ui def load_shop(self): self.ui.centralwidget.close() del self.ui self.ui = UiSkinSkills() self.shop_config() self.ui.setupUi(self.window, "skins") self.window.resize_tlo() self.ui.centralwidget.show() def shop_config(self): self.window.ui = self.ui self.cls_game_config(self.ui, self.window) self.ui.core = self def load_skills_menu(self): self.ui.centralwidget.close() del self.ui self.ui = UiSkinSkills() self.skills_menu_config() self.ui.setupUi(self.window, "skills") self.window.resize_tlo() self.ui.centralwidget.show() def skills_menu_config(self): self.window.ui = self.ui self.cls_game_config(self.ui, self.window) self.ui.core = self def player_death(self): self.window.main_game = False self.make_aftergame() self.check_score() self.music.stop() x = SoundThread("shotError.wav") x.start() def check_score(self): x = JsonConnector.get_from_config("highscore") if x < Postac.score: JsonConnector.save_key_to_config("highscore", Postac.score) def start_menu_music(self): self.menu_music = SoundThread("shotMenuSoundtrack.wav", loop=True) self.menu_music.start() def stop_menu_music(self): self.menu_music.stop()
def start_btn_click(self): x = SoundThread("shotClick.wav") x.start() self.core.reload_main_game()
def exit_btn_click(self): if self.chest_timer: self.chest_timer.stop() x = SoundThread("shotError.wav") x.start() sys.exit()
def play_hit_sound(): sound = SoundThread("shotLaserExplosion.wav", loop=False) sound.start()
def menu_btn_click(self): x = SoundThread("shotClick.wav") x.start() self.core.reload_main_menu() self.core.start_menu_music()
def exit_btn_click(self): x = SoundThread("shotError.wav") x.start() self.core.stop_menu_music() sys.exit()
class Benek(Postac): def __init__(self, layout, move_distance=100, *args): super().__init__(layout, *args) self.clocks = [] self.load_player_config() self.set_player_geo() self.setText("") self.setPixmap(QtGui.QPixmap("images/skins/" + JsonConnector.get_from_config("skin") + ".png")) self.setScaledContents(True) # odpowiada za pozwolenie na skalowanie grafik self.move_distance = self.okno.gWidth / move_distance self.can_shot = True self.playing_sound = False self.rtimer = QTimer() self.clocks.append(self.rtimer) self.rtimer.timeout.connect(self.ruch_prawo) self.ltimer = QTimer() self.clocks.append(self.ltimer) self.ltimer.timeout.connect(self.ruch_lewo) self.shot_block_timer = QTimer() self.clocks.append(self.shot_block_timer) self.shot_block_timer.timeout.connect(self.shot_accuracy_unblock) self.make_move_animations() self.make_reload_bar() def load_player_config(self): config = JsonConnector.get_from_config("shipConfig") self.laser_penetrate = config["penetrate"] self.ship_size = config["shipSize"] self.fire_speed = config["fireSpeed"] self.ammo_size = config["ammoSize"] self.ammo_dist = config["ammoSpeed"] def set_player_geo(self): size = int(self.okno.gWidth/self.ship_size) self.setGeometry(QtCore.QRect((self.okno.width() - size)/2, self.okno.height() - size, size, size)) def rusz_benek_prawo(self): self.rtimer.start(15) self.pg = int((self.okno.width() - self.okno.gWidth) / 2 + self.okno.gWidth) # prawa granica self.x_ = self.geometry().x() self.move_sound_start() self.expand_anim_movie(self.lmove_anim) def ruch_prawo(self): if self.pg >= (self.x_ + self.width()): # zapezpiecza postać przed wyjściem poza okno if (self.pg - (self.x_ + self.width())) < self.move_distance: # jeśli odległość od lewego krańca jest mniejsza od ruchu to zmienia pozycje benka na krańcową self.x_ = self.pg - self.width() else: self.x_ += self.move_distance geo = QtCore.QRect(self.x_, self.geometry().y(), self.geometry().width(), self.geometry().height()) self.setGeometry(geo) def stop_rusz_benek_prawo(self): self.rtimer.stop() self.move_sound_stop() self.collapse_anim_movie(self.lmove_anim) def rusz_benek_lewo(self): self.ltimer.start(15) self.x_ = self.geometry().x() self.lg = int((self.okno.width() - self.okno.gWidth) / 2) # lewa granica self.move_sound_start() self.expand_anim_movie(self.rmove_anim) def ruch_lewo(self): if self.x_ > self.lg: # zabezpiecza przed wyjściem benka poza ekran if self.x_ - self.lg < self.move_distance: # jeśli benkowi brakuje mniej niż dystans ruchu to przyjmuje zerową pozycje self.x_ = self.lg else: self.x_ -= self.move_distance geo = QtCore.QRect(self.x_, self.geometry().y(), self.geometry().width(), self.geometry().height()) self.setGeometry(geo) def stop_rusz_benek_lewo(self): self.ltimer.stop() self.move_sound_stop() self.collapse_anim_movie(self.rmove_anim) def shot(self): # tworzy nowy strzał if self.can_shot: x = SoundThread("shotLaser.wav") x.start() self.ui.centralwidget.close() x = PlayerLaser(self.ui.centralwidget, cause_geo=self.geometry(), can_penetrate=self.laser_penetrate, bullet_width=self.ammo_size[0], bullet_height=self.ammo_size[1], bullet_distance=self.ammo_dist) # aby poprawnie dodać element do główneg widgetu należy go wyłączyć a następnie pokazać, czli zresetować self.ui.centralwidget.show() self.shot_accuracy_block() def shot_accuracy_block(self): self.can_shot = False self.shot_block_timer.start(self.fire_speed) self.movie.start() def shot_accuracy_unblock(self): self.can_shot = True self.shot_block_timer.stop() self.movie.stop() self.movie.jumpToFrame(self.movie.frameCount()-1) def move_sound_start(self): if not self.playing_sound: self.playing_sound = True self.move_sound = SoundThread("shotEngineNoise.wav", loop=True) self.clocks.append(self.move_sound) self.move_sound.start() def move_sound_stop(self): if self.playing_sound and not (self.ltimer.isActive() or self.rtimer.isActive()): self.move_sound.stop() self.clocks.remove(self.move_sound) self.playing_sound = False def make_reload_bar(self): self.reload_bar = QLabel(self) self.reload_bar.setGeometry(QtCore.QRect(int(self.width()/6), 0, int(self.width()/6), int(self.height()/2))) self.reload_bar.setText("aa") self.reload_bar.setPixmap(QtGui.QPixmap("images/reloadBar.gif")) self.reload_bar.setScaledContents(True) self.movie = QMovie("images/reloadBar.gif", QByteArray(), self.reload_bar) self.movie.setCacheMode(QMovie.CacheAll) self.movie.setSpeed(100 * 1000 / self.fire_speed) # ustala tak prędkość aby pokrywała się z szybkostrzelnością self.reload_bar.setMovie(self.movie) self.movie.jumpToFrame(self.movie.frameCount()-1) def make_move_animations(self): self.lmove_lab = QLabel(self) self.lmove_lab.setText("") self.lmove_lab.setGeometry(QtCore.QRect(0, 0, int(self.width() / 3), self.height())) self.lmove_lab.setScaledContents(True) self.lmove_anim = QMovie("images/lPlayerMove.gif", QByteArray(), self.lmove_lab) self.lmove_anim.setCacheMode(QMovie.CacheAll) self.lmove_anim.setSpeed(7000) self.lmove_lab.setMovie(self.lmove_anim) self.lmove_anim.jumpToFrame(0) self.rmove_lab = QLabel(self) self.rmove_lab.setText("") self.rmove_lab.setGeometry(QtCore.QRect(int(self.width() / 3*2), 0, int(self.width() / 3), self.height())) self.rmove_lab.setScaledContents(True) self.rmove_anim = QMovie("images/rPlayerMove.gif", QByteArray(), self.rmove_lab) self.rmove_anim.setCacheMode(QMovie.CacheAll) self.rmove_anim.setSpeed(7000) self.rmove_lab.setMovie(self.rmove_anim) self.rmove_anim.jumpToFrame(0) self.rmove_anim.frameCount() def expand_anim_movie(self, movie): movie.jumpToFrame(int(movie.frameCount() / 2)) def collapse_anim_movie(self, movie): movie.jumpToFrame(0)
def shop_btn_click(self): x = SoundThread("shotClick.wav") x.start() self.core.load_shop()