def initArea(self, mapFile): """Load maps and initialize sprite layers for each new area""" self.tilemap = tmx.load(mapFile, screen.get_size()) self.players = tmx.SpriteLayer() self.objects = tmx.SpriteLayer() #self.life = pygame.image.load('life.png') #self.gamefont = pygame.font.Font(None,30) #self.timertext = gamefont.render('Timer:' +str(timer),1,[255,0,0]) #self.boxsize = self.timertext.get_rect() #self.timerXpos = (640-self.boxsize[2])/2 #screen.blit(self.timertext,[0,0]) try: for cell in self.tilemap.layers['sprites'].find('src'): SpriteLoop((cell.px,cell.py), cell, self.objects) # In case there is no sprite layer for the current map except KeyError: pass else: self.tilemap.layers.append(self.objects) #self.life.layers.append(self.objects) #추가한거 # Initializing player sprite startCell1 = self.tilemap.layers['triggers'].find('playerStart')[random.randint(0,30)] self.player1 = character.Player((startCell1.px, startCell1.py), startCell1['playerStart'], self.players) startCell2 = self.tilemap.layers['triggers'].find('playerStart')[random.randint(0, 30)] self.player2 = character.Player2((startCell2.px, startCell2.py), startCell2['playerStart'], self.players) for i in range (0,9): startCell3 = self.tilemap.layers['triggers'].find('playerStart')[random.randint(0, 30)] self.player3 = character.NPC((startCell3.px, startCell3.py), startCell2['playerStart'],self.players) self.tilemap.layers.append(self.players) self.tilemap.set_focus(self.player1.rect.x, self.player1.rect.y) # 1p 시점
def __init__(self, screen, level=0, world=0): self.screen = screen self.clock = pygame.time.Clock() self.dt = self.clock.tick(30) self.sprites = pygame.sprite.Group() self.player = character.Player(100, 100, self.dt, self.sprites) self.distance = 100 self.levels = {} self.apoint = {} self.bpoint = {} self.level = level self.world = world self.flipped = False self.sound_list = [] self.font = pygame.font.Font(None, 80)
def __init__(self): # Tier measures how much the player has advanced. # The higher the tier, the higher level employees, subjects, and events occur self.tier = 0 # Creates a world, which is a list of regions, including the facility self.world = world.World() # The amount of money available to the player self.funds = 1000000 # The current date, only the weeks matter self.week = 0 # The amount of global heat, serves as a modifier on mission difficulty # Each region also has local heat self.globalHeat = 0 # The player object self.player = character.Player()
def on_init(self): # initialize pygame pygame.init() # load and set the logo & caption self.logopath = Path("assets/logo32x32.png") self.logo = pygame.image.load(self.logopath.resolve().as_posix()) pygame.display.set_icon(self.logo) pygame.display.set_caption("ISP Game - Doodle Dragon") # create a surface on the screen self.screen = pygame.display.set_mode( self.size, pygame.HWSURFACE | pygame.DOUBLEBUF) # initialize level 1 self.currentLevel = level.Level(1) self.currentLevel.display(self.screen) # initialize characters self.characters = pygame.sprite.Group() # create the player character self.playerChar = character.Player(self.floor) self.characters.add(self.playerChar)
# The game is not over, and the player has not yet done anything to # use up their turn # {{ game_over = False turn_finished = False # }} # Establish a screen # {{ myscreen = curses.initscr() # }} # Establish a test room, and place items into that test room # {{ test_player = character.Player() # Make a dictionary of rooms # {{ main_room_dict = {} for i in range(0, 5): for j in range(0, 5): main_room_dict[(i, j)] = room.Room([], (i, j)) # }} main_room_dict[(0, 0)].characters.append(test_player) print_room(main_room_dict[(0, 0)], test_player) # Place test items at (24, 10) # {{
ani = 4 #set the clock clock = pygame.time.Clock() #initialize the pygame module pygame.init() #define the world size world = pygame.display.set_mode([worldx, worldy]) #load the background image backdrop = pygame.image.load(os.path.join('./images/background0.png')) backdropbox = world.get_rect() #used to close game main = True #character setup player = character.Player() # spawn player player.rect.x = 100 # go to x player.rect.y = 100 # go to y player_list = pygame.sprite.Group() player_list.add(player) #level setup ground_list = level1.level1.ground(1, 0, worldy - 97, 1080, 97) #plat_list = Level.platform(1) steps = 10 #how many pixels per movement #combine all spites into a single group game_objects = pygame.sprite.Group() game_objects.add(player_list) game_objects.add(ground_list) ''' Main Loop
msg += 'Welcome to our game! Move around with \'wasd\' and \'q\' to quit.\nYou will need a key to open the door. Try asking the Boss.' msg += '\n' + '*'*60 + '\n' + '*'*60 + '\n' print(msg) def end_card(): msg = '' msg += '*'*60 + '\n' + '*'*60 + '\n' msg += '\t\tThank you for playing!\n\t\t\tCaleb\n\t\t\tJack\n\t\t\tScott\n' msg += '*'*60 + '\n' + '*'*60 + '\n' print(msg) if __name__ == '__main__': start_card() hero_player = character.Player() main_room = room.Room([hero_player], 10, 10) main_room.spawn(hero_player, hero_player.location) #this is a test boss = character.Boss() main_room.spawn(boss, boss.location) main_room.add_door() new_loc = hero_player.location keep_playing = True while keep_playing: print(f'Health: {hero_player.health}') if boss.check(hero_player): print(f'Hello {hero_player.name}, you must defeat me in a supreme contest of skill...ROCK PAPER SCISSORS!') if RPS():
tile.draw_tile(screen) player.playerdraw(screen) for i in slimelist: i.draw(screen) pygame.display.flip() #Screen 1 overworld1_spritesheet = Spritesheet('src/assets/temp/texturepack.png') sheet_items = [('grass0000.png', 1), ('sand0000.png', 0)] level = Map("src/assets/temp/level1.csv") level = level.create_map() level = make_tile_map(level, 32, overworld1_spritesheet, sheet_items) player = character.Player(224, 0) slimelist = [enemies.BlueSlime(540, 540), enemies.BlueSlime(400, 780)] #sound.beta_soundtrack() while True: clock.tick(30) pygame.display.set_caption( f"{player.rect.x}, {player.rect.y} - {player.health}") player.update(level) player.check_collision_enemy(slimelist) for action in pygame.event.get(): if action.type == pygame.QUIT: #sound.stop_music() exit() drawscreen()
def create_player(self): self.player = character.Player( ("male", 0), self, self.get_tile(0, 0)) self.get_tile(0, 0).rigid_body = self.player self.player.initialize(self.player.tile.scene_pos_x, self.player.tile.scene_pos_y) self.addItem(self.player)
def main(screenRes): # Initialise screen pygame.init() screen = pygame.display.set_mode(screenRes, pygame.FULLSCREEN|pygame.DOUBLEBUF) pygame.display.set_caption('Cosplay Chase') screenRect = screen.get_rect() # Fill background background = pygame.Surface(screen.get_size()) background = background.convert() background.fill((50,50,50)) splash_screen = pygame.image.load(os.path.join("data", "title.png")) splash_rect = splash_screen.get_rect() splash_rect.center = screenRect.center gameover_screen = pygame.image.load(os.path.join("data", "gameover.png")) gameover_rect = gameover_screen.get_rect() gameover_rect.center = screenRect.center font = pygame.font.SysFont("Arial", 50) splash = True while splash: screen.blit(background, (0,0)) screen.blit(splash_screen, splash_rect) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN: if event.key == K_ESCAPE: return else: splash = False # Load level from tiled level file level = world.Level(os.path.join("data","world","test.tmx")) spawnLoc = level.data.get_object_by_name("Player") while True: level.dx = 0 level.dy = 0 sprites = pygame.sprite.Group() player = character.Player(spawnLoc.x, spawnLoc.y, 800, level) sprites.add(player) guards = character.GuardManager(player, level, screenRect) # Initialise clock clock = pygame.time.Clock() time = 0.0 frames = 0 start_time = pygame.time.get_ticks() while not guards.iscaptured(player): dt = clock.tick(200) / 1000.0 time += dt frames += 1 if time >= 1.0: fps = frames / time if fps < 70: print("WARNING: Low framerate: "+str(fps)+" FPS") time = 0.0 frames = 0 for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN: if event.key == K_ESCAPE: return movedir = [0, 0] pressed = pygame.key.get_pressed() if pressed[K_LEFT]: movedir[0] -= 1 if pressed[K_RIGHT]: movedir[0] += 1 if pressed[K_UP]: movedir[1] -= 1 if pressed[K_DOWN]: movedir[1] += 1 if abs(movedir[0]) + abs(movedir[1]) == 2: movedir[0] *= ONEONSQRT2 movedir[1] *= ONEONSQRT2 player.move(movedir, dt) dx = player.rect.center[0] - screenRect.width / 2 dy = player.rect.center[1] - screenRect.height / 2 level.update(dt, dx, dy) sprites.update(dt, dx, dy) guards.update(dt, dx, dy) # Blit everything to the screen screen.blit(background, (0,0)) level.draw(screenRect, screen) guards.draw(screen) sprites.draw(screen) pygame.display.flip() lifetime = (pygame.time.get_ticks() - start_time) / 1000.0 print("You've been caught and your weapon has been confiscated!") print("You lasted {:.1f} seconds.".format(lifetime)) lifetime_img = font.render("{:.1f} seconds".format(lifetime), True, (0,0,0)) lifetime_rect = lifetime_img.get_rect() lifetime_rect.center = (gameover_rect.center[0], gameover_rect.center[1] + 25) splash = True while splash: screen.blit(background, (0,0)) screen.blit(gameover_screen, gameover_rect) screen.blit(lifetime_img, lifetime_rect) pygame.display.flip() for event in pygame.event.get(): if event.type == QUIT: return elif event.type == KEYDOWN: if event.key == K_ESCAPE: return else: splash = False
def create_player(self, _id, data): p = character.Player(_id, data, location=self.world.locations[0]) self.players[_id] = p
def main(): #create map mapList = MapFile.makeMap() faireMap = Map.TheMap(mapList) #intro theGame, loaded = renquestIntro.intro() playerStats = renquestIntro.chooseChar() os.system('cls') #create player character if loaded != True: inventory = { "Name": "", "Attack": 0, "Dexterity": 0, "Charisma": 0, "Hit Points": 0, "Weapon": "", "Player Class": "", "Money": 0, "Consumables": [], "Spaces": ["", "", "", "", "", "", "", ""] } consumable = inventory.get("Consumables") spaces = inventory.get("Spaces") dagger = Items.Weapon("Dagger", 0, 4, 0) armor = Items.Armor("", 0, 0) necklace = Items.Armor("", 0, 0) inventory["Weapon"] = dagger inventory["Armor"] = armor inventory["Accessory"] = necklace inventory["Name"] = playerStats.get("name") inventory["Player Class"] = playerStats.get("playerClass") inventory["Attack"] = playerStats.get("ATK") inventory["Dexterity"] = playerStats.get("DEX") inventory["Charisma"] = playerStats.get("CHA") inventory["Hit Points"] = playerStats.get("HP") inventory["Money"] = playerStats.get("money") player = character.Player(inventory.get("Name"), inventory.get("Attack"), inventory.get("Dexterity"), inventory.get("Charisma"), inventory.get("Hit Points"), inventory.get("Weapon"), inventory.get("Money"), inventory, consumable, spaces, inventory.get("Player Class")) playerClass = inventory.get("Player Class") maxHP = player.getHP() #create the knight knightWeapon = Items.Weapon("Two-Handed Sword", 3, 6, 0) knight = character.Character("The Knight", 7, 7, 8, 50, knightWeapon) #create the game theGame = TheGame(inventory.get("Name"), knight, player) #set fill character oldPos = "·" #create display print("Use arrow keys to move around the map") gameMapUI = faireMap.mapUI() gameStatsUI = player.statsUI(player.inventory) theGame.display(gameMapUI, gameStatsUI) #set current player variables curPosX = 2 curPosY = 10 newPos = "·" #loop to allow movement to continue # curPosX = 6 # curPosY = 18 newPos = "§" if newPos == "§": theGame.theInfoDragon(theGame) while True: uIn = ord(msvcrt.getch()) playerChar = "☻" if uIn == 224: #special character uIn = ord(msvcrt.getch()) os.system('cls') message, dead, level = player.healthValidation(inventory) if level != 1: os.system('cls') print(message) if dead == True: print("\nYou lose the game") input() sys.exit() if uIn == 72: #UP newPosX = (curPosX - 1) newPos = faireMap.getFillChar(newPosX, curPosY) print("Your current position: " + newPos) if newPos == "▲": theGame.display(gameMapUI, gameStatsUI) print( "The trees are too dense to go through. Choose another direction." ) else: faireMap.setFillChar(curPosX, curPosY, oldPos) faireMap.setFillChar(newPosX, curPosY, playerChar) oldPos = newPos curPosX = newPosX gameMapUI = faireMap.mapUI() gameStatsUI = player.statsUI(player.inventory) theGame.display(gameMapUI, gameStatsUI) if newPos == "█": theGame.vendors(curPosX, curPosY, playerClass, player.inventory, faireMap, player) elif newPos == "♥": theGame.waterTent(player, faireMap, inventory, maxHP) elif newPos == "╔" or newPos == "╗" or newPos == "╚" or newPos == "╝": theGame.battle(player, inventory, knight, knightWeapon) elif newPos == "§": theGame.theInfoDragon(theGame) if uIn == 80: #DOWN newPosX = (curPosX + 1) newPos = faireMap.getFillChar(newPosX, curPosY) print("Your current position: " + newPos) if newPos == "▲": theGame.display(gameMapUI, gameStatsUI) print( "The trees are too dense to go through. Choose another direction." ) else: faireMap.setFillChar(curPosX, curPosY, oldPos) faireMap.setFillChar(newPosX, curPosY, playerChar) oldPos = newPos curPosX = newPosX gameMapUI = faireMap.mapUI() gameStatsUI = player.statsUI(player.inventory) theGame.display(gameMapUI, gameStatsUI) if newPos == "█": theGame.vendors(curPosX, curPosY, playerClass, player.inventory, faireMap, player) elif newPos == "♥": theGame.waterTent(player, faireMap, inventory, maxHP) elif newPos == "╔" or newPos == "╗" or newPos == "╚" or newPos == "╝": theGame.battle(player, inventory, knight, knightWeapon) elif newPos == "§": theGame.theInfoDragon(theGame) if uIn == 75: #LEFT newPosY = (curPosY - 1) newPos = faireMap.getFillChar(curPosX, newPosY) print("Your current position: " + newPos) if newPos == "▲": theGame.display(gameMapUI, gameStatsUI) print( "The trees are too dense to go through. Choose another direction." ) else: faireMap.setFillChar(curPosX, curPosY, oldPos) faireMap.setFillChar(curPosX, newPosY, playerChar) oldPos = newPos curPosY = newPosY gameMapUI = faireMap.mapUI() gameStatsUI = player.statsUI(player.inventory) theGame.display(gameMapUI, gameStatsUI) if newPos == "█": theGame.vendors(curPosX, curPosY, playerClass, player.inventory, faireMap, player) elif newPos == "♥": theGame.waterTent(player, faireMap, inventory, maxHP) elif newPos == "╔" or newPos == "╗" or newPos == "╚" or newPos == "╝": theGame.battle(player, inventory, knight, knightWeapon) elif newPos == "§": theGame.theInfoDragon(theGame) if uIn == 77: #RIGHT newPosY = (curPosY + 1) newPos = faireMap.getFillChar(curPosX, newPosY) print("Your current position: " + newPos) if newPos == "▲": theGame.display(gameMapUI, gameStatsUI) print( "The trees are too dense to go through. Choose another direction." ) else: faireMap.setFillChar(curPosX, curPosY, oldPos) faireMap.setFillChar(curPosX, newPosY, playerChar) oldPos = newPos curPosY = newPosY gameMapUI = faireMap.mapUI() gameStatsUI = player.statsUI(player.inventory) theGame.display(gameMapUI, gameStatsUI) if newPos == "█": theGame.vendors(curPosX, curPosY, playerClass, player.inventory, faireMap, player) elif newPos == "♥": theGame.waterTent(player, faireMap, inventory, maxHP) elif newPos == "╔" or newPos == "╗" or newPos == "╚" or newPos == "╝": theGame.battle(player, inventory, knight, knightWeapon) elif newPos == "§": theGame.theInfoDragon(theGame)
def test_person(): try: y=character.Player('playerright.png',50,50) except: pytest.fail('Player Not Created')
import pytest import pygame import board import random import character y=character.Player('playerright.png',50,50) play_list=pygame.sprite.Group() play_list.add(y) line_list,stair_list = board.boards().gen_board() def test_donkey(): try: a=character.Donkey('donkey.png',20,30) except: pytest.fail('Donkey Not Created') def test_person(): try: y=character.Player('playerright.png',50,50) except: pytest.fail('Player Not Created') def test_get_pos(): y=character.Donkey('donkey.png',20,30) c=0 while c<100: x = random.randrange(0,900) z = random.randrange(0,500) y.rect.x=x y.rect.y=z
import pygame, window, character, enemies pygame.init() Adam = character.Player("Adam", 18) run = True while run: pygame.time.delay(10) for event in pygame.event.get(): if event.type == pygame.QUIT: run = False key = pygame.key.get_pressed() if key[pygame.K_LEFT]: Adam.move_left() if key[pygame.K_RIGHT]: Adam.move_right() window.win.fill((0, 0, 0)) Adam.spawn() pygame.quit()
for i in range(num_of_encounters): blockPrint() #initialisation p = inflect.engine() encounter = encounters.Encounter() #Generate players players = [] with open('data/' + teams[team] + '.csv') as players_file: reader = csv.reader(players_file) next(reader, None) for row in reader: players.append( character.Player(row[0], int(row[1]), row[2], int(row[3]), int(row[4]), int(row[5]), int(row[6]), int(row[7]), int(row[8]), int(row[9]), int(row[10]), int(row[11]), int(row[12]), int(row[13]))) encounter.players = players.copy() #Generate enemies enemies = [] with open('data/enemies.csv') as enemies_file: reader = csv.reader(enemies_file) next(reader, None) for row in reader: enemies.append(character.NPC(row[0], int(row[1]), int(row[2]))) encounter.enemies = enemies.copy() #player the encounter and get the lethal damage players_lethal = encounter.play()
def main2(level, score, life, heart): fl = 0 if level == 1: line_list, stair_list = board.boards().gen_board() else: line_list, stair_list = board.boards().gen_board2() coin_list = objects.coins('coin.png').gen_coins() coin_list = objects.coins('coin.png').settle_coins( coin_list, line_list) play = character.Player('playerright.png', 50, 50) play.rect.x = 50 play.rect.y = 626 play_list.add(play) don = character.Donkey('donkey.png', 75, 80) don.rect.x = 500 don.rect.y = 98 don.dire = 0 c = 0 st = 0 img = pygame.image.load('princess.png') img = pygame.transform.scale(img, (50, 50)) while True: ll = 0 for event in pygame.event.get(): if event.type == QUIT: pygame.quit() elif event.type == KEYDOWN: if event.key == pygame.K_RIGHT or event.key == pygame.K_d: play.right() elif event.key == pygame.K_LEFT or event.key == pygame.K_a: play.left() elif event.key == pygame.K_DOWN or event.key == pygame.K_s: play.down(play_list, stair_list, fl) elif event.key == pygame.K_UP or event.key == pygame.K_w: play.up(fl, play_list, stair_list) elif event.key == pygame.K_SPACE: if fl == 0: up = -26 fl = 1 elif event.key == pygame.K_q: pygame.quit() sys.exit() if fl: play.rect.y += up up += 13 if up == 26: fl = 0 if c % 45 == 0: ball_list.add( objects.fireball('fireball.png').gen_ball(don)) if c % 2 == 0: if don.move(don.dire, level): don.dire = 1 - don.dire for ball in ball_list: a = ball.move_ball(ball.di, stair_list) if a[1] == True: ball_list.remove(ball) else: if a[0] == True: ball.di = 1 - ball.di ball_list.remove(ball) ball = objects.fireball('fireball.png').getlower( ball, line_list) ball_list.add(ball) c = (c + 1) % 45 play.rect.x, play.rect.y = play.get_lower( play.rect.x, play.rect.y, line_list, stair_list, play_list, fl) if play.rect.y == 26 and play.rect.x < 260: play_list.empty() ball_list.empty() coin_list.empty() line_list.empty() stair_list.empty() fontObj = pygame.font.Font('freesansbold.ttf', 64) textSurfaceObj = fontObj.render( "Yay!!! You saved the princess", True, Black) rect = textSurfaceObj.get_rect() rect.center = (500, 350) DISPLAYSURF.blit(textSurfaceObj, rect) score += 50 pygame.display.update() time.sleep(3) main2(3 - level, score, life, heart) aqw = objects.coins('coin.png').check_collision( coin_list, play_list) score += aqw * 5 DISPLAYSURF.fill(White) DISPLAYSURF.blit(bgimg, bgrect) if objects.fireball('fireball.png').check( ball_list, play_list): life -= 1 ball_list.empty() heart[life].change() play.rect.x = 150 play.rect.y = 626 score -= 25 score = max(score, 0) if life > 0: ll = 1 fontObj = pygame.font.Font('freesansbold.ttf', 64) textSurfaceObj = fontObj.render( "Life Lost", True, Black) rect = textSurfaceObj.get_rect() rect.center = (500, 350) DISPLAYSURF.blit(textSurfaceObj, rect) if life == 0: st = 1 fontObj = pygame.font.Font('freesansbold.ttf', 64) textSurfaceObj = fontObj.render( "Game Over!!!", True, Black) rect = textSurfaceObj.get_rect() rect.center = (500, 350) DISPLAYSURF.blit(textSurfaceObj, rect) s = 'SCORE: ' + str(score) fontObj = pygame.font.Font('freesansbold.ttf', 20) textSurfaceObj = fontObj.render(s, True, Black) rect = textSurfaceObj.get_rect() rect.center = (900, 100) DISPLAYSURF.blit(textSurfaceObj, rect) line_list.draw(DISPLAYSURF) stair_list.draw(DISPLAYSURF) coin_list.draw(DISPLAYSURF) ball_list.draw(DISPLAYSURF) play_list.draw(DISPLAYSURF) DISPLAYSURF.blit(img, (250, 26)) DISPLAYSURF.blit(don.image, don.rect) for i in range(3): DISPLAYSURF.blit(heart[i].image, heart[i].rect) pygame.display.update() if ll: time.sleep(2) if st == 1: #Game Over del heart[0:3] ball_list.empty() stair_list.empty() line_list.empty() play_list.empty() coin_list.empty() time.sleep(2) main() fpsClock.tick(FPS)