def __init__(self, Tetris3d): self.Tetris3d = Tetris3d self.data = Tetris3d.data self.time = glutGet(GLUT_ELAPSED_TIME) self.state = 'mainMenu' self.menuitem = 'start' self.texture = Textures() self.tstring = TextureString(Tetris3d.data) self.playername = '' self.planes = {} self.skins = {} self.stringPlane = {} self.textureWidthRatio = 512.0 / 640.0 self.textureHeightRatio = 512.0 / 480.0 self.generateStringTexture('mainMenu') self.generateStringTexture('gameoverMenu') self.generateStringTexture('enterName') self.generateStringTexture('pause') self.generateStringTexture('play') self.generateStringTexture('highscore') self.backgroundTexture = self.texture.createTex(self.data.get('texture/menu/menu.jpg')) self.stringTexture = self.texture.createTex(self.stringPlane['mainMenu']) self.planes['0_background'] = {'texture': self.backgroundTexture, 'coord': [16.7, 12.525, 16.6]} self.planes['1_strings'] = {'texture': self.stringTexture, 'coord': [16.7, 12.525, 16.59]} self.cubePos = {'mainMenu': { 'start': [4,4.6,0], 'highscore': [10,0.2,0], 'exit': [3,-4,0] }, 'gameoverMenu': { 'start': [10.5,2.4,0], 'highscore': [10,-2,0], 'exit': [3,-6.2,0] }} self.cube = TetrisCube(1, self.cubePos[self.state][self.menuitem])
def update(self): collision_list = arcade.check_for_collision_with_list( self, self.level.entities) for entity in collision_list: if isinstance(entity, Projectile): self.hurt(entity.damage, entity.change_x) player = self.level.player dist = Maths.manhattan_dist(self.center_x, self.center_y, player.center_x, player.center_y) if dist <= self.range: self.move_to(player) else: self.wander() if self.curr_invis_frame > 0 and self.curr_invis_frame % 12 < 6: self.texture = Textures.get_texture(15, 15) else: self.texture = self.tex if self.intersects(player): player.hurt(self.damage, self.change_x) super().update()
def main(): textures = Textures.Textures(os.path.join(TEXTURES_FOLDER, TEXTURE)) window = Game.Game(textures=textures, width=800, height=600, caption='Pyglet', resizable=True) # Hide the mouse cursor and prevent the mouse from leaving the window. window.set_exclusive_mouse(True) setup() pyglet.app.run()
def __init__(self, camera, keyboard): self.camera = camera self.keyboard = keyboard self.width = WIDTH // TILE_SIZE self.height = HEIGHT // TILE_SIZE self.tile_list = arcade.SpriteList(use_spatial_hash=True, spatial_hash_cell_size=32, is_static=True) self.entities = arcade.SpriteList(use_spatial_hash=True, spatial_hash_cell_size=64) self.particles = arcade.SpriteList(is_static=True) self.tiles = {} self.reset = False self.reset_timer = -1 # for i in range(100): # ball = Ball(Textures.get_texture(2, 5), 128 * random.random(), 128 * random.random()) # ball.change_x = random.randint(-8, 8) # ball.change_y = random.randint(-8, 8) # self.add_entity_to_list(ball, self.entities) self.player = Player(64, 64, self.keyboard) self.add_entity_to_list(self.player, self.entities) self.level_gen = LevelGenerator.LevelGen(self) self.paused = True self.difficulty = 1 self.engine = Engine(self.entities, self.tile_list, self, GRAVITY) self.physics_engine = arcade.PhysicsEnginePlatformer(self.player, self.tile_list, GRAVITY) self.pause_text = Graphics.create_text_list("Paused", 0, 0, True) self.game_over_text = Graphics.create_text_list("Game Over :<", 0, 0, True) self.game_over = False self.game_over_timer = 0 self.curr_health = self.player.health self.health_bar = arcade.SpriteList() for i in range(3): heart = arcade.Sprite() heart.center_x = self.player.center_x - TILE_SIZE + i * TILE_SIZE heart.center_y = self.player.center_y - TILE_SIZE * 1.5 heart.texture = Textures.get_texture(4, 9) self.health_bar.append(heart) self.setup()
def update(self, delta): self.level_gen.update() if not (self.level_gen.generating or self.level_gen.drawing or self.paused or self.game_over): self.engine.update() if self.game_over_timer >= 0: self.game_over_timer -= 1 if self.game_over_timer == 0: self.game_over = False self.reset = True if self.reset_timer >= 0: self.reset_timer -= 1 if self.reset_timer == 0: self.reset = True if self.reset: self.difficulty += 1 self.reset_level() self.generate_level(self.player.center_x, self.player.center_y) self.reset = False remainder = self.player.health if self.player.health > 0 else 0 for i, health in enumerate(self.health_bar): health.center_x = self.player.center_x - TILE_SIZE + i * TILE_SIZE health.center_y = self.player.center_y - TILE_SIZE * 1.5 if self.player.health < self.curr_health: if remainder >= 3: health.texture = Textures.get_texture(4, 9) remainder -= 3 else: health.texture = Textures.get_texture(4 + 3 - remainder, 9) remainder = 0 self.curr_health = self.player.health
def __init__(self, x, y, keyboard): self.keyboard = keyboard self.movespeed = 2.5 self.jump_height = 4 self.jumping = False self.max_attack_speed = 12 self.curr_attack_speed = 0 self.attack_dir = 0 self.curr_jump_height = 0 self.min_jump_height = 8 self.max_jump_height = 64 self.walk_count = 0 self.walk_frame_speed = 8 self.not_mirrored = True self.curr_dash_frame = 0 self.dash_frame_speed = 12 self.dashing = False self.crawling = False self.curr_crawl_frame = 0 self.crawl_frame_speed = 16 self.health = 9 self.curr_invis_frame = 0 self.invis_frame = 150 # Textures self.idle_texture = Textures.get_texture(0, 4) self.idle_texture_mirrored = Textures.get_texture(0, 5) self.walking_textures = Textures.get_textures(1, 4, 4) self.walking_textures_mirrored = Textures.get_textures(1, 5, 4) self.dash_textures = Textures.get_textures(5, 4, 3) self.dash_textures_mirrored = Textures.get_textures(5, 5, 3) self.crawl_textures = Textures.get_textures(7, 4, 4) self.crawl_textures_mirrored = Textures.get_textures(7, 5, 4) super().__init__(self.idle_texture, x, y)
class TetrisHUD(GameObject): def __init__(self, Tetris3d): self.Tetris3d = Tetris3d self.data = Tetris3d.data self.time = glutGet(GLUT_ELAPSED_TIME) self.state = 'mainMenu' self.menuitem = 'start' self.texture = Textures() self.tstring = TextureString(Tetris3d.data) self.playername = '' self.planes = {} self.skins = {} self.stringPlane = {} self.textureWidthRatio = 512.0 / 640.0 self.textureHeightRatio = 512.0 / 480.0 self.generateStringTexture('mainMenu') self.generateStringTexture('gameoverMenu') self.generateStringTexture('enterName') self.generateStringTexture('pause') self.generateStringTexture('play') self.generateStringTexture('highscore') self.backgroundTexture = self.texture.createTex(self.data.get('texture/menu/menu.jpg')) self.stringTexture = self.texture.createTex(self.stringPlane['mainMenu']) self.planes['0_background'] = {'texture': self.backgroundTexture, 'coord': [16.7, 12.525, 16.6]} self.planes['1_strings'] = {'texture': self.stringTexture, 'coord': [16.7, 12.525, 16.59]} self.cubePos = {'mainMenu': { 'start': [4,4.6,0], 'highscore': [10,0.2,0], 'exit': [3,-4,0] }, 'gameoverMenu': { 'start': [10.5,2.4,0], 'highscore': [10,-2,0], 'exit': [3,-6.2,0] }} self.cube = TetrisCube(1, self.cubePos[self.state][self.menuitem]) def generateStringTexture(self, name): if self.stringPlane.has_key(name): del self.stringPlane[name] if name == 'mainMenu': self.registerString('mainMenu', self.tstring.create('Cubix'), 320, 20, 'center', 1) self.registerString('mainMenu', self.tstring.create('Start'), 320, 145, 'center', 0.8) self.registerString('mainMenu', self.tstring.create('Highscores'), 320, 215, 'center', 0.8) self.registerString('mainMenu', self.tstring.create('Exit'), 320, 290, 'center', 0.8) elif name == 'gameoverMenu': self.registerString('gameoverMenu', self.tstring.create('Game Over'), 320, 20, 'center', 1) self.registerString('gameoverMenu', self.tstring.create('Your Score: ' + str(self.Tetris3d.objects['mainGrid'].score.value)), 320, 120, 'center', 0.35) self.registerString('gameoverMenu', self.tstring.create('Try Again!'), 320, 180, 'center', 0.8) self.registerString('gameoverMenu', self.tstring.create('Highscores'), 320, 250, 'center', 0.8) self.registerString('gameoverMenu', self.tstring.create('Exit'), 320, 320, 'center', 0.8) elif name == 'enterName': self.registerString('enterName', self.tstring.create('Game Over'), 320, 20, 'center', 1) self.registerString('enterName', self.tstring.create('Enter Your Name:'), 320, 120, 'center', 0.4) self.registerString('enterName', self.tstring.create(self.playername), 320, 215, 'center', 0.5) elif name == 'pause': self.registerString('pause', self.tstring.create('Pause'), 320, 180, 'center', 1) elif name == 'highscore' or name == 'highscoreHighlight': if name == 'highscoreHighlight' and self.stringPlane.has_key('highscore'): del self.stringPlane['highscore'] self.registerString('highscore', self.tstring.create('Highscores'), 320, 20, 'center', 1) self.registerString('highscore', self.tstring.create('Press escape to go back to menu'), 320, 110, 'center', 0.32) id = self.Tetris3d.objects['mainGrid'].db.getNewestId() extraSpace = 0 i = 1 for score in self.Tetris3d.objects['mainGrid'].db.getToTen(): if score[2] == id and name == 'highscoreHighlight': size = 0.5 else: size = 0.35 nameString = self.tstring.create(score[0]) scoreString = self.tstring.create(str(score[1])) # To prevent that the name and score overlap each other in the list if int((nameString.size[0] + scoreString.size[0]) * size) > 540: ratio = 540.0 / ((nameString.size[0] + scoreString.size[0]) * size) size *= ratio self.registerString('highscore', nameString, 50, 120 + 30 * i + extraSpace, 'left', size) self.registerString('highscore', scoreString, 590, 120 + 30 * i + extraSpace, 'right', size) if score[2] == id and name == 'highscoreHighlight': extraSpace = 8 i += 1 elif name == 'play': self.registerString('play', self.tstring.create('Cubix'), 320, 30, 'left', 1) self.registerString('play', self.tstring.create('Score:'), 320, 365, 'left', 0.33) self.registerString('play', self.tstring.create('Level:'), 320, 423, 'left', 0.33) def registerString(self, skin, image, x, y, align, size): x = int(x * self.textureWidthRatio) y = int(y * self.textureHeightRatio) if not self.stringPlane.has_key(skin): self.stringPlane[skin] = new('RGBA', (512, 512)) width = int(image.size[0] * size * self.textureWidthRatio) height = int(image.size[1] * size * self.textureHeightRatio) image = image.resize((width, height), ANTIALIAS) posY = y if align == 'left': posX = x elif align == 'right': posX = x - width elif align == 'center': posX = x - (width / 2) else: posX = 0 self.stringPlane[skin].paste(image, (posX, posY)) def setState(self, state): if state == 'mainMenu': self.state = 'mainMenu' self.texture.createTex(self.stringPlane['mainMenu'], self.stringTexture) self.menuitem = 'start' self.cube.renderPos = self.cubePos[self.state][self.menuitem] elif state == 'gameoverMenu': self.state = 'gameoverMenu' self.generateStringTexture('gameoverMenu') self.texture.createTex(self.data.get('texture/menu/menu.jpg'), self.backgroundTexture) self.texture.createTex(self.stringPlane['gameoverMenu'], self.stringTexture) self.menuitem = 'start' self.cube.renderPos = self.cubePos[self.state][self.menuitem] elif state == 'play': self.state = 'play' self.Tetris3d.objects['mainGrid'].run = True self.texture.createTex(self.data.get('texture/menu/front.jpg'), self.backgroundTexture) self.texture.createTex(self.stringPlane['play'], self.stringTexture) if self.Tetris3d.objects['mainGrid'].gameover: self.Tetris3d.objects['mainGrid'].gameover = 0 self.Tetris3d.objects['mainGrid'].clearGrid() elif state == 'pause': self.state = 'pause' self.Tetris3d.objects['mainGrid'].run = None self.texture.createTex(self.data.get('texture/menu/menu.jpg'), self.backgroundTexture) self.texture.createTex(self.stringPlane['pause'], self.stringTexture) elif state == 'highscore': if self.state == 'enterName': self.generateStringTexture('highscoreHighlight') else: self.generateStringTexture('highscore') self.state = 'highscore' self.texture.createTex(self.data.get('texture/menu/highscore.jpg'), self.backgroundTexture) self.texture.createTex(self.stringPlane['highscore'], self.stringTexture) elif state == 'enterName': self.state = 'enterName' self.generateStringTexture('enterName') self.texture.createTex(self.data.get('texture/menu/menu.jpg'), self.backgroundTexture) self.texture.createTex(self.stringPlane['enterName'], self.stringTexture) def keyboardSpecial(self, key, x, y): if self.state == 'mainMenu' or self.state == 'gameoverMenu': if key == 101: # up arrow key if self.menuitem == 'start': self.menuitem = 'exit' elif self.menuitem == 'exit': self.menuitem = 'highscore' elif self.menuitem == 'highscore': self.menuitem = 'start' elif key == 103: # down arrow key if self.menuitem == 'start': self.menuitem = 'highscore' elif self.menuitem == 'highscore': self.menuitem = 'exit' elif self.menuitem == 'exit': self.menuitem = 'start' self.cube.renderPos = self.cubePos[self.state][self.menuitem] def keyboard(self, key, x, y): if self.state == 'mainMenu' or self.state == 'gameoverMenu': if ord(key) == 27: # key = esc sys.exit() if ord(key) == 13: # key = enter if self.menuitem == 'start': self.setState('play') elif self.menuitem == 'highscore': self.setState('highscore') elif self.menuitem == 'exit': sys.exit() elif self.state == 'highscore': if ord(key) == 27: # key = esc self.setState('mainMenu') elif self.state == 'enterName': if self.tstring.data.has_key(key) and len(self.playername) < 13: self.playername += key elif ord(key) == 8: # key = backspace self.playername = self.playername[:-1] self.generateStringTexture('enterName') self.texture.createTex(self.stringPlane['enterName'], self.stringTexture) if ord(key) == 13 and len(self.playername) != 0: # key = enter self.Tetris3d.objects['mainGrid'].db.addScore(unicode(self.playername), self.Tetris3d.objects['mainGrid'].score.getValue()) self.setState('highscore') elif ord(key) == 27: # key = esc self.setState('mainMenu') elif self.state == 'pause': if key == 'p': self.setState('play') if ord(key) == 27: # key = esc self.Tetris3d.objects['mainGrid'].gameOver() elif self.state == 'play': if key == 'p': self.setState('pause') if ord(key) == 27: # key = esc self.Tetris3d.objects['mainGrid'].gameOver() def display(self): for name, plane in self.planes.iteritems(): x = plane['coord'][0] y = plane['coord'][1] z = plane['coord'][2] glEnable (GL_BLEND) glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA) glEnable(GL_TEXTURE_2D) glBindTexture(GL_TEXTURE_2D, plane['texture']) glBegin(GL_QUADS) glTexCoord2f(1.0, 0.0); glVertex3f(-x, -y, z) # Bottom Right glTexCoord2f(0.0, 0.0); glVertex3f( x, -y, z) # Bottom Left glTexCoord2f(0.0, 1.0); glVertex3f( x, y, z) # Top Left glTexCoord2f(1.0, 1.0); glVertex3f(-x, y, z) # Top Right glEnd() glDisable(GL_TEXTURE_2D) glDisable(GL_BLEND) if self.state == 'mainMenu' or self.state == 'gameoverMenu': glPushMatrix() glScaled(0.2,0.2,0.2) glTranslated(0,0,-0.2) self.cube.display() glPopMatrix() def update(self, delay): if self.time + 1000 < glutGet(GLUT_ELAPSED_TIME) and (self.state == 'mainMenu' or self.state == 'gameoverMenu'): new = randrange(0,6) if new >= self.cube.type: self.cube.setType(new + 1) else: self.cube.setType(new) if self.cube.type == 0: for pos in self.cube.position: pos[0] += 0.5 elif self.cube.type == 2: for pos in self.cube.position: pos[1] += 0.5 elif self.cube.type == 3 or self.cube.type == 4: for pos in self.cube.position: pos[0] -= 0.5 elif self.cube.type == 5 or self.cube.type == 6: for pos in self.cube.position: pos[1] -= 0.5 self.time = glutGet(GLUT_ELAPSED_TIME)
class Player: screen_pos_x = config.screen_width screen_pos_y = config.screen_height player_png = Textures.loadTxt("player", "player") can_jump = True jump_count = 44 already_digged = False can_dig_left = False can_dig_right = False clicked_mouse = False is_falling = False left_wall = False right_wall = False POS_ON_SCREEN_X = 32 * config.screen_width // 64 - 16 POS_ON_SCREEN_Y = 32 * config.screen_height // 64 def __init__(self, terrain): self.player_name = "SKR" self.position = [terrain.world_size_x * 32 // 2 + 16, 64 * 32 + 32] self.block_x = self.position[0] // 32 self.block_y = self.position[1] // 32 - 1 def drawNickname(self, window): Inventory.Inventory.gui_handler.drawText(window, config.screen_width//2 - (len(self.player_name) * 4), config.screen_height//2 - 16, self.player_name) def update_current_block(self): if self.position[0] % 32 != 0: self.block_x = self.position[0] // 32 if self.position[1] % 32 != 0: self.block_y = self.position[1] // 32 def clicked_block(self): mouse_pos = pygame.mouse.get_pos() dif_x = mouse_pos[0] - (self.POS_ON_SCREEN_X + 16) dif_y = mouse_pos[1] - (self.POS_ON_SCREEN_Y + 32) if not self.right_wall: clicked_block_x = self.block_x + (dif_x + self.position[0] % 32) // 32 else: clicked_block_x = self.block_x + dif_x // 32 + 1 if self.position[1] % 32 != 0: clicked_block_y = self.block_y + (dif_y + self.position[1] % 32) // 32 else: clicked_block_y = self.block_y + dif_y // 32 + 1 return clicked_block_x, clicked_block_y def jump(self): if self.jump_count > 0: self.jump_count -= 2 self.position[1] -= 2 else: self.is_falling = True self.can_jump = True self.jump_count = 44 def start_fall(self, terrain): if self.position[1] % 32 == 0 and terrain.terrain[self.block_y + 1][self.block_x].transparent: self.is_falling = True def fall(self, terrain): if self.is_falling: self.position[1] += 2 if self.position[1] % 32 == 0: self.is_falling = False self.start_fall(terrain) def dig(self, terrain, events, eq): for event in events: if event.type == pygame.MOUSEBUTTONDOWN and event.button == 1: clicked_block = self.clicked_block() if clicked_block is not None: rel_blocks_x = clicked_block[0] - self.block_x rel_blocks_y = clicked_block[1] - self.block_y if terrain.terrain[clicked_block[1]][clicked_block[0]].breakable: if (rel_blocks_x == 0 and (rel_blocks_y == 1 or rel_blocks_y == -1)) \ or (rel_blocks_y == 0 and (rel_blocks_x == 1 or rel_blocks_x == -1)) \ or (rel_blocks_y == -1 and rel_blocks_x == -1 and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \ or (rel_blocks_y == -1 and rel_blocks_x == 1 and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)) \ or (rel_blocks_y == 1 and rel_blocks_x == -1 and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \ or (rel_blocks_y == 1 and rel_blocks_x == 1 and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)): if terrain.terrain[clicked_block[1]][clicked_block[0]].collectable: # eq.set_slot(terrain.terrain[clicked_block[1]][clicked_block[0]].type) eq.bar.container.addItem(terrain.terrain[clicked_block[1]][clicked_block[0]].type, 1, (0, 0)) terrain.terrain[clicked_block[1]][clicked_block[0]] = Sky() def place_block(self, terrain, events, eq): for event in events: if event.type == pygame.MOUSEBUTTONDOWN and event.button == 3 and eq.bar.container.content[eq.picked_slot].item != "empty": clicked_block = self.clicked_block() if clicked_block is not None: rel_blocks_x = clicked_block[0] - self.block_x rel_blocks_y = clicked_block[1] - self.block_y if terrain.terrain[clicked_block[1]][clicked_block[0]].transparent: if (rel_blocks_x == 0 and (rel_blocks_y == 1 or rel_blocks_y == -1)) \ or (rel_blocks_y == 0 and (rel_blocks_x == 1 or rel_blocks_x == -1)) \ or (rel_blocks_y == -1 and rel_blocks_x == -1 and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \ or (rel_blocks_y == -1 and rel_blocks_x == 1 and (terrain.terrain[clicked_block[1] + 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)) \ or (rel_blocks_y == 1 and rel_blocks_x == -1 and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] + 1].transparent)) \ or (rel_blocks_y == 1 and rel_blocks_x == 1 and (terrain.terrain[clicked_block[1] - 1][clicked_block[0]].transparent or terrain.terrain[clicked_block[1]][clicked_block[0] - 1].transparent)): terrain.terrain[clicked_block[1]][clicked_block[0]] = \ blocks[eq.bar.container.content[eq.picked_slot].item]() eq.bar.container.takeItem(1, (0, eq.picked_slot)) def move(self, events, terrain, eq): #handling toggleable keys for event in events: if event.type == pygame.KEYDOWN: if event.key == pygame.K_e: eq.eq_opened = not eq.eq_opened if event.key == pygame.K_c: eq.crafting_opened = not eq.crafting_opened #handling keys held continuously keys = pygame.key.get_pressed() if keys[pygame.K_a]: if (self.position[0] % 32 != 0 or terrain.terrain[self.block_y][self.block_x - 1].transparent or self.right_wall and terrain.terrain[self.block_y][self.block_x].transparent and not (self.left_wall and keys[pygame.K_d])): self.position[0] -= 1 elif not self.right_wall: self.left_wall = True if keys[pygame.K_d]: if (self.position[0] % 32 != 0 or terrain.terrain[self.block_y][self.block_x + 1].transparent or (self.left_wall and not keys[pygame.K_a])) \ and terrain.terrain[self.block_y][self.block_x].transparent: self.position[0] += 1 elif not self.left_wall: self.right_wall = True if (keys[pygame.K_w] or keys[pygame.K_SPACE]) and self.can_jump and not self.is_falling: if terrain.terrain[self.block_y - 1][self.block_x].transparent: self.can_jump = False #some other stuff idk not my code lmao if not self.can_jump: self.jump() if self.position[0] % 32 != 0: self.left_wall = False self.right_wall = False self.start_fall(terrain) self.fall(terrain) def update(self, events, terrain, eq): self.move(events, terrain, eq) self.update_current_block() self.dig(terrain, events, eq) self.place_block(terrain, events, eq) def draw(self, window): window.blit(self.player_png, (self.POS_ON_SCREEN_X, self.POS_ON_SCREEN_Y)) self.drawNickname(window)
def __init__(self, x, y): super().__init__(Textures.get_texture(4, 9), x, y)
def update(self): speed_mult = 1 if self.keyboard.is_pressed("sprint"): speed_mult = 2 if self.keyboard.is_pressed("dash"): if not self.dashing: self.change_y += 2 self.dashing = True if self.keyboard.is_pressed("l"): pass # self.level.reset = True if self.keyboard.is_pressed("down"): self.change_y -= 0.1 self.crawling = True speed_mult *= 0.5 else: self.crawling = False if self.keyboard.is_pressed("attack"): if self.curr_attack_speed == 0: extra_y_dir = 0 if self.keyboard.is_pressed("up"): extra_y_dir = 4 elif self.keyboard.is_pressed("down"): extra_y_dir = -4 attack_x = (self.change_x) * 4 attack_y = (self.change_y + extra_y_dir) * 3 attack_angle = int( math.atan2(attack_y, attack_x) / math.pi * 180) card = Projectile( Textures.SPRITESHEET[3 + int((attack_angle % 360) / 45) + 16], self.center_x, self.center_y, attack_x, attack_y) self.level.add_entity_to_list(card, self.level.entities) self.curr_attack_speed = self.max_attack_speed Sounds.play(Sounds.SHOOT) if self.curr_attack_speed > 0: self.curr_attack_speed -= 1 if self.keyboard.is_pressed("jump"): if self.level.physics_engine.can_jump(1): # if self.level.engine.can_jump(self, 1): if not self.jumping: Sounds.play(Sounds.JUMP) self.level.physics_engine.jump(self.jump_height) self.jumping = True # elif self.level.engine.can_jump(self, -1): elif self.level.physics_engine.can_jump(-1): self.jumping = False self.curr_jump_height = 0 if self.curr_jump_height > self.max_jump_height: self.jumping = False self.curr_jump_height = 0 elif self.curr_jump_height >= self.min_jump_height: self.jumping = False self.curr_jump_height = 0 if self.jumping: self.change_y = self.jump_height self.curr_jump_height += self.jump_height if self.keyboard.is_pressed("left"): self.change_x = -self.movespeed * speed_mult elif self.keyboard.is_pressed("right"): self.change_x = self.movespeed * speed_mult else: if self.change_x > 1: self.change_x -= 1 self.not_mirrored = True elif self.change_x < -1: self.change_x += 1 self.not_mirrored = False else: self.change_x = 0 if self.dashing: if self.change_x > 0: self.change_x = self.movespeed * speed_mult * 1.5 elif self.change_x < 0: self.change_x = -self.movespeed * speed_mult * 1.5 self.curr_dash_frame += 1 if self.curr_dash_frame >= self.dash_frame_speed * len( self.dash_textures): self.curr_dash_frame = 0 self.dashing = False elif self.crawling: self.curr_crawl_frame += 1 if self.curr_crawl_frame >= self.crawl_frame_speed * len( self.crawl_textures): self.curr_crawl_frame = 0 else: self.walk_count += 1 if self.walk_count >= len( self.walking_textures) * self.walk_frame_speed: self.walk_count = 0 if self.curr_invis_frame > 0 and self.curr_invis_frame % 12 < 6: self.texture = Textures.get_texture(15, 15) elif self.change_x > 0: if self.dashing: self.texture = self.dash_textures[self.curr_dash_frame // self.dash_frame_speed] elif self.crawling: self.texture = self.crawl_textures[self.curr_crawl_frame // self.crawl_frame_speed] else: self.texture = self.walking_textures[self.walk_count // self.walk_frame_speed] # self.player_dir = True elif self.change_x < 0: if self.dashing: self.texture = self.dash_textures_mirrored[ self.curr_dash_frame // self.dash_frame_speed] elif self.crawling: self.texture = self.crawl_textures_mirrored[ self.curr_crawl_frame // self.crawl_frame_speed] else: self.texture = self.walking_textures_mirrored[ self.walk_count // self.walk_frame_speed] # self.player_dir = False else: if self.not_mirrored: if self.crawling: self.texture = self.crawl_textures[0] else: self.texture = self.idle_texture else: if self.crawling: self.texture = self.crawl_textures_mirrored[0] else: self.texture = self.idle_texture_mirrored super().update()
def drawRoom(self, room): room_x = int(room.x * ROOM_WIDTH) room_y = int(room.y * ROOM_HEIGHT) for x in range(0, 3): for y in range(0, 3): template = ROOM_TEMPLATES[2] neighbor_rooms = {} for direction in DIRS: neighbor = self.rooms.get((room.x + direction[0], room.y + direction[1])) if neighbor is not None: neighbor_rooms[direction] = neighbor left = neighbor_rooms.get((-1, 0)) is not None bottom = neighbor_rooms.get((0, -1)) is not None top = neighbor_rooms.get((0, 1)) is not None right = neighbor_rooms.get((1, 0)) is not None if y == 0: if x == 1: if bottom: template = getRandTemplate(BOTTOM_OPEN) else: template = getRandTemplate(BOTTOM) elif x == 0: template = getRandTemplate(BOTTOM_LEFT) elif x == 2: template = getRandTemplate(BOTTOM_RIGHT) elif y == 1: if x == 0: if left: template = EMPTY else: template = MIDDLE_LEFT[0] elif x == 1: template = MIDDLE_CENTER[0] elif x == 2: if right: template = EMPTY else: template = MIDDLE_RIGHT[0] elif y == 2: if x == 1: if top: template = getRandTemplate(TOP_OPEN) else: template = TOP[0] elif x == 0: template = TOP_LEFT[0] elif x == 2: template = TOP_RIGHT[0] drawTemplate( self.level, room_x + x * TEMPLATE_WIDTH, room_y + y * TEMPLATE_HEIGHT, template) difficulty = self.level.difficulty while difficulty > 0: if room.type == START_ROOM: break if room.type == BOSS_ROOM: entity_x = (room_x + ROOM_WIDTH / 2) * TILE_SIZE entity_y = (room_y + ROOM_HEIGHT / 2) * TILE_SIZE boss = Boss(entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(boss, self.level.entities) difficulty -= 5 else: enemy_type = random.randint(0, 2) entity_x = random.randrange(room_x + 1, room_x + ROOM_WIDTH - 1) entity_y = random.randrange(room_y + 1, room_y + ROOM_HEIGHT - 1) tries = 0 while self.level.get_tile(entity_x, entity_y) or tries < 5: entity_x = random.randrange(room_x + 1, room_x + ROOM_WIDTH - 1) entity_y = random.randrange(room_y + 1, room_y + ROOM_HEIGHT - 1) tries += 1 entity_x *= TILE_SIZE entity_y *= TILE_SIZE if enemy_type == 0: slem = Slime(Textures.get_texture(3, 2), entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(slem, self.level.entities) difficulty -= 1 elif enemy_type == 1: enemy = Enemy(Textures.get_texture(0, 2), entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(enemy, self.level.entities) difficulty -= 1 else: spiky_ball = SpikyBall(entity_x, entity_y, self.level.difficulty) self.level.add_entity_to_list(spiky_ball, self.level.entities) difficulty -= 1
def __init__(self, x, y, difficulty): super().__init__(Textures.get_texture(1, 1), x, y, difficulty) self.movespeed = 1 + difficulty * 0.2 self.damage = 2