def __init__(self): self.path = DATABASE # Path database self.sql = None self.id = 1 self.coint = 0 self.name = "Ocong" self.logs = Logs()
def __init__(self): self.config = Config() self.core = Core() self.logs = Logs() self.database = Database() self.data = {} # Data assets self.running = True
def __init__(self): super().__init__() self.logs = Logs() # Funtion logs self.path = CONFIG_PATH # config path self.data = {} # data (null) self.rect = Rect(0, 0, 960, 540)
def __init__(self): self.logs = Logs() self.data = { "backgrounds": {}, "textures": {}, "anim": {}, "other": {}, "sounds": {}, "effects": {}, "fonts": {}, "icon": False } self.config = Config() self.config.read()
class Config(object): def __init__(self): super().__init__() self.logs = Logs() # Funtion logs self.path = CONFIG_PATH # config path self.data = {} # data (null) self.rect = Rect(0, 0, 960, 540) @property def parse(self): #=== parse data ===# return self.data @property def getVersion(self): try: return str(self.data["informations"]["version"]) except KeyError: self.logs.error("Tidak dapat mengambil data!", keluar=True) @property def getName(self): try: return self.data["informations"]["name"] except KeyError: self.logs.error("Tidak dapat mengambil data!", keluar=True) @property def getInformation(self): #=== get information from config ===# try: return self.data["informations"] except KeyError: self.logs.error("Tidak dapat mengambil data!", keluar=True) @property def getRect(self): return self.rect def read(self): #==== membaca config ====# try: buka = open(self.path, "r").read() self.data = json.loads(buka) return True except IOError: self.logs.error("Tidak dapat membuka config!", keluar=True) return False
class Database: def __init__(self): self.path = DATABASE # Path database self.sql = None self.id = 1 self.coint = 0 self.name = "Ocong" self.logs = Logs() #=== Function connect to database ===# def connect(self): self.logs.info("Menghubungkan ke database") self.sql = sqlite3.connect(self.path) def getProfile(self): if self.sql: select = self.sql.execute("SELECT * FROM profile") for row in select: self.id = row[0] self.coint = row[1] self.name = row[2] return self.id, self.coint, self.name else: self.logs.error("Kamu belum menghubungkan ke database", keluar=True) def updateScore(self, score): if self.sql: id, coint, name = self.getProfile() c = self.sql.cursor() c.execute("UPDATE profile SET coint=%d WHERE id=%d" % (coint+score, id)) self.sql.commit() else: self.logs.error("Kamu belum menghubungkan ke database")
class Acticity: def __init__(self): self.config = Config() self.core = Core() self.logs = Logs() self.database = Database() self.data = {} # Data assets self.running = True def setup(self): pygame.init() self.database.connect() self.config.read() self.data = self.core.get_data self.speed = self.config.parse["settings"]["speed"] set_caption(self.config.getName + " " + self.config.getVersion) self.screen = set_mode(self.config.getRect.size) self.core.load_assets() if self.data["icon"]: self.logs.info("Mengatur icon") set_icon(self.data["icon"]) set_visible(True) self.clock = Clock() #=== Background game ===# self.BACKGROUND = self.data["backgrounds"][random.choice( [x for x in self.data["backgrounds"]][1:])] self.BACKGROUND = scale(self.BACKGROUND, self.config.getRect.size) #=== Settings tanah texture ===# self.tanah = self.data["textures"]["tanah0"] self.tanah = scale(self.tanah, (1000, self.tanah.get_height())) self.tanah_diff = self.tanah.get_width() - self.BACKGROUND.get_width() self.tanah_position = [0, self.config.getRect.height * 0.95] #=== Settings grass texture ===# self.grass = self.data["textures"]["grass0"] self.grass = scale(self.grass, (1000, 10)) self.grass_diff = self.grass.get_width() - self.BACKGROUND.get_width() self.grass_position = [10, self.config.getRect.height * 0.94] #=== Settings rumput texture ===# rumput_image_list = [] for x in self.data["textures"]: if "rumput" in x: rumput_image_list.append(x) self.rumputGroup = Group() for x in range(random.randint(2, 4)): image = self.data["textures"]["rumput0"] self.rumputGroup.add( Rumput(image, [ random.randint(self.config.getRect.width, 3000), self.config.getRect.height * 0.80 ])) #=== Settings keris image ===# self.kerisGroup = Group() for x in range(random.randint(1, 3)): keris = Keris(self.data["anim"]["keris"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) keris.rect.bottom += random.randint( 10, self.config.getRect.height - 130) self.kerisGroup.add(keris) #=== Settings bambu image ===# self.bambuGroup = Group() for x in range(random.randint(1, 3)): bambu = Bambu(self.data["other"]["bambu"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) bambu.rect.bottom += self.config.getRect.height * 0.78 self.bambuGroup.add(bambu) #=== Settings player image ===# self.player = Player(self.data["anim"]["player1"], [60, self.config.getRect.height - 40]) #=== Settings score ===# self.score = Score(self.data) #=== Health bar ===# self.health = Health(self.data) def run(self): self.setup() self.running = introGame(self.screen, self.data, self.database.getProfile()) add_rumput = True add_keris = True add_bambu = True while self.running: events = self.core.events() if events["type"]["quit"]: self.running = False break elif events["type"]["keydown"]: if events["keys"]["space"] or events["keys"]["up"]: self.player.jump_up() #=== Move position ===# boundary_values = [0, self.grass_position[-1]] self.player.update( boundary_values, float( self.clock.tick(self.config.parse["settings"]["frame"]) / 1000)) self.tanah_position[0] = -( (-self.tanah_position[0] + self.speed) % self.tanah_diff) self.grass_position[0] = -( (-self.grass_position[0] + self.speed) % self.grass_diff) #=== Move rumput object ===# for rumput in self.rumputGroup: rumput.rect.left -= self.config.parse["settings"]["speed"] if rumput.rect.left < 4 and rumput.rect.left > 0 and add_rumput: if len(self.rumputGroup) > 50: continue else: image = self.data["textures"][ "rumput" + str(random.randint(1, 6))] self.rumputGroup.add( Rumput(image, [ random.randint(self.config.getRect.width, 3000), self.config.getRect.height * 0.80 ])) elif rumput.rect.right < 0: if len(self.rumputGroup) > 50: self.rumputGroup.remove(rumput) else: self.rumputGroup.remove(rumput) image = self.data["textures"][ "rumput" + str(random.randint(1, 6))] self.rumputGroup.add( Rumput(image, [ random.randint(self.config.getRect.width, 3000), self.config.getRect.height * 0.80 ])) #=== Move keris object ===# for keris in self.kerisGroup: keris.rect.left -= self.config.parse["settings"]["speed"] #=== Check object to object ===# if collide_mask(self.player, keris) and not keris.is_used: keris.is_used = True self.kerisGroup.remove(keris) add_keris = True self.health.add_value() if self.score.add_score(): self.running = False break if keris.rect.left < 4 and keris.rect.left > 0 and add_keris: if self.health.revalue(): self.running = False break keris = Keris(self.data["anim"]["keris"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) keris.rect.bottom += random.randint( 10, self.config.getRect.height - 130) self.kerisGroup.add(keris) add_keris = False elif add_keris and keris.is_used: if random.randint(0, 1) == 1: for _ in range(random.randint(1, 2)): keris = Keris(self.data["anim"]["keris"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) keris.rect.bottom += random.randint( 10, self.config.getRect.height - 130) self.kerisGroup.add(keris) else: keris = Keris(self.data["anim"]["keris"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) keris.rect.bottom += random.randint( 10, self.config.getRect.height - 130) self.kerisGroup.add(keris) add_keris = False elif keris.rect.right < 0: self.health.revalue() self.kerisGroup.remove(keris) keris = Keris(self.data["anim"]["keris"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) keris.rect.bottom += random.randint( 10, self.config.getRect.height - 130) self.kerisGroup.add(keris) for bambuEnemy in self.bambuGroup: bambuEnemy.rect.left -= self.config.parse["settings"]["speed"] if collide_mask(self.player, bambuEnemy) and not bambuEnemy.is_used: bambuEnemy.is_used = True add_bambu = True self.health.revalue() self.score.rescore() if bambuEnemy.rect.left < 4 and bambuEnemy.rect.left > 0 and add_bambu: bambu = Bambu(self.data["other"]["bambu"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) bambu.rect.bottom += self.config.getRect.height * 0.78 self.bambuGroup.add(bambu) add_bambu = False elif bambuEnemy.rect.right < 0: self.bambuGroup.remove(bambuEnemy) bambu = Bambu(self.data["other"]["bambu"], [ random.randint(self.config.getRect.width, self.config.getRect.width * 2), 10 ]) bambu.rect.bottom += self.config.getRect.height * 0.78 self.bambuGroup.add(bambu) #=== Screen display ===# self.screen.blit(self.BACKGROUND, (0, 0)) self.rumputGroup.draw(self.screen) self.screen.blit(self.tanah, self.tanah_position) self.screen.blit(self.grass, self.grass_position) self.bambuGroup.draw(self.screen) self.health.draw(self.screen) self.kerisGroup.draw(self.screen) self.player.draw(self.screen) self.score.draw(self.screen) #=== Check ===# if self.score.check(): self.running = False elif self.health.check(): self.running = False self.clock.tick(self.config.parse["settings"]["frame"]) update() self.logs.warning("Menyimpan score") self.database.updateScore(self.score.getScore) self.logs.warning("Menutup game...") pygame.quit()
'%s_opt_G.pkl' % opt.which_epoch) if os.path.exists(path_opt_G): opt_G.load_state_dict(torch.load(path_opt_G)) if hasattr(model, 'dis_params'): opt_D = Adam(model.dis_params, lr=opt.lr, betas=(opt.beta1, 0.999)) path_opt_D = os.path.join(model.weights_path, '%s_opt_D.pkl' % opt.which_epoch) if os.path.exists(path_opt_D): opt_D.load_state_dict(torch.load(path_opt_D)) logs = Logs(model, opt) epoch_start = 0 if opt.which_epoch == 'latest' else int(opt.which_epoch) for epoch in range(epoch_start + 1, opt.num_epoch + 1): model.train() for inputs in train_dataloader: model.forward(inputs) if hasattr(model, 'dis_params'): for p in model.dis_params: p.requires_grad = False
class Core: def __init__(self): self.logs = Logs() self.data = { "backgrounds": {}, "textures": {}, "anim": {}, "other": {}, "sounds": {}, "effects": {}, "fonts": {}, "icon": False } self.config = Config() self.config.read() @property def get_data(self): return self.data def read(self, path="", convert=False, convert_alpha=False): #=== Load image ===# try: resource = load(path) except error: self.logs.error("Tidak dapat memuat gambar! " + path, keluar=True) if convert: return resource.convert() elif convert_alpha: return resource.convert_alpha() else: return resource def load_assets(self): #=== Load background asset ===# for background in BACKGROUND_IMAGE: name = get_name_file(background) image = self.read(path=background, convert_alpha=True, convert=False) self.data["backgrounds"][name] = image #=== Load texture asset ===# for texture in TEXTURE_IMAGE: name = get_name_file(texture) image = self.read(path=texture, convert_alpha=True, convert=False) self.data["textures"][name] = image #=== Load anim asset ===# pocng_name = get_name_file(POCONG_IMAGE) print(POCONG_IMAGE) image = self.read(path=POCONG_IMAGE, convert_alpha=True, convert=False) self.data["anim"][pocng_name] = flip(image, True, False) keris_name = get_name_file(KERIS_IMAGE) image = self.read(path=KERIS_IMAGE, convert_alpha=True, convert=False) self.data["anim"][keris_name] = image #=== Load other asset ===# for other in OTHERS: name = get_name_file(other) image = self.read(path=other, convert_alpha=True, convert=False) self.data["other"][name] = image #=== Load sound asset ===# for sound_file in SOUND: name = get_name_file(sound_file) sound = Sound(sound_file) self.data["sounds"][name] = sound #=== Load effects asset ===# for effects_file in EFFECTS: name = get_name_file(effects_file) sound = Sound(effects_file) sound.set_volume(self.config.parse["settings"]["volume"] / 10) self.data["effects"][name] = sound #=== Load fonts ===# for fonts in FONTS: name = get_name_file(fonts) self.data["fonts"][name] = fonts self.data["icon"] = self.read(path=ICON_GAME, convert_alpha=True, convert=False) def events(self): events_dict = { "type": { "quit": False, "keydown": False, "keyup": False, "mousedown": False }, "keys": { "ESC": False, "space": False, "up": False, "button": 0 } } for event in get(): if event.type == QUIT: events_dict["type"]["quit"] = True elif event.type == MOUSEBUTTONDOWN: events_dict["type"]["mousedown"] = True if event.button == 1: events_dict["keys"]["button"] = 1 elif event.button == 2: events_dict["keys"]["button"] = 2 elif event.button == 3: events_dict["keys"]["button"] = 3 elif event.type == KEYDOWN: events_dict["type"]["keydown"] = True if event.key == K_SPACE: events_dict["keys"]["space"] = True elif event.key == K_UP: events_dict["keys"]["up"] = True return events_dict
from pygame.display import update from pygame.transform import scale from pygame.mouse import get_pos from pygame.surface import Surface from pygame.freetype import Font from src.config import Config from src.core import Core from src.logs import Logs config = Config() core = Core() logs = Logs() def introGame(screen, asset, data): #=== Background image ===# logs.info("Mengatur latar belakang...") bg = scale(asset["backgrounds"]["intro"], (config.getRect.size)) #=== Button play image ===# logs.info("Mengatur button...") btn_play = asset["other"]["btn-play2"] btn_play = scale( btn_play, (int(btn_play.get_width() / 2), btn_play.get_height() - 100)) btn_play_center = btn_play.get_rect() btn_play_center.center = config.getRect.center #=== button keluar image ===# btn_keluar = asset["other"]["btn-keluar2"] btn_keluar = scale(