def __init__(self, verbose_output=False): self.player_1 = player.Player('player_1', []) self.player_2 = player.Player('player_2', []) self.result = None self.verbose_output = verbose_output self._allot_cards()
def two_players(self): self.playing = True self.shots = 0 p1 = player.Player(game=self) p1.place_ships() p2 = player.Player(num=2, game=self) p2.place_ships() status = [] while True: status.insert(0, p1.take_shot(p2, self.get_status(status))) status = self.clean_status(status) self.shots+=1 # Check for a winner if p2.check_end(): utils.cls() utils.head("Player 1 Won!") print(" ") print("Player 1 triumphed victoriously over Player 2!") print(" ") print(p1.board.mux_boards( p1.board.print_board(title = "Player 1"), p2.board.print_board(title = "Player 2") )) print(" ") print("Game took 1 shot." if self.shots == 1 else "Game took {} shots.".format(self.shots)) print(" ") utils.get("Press [enter] to continue...") self.playing = False return status.insert(0, p2.take_shot(p1, self.get_status(status))) status = self.clean_status(status) self.shots+=1 # Check for a winner if p1.check_end(): utils.cls() utils.head("Player 2 Won!") print(" ") print("Player 2 triumphed victoriously over Player 1!") print(" ") print(p1.board.mux_boards( p1.board.print_board(title = "Player 1"), p2.board.print_board(title = "Player 2") )) print(" ") print("Game took 1 shot." if self.shots == 1 else "Game took {} shots.".format(self.shots)) print(" ") utils.get("Press [enter] to continue...") self.playing = False return
def main(): """Main program.""" # ask human color game_board = board.Board() human_color = "white" computer_color = "black" human = player.Player("human", human_color) computer = player.Player("computer", computer_color) winner = False while not winner: draw_game(human, computer, game_board) move = raw_input("Please input your move: ") game_board.set_element(move, "HOR")
def reset_level(): global score, player_car, old_player_position global game_objects, event_stack_size while event_stack_size > 0: game_window.pop_handlers() event_stack_size -= 1 score = 0 # Initialize the player sprite player_car = player.Player(max_x=game_window.width, max_y=game_window.height, x=game_window.width / 4, y=game_window.height / 3, batch=main_batch) old_player_position = [player_car.x, player_car.y] # Store all objects that update each frame in a list game_objects = [player_car] # Add any specified event handlers to the event handler stack for obj in game_objects: for handler in obj.event_handlers: game_window.push_handlers(handler) event_stack_size += 1
def reset_level(): global player_ship, game_objects, event_stack_size # clear the event handler stack while event_stack_size > 0: game_window.pop_handlers() event_stack_size -= 1 # initialize the player ship if it doesn't already exist in the level if player_ship is None: player_ship = player.Player(x=width // 2, y=height // 2, batch=main_batch) # adding asteroids makes the game less boring asteroids = load.asteroids(current_level, player_ship.position, main_batch) if not game_objects: game_objects = [player_ship] game_objects = game_objects + asteroids # keeps bullets flying between levels # add event handlers for obj in game_objects: for handler in obj.event_handlers: game_window.push_handlers( handler) # tells pyglet who responds to events event_stack_size += 1 level_label.text = 'Level: {0}'.format(current_level)
def reset_level(num_lives=parameters.num_lives): global player_ship, player_lives, game_objects, event_stack_size, level, num_asteroids # Clear the event stack of any remaining handlers from other levels while event_stack_size > 0: game_window.pop_handlers() event_stack_size -= 1 # Initialize the player sprite player_ship = player.Player(x=parameters.half_width, y=parameters.half_height, batch=main_batch) # Make a number of sprites to represent remaining lives player_lives = load.player_lives(num_lives, batch=main_batch) # Make some asteroids so we have something to shoot at asteroids = load.asteroids(num_asteroids, player_ship.position, batch=main_batch) # Store all objects that update each frame in a list game_objects = [player_ship] + asteroids # Add any specified event handlers to the event handler stack for obj in game_objects: for handler in obj.event_handlers: game_window.push_handlers(handler) event_stack_size += 1 level_label.text = f"Level: {level}"
def __init__(self): super(Game, self).__init__(255, 255, 255, 255) self.player = player.Player() self.collision_manager = cm.CollisionManagerBruteForce() self.add(self.player, z=1) self.player.do(Move()) self.player.jumping = False self.collision_manager.add(self.player) self.obstacle = cocos.sprite.Sprite(resources.obstacle) self.obstacle.position = 770, 30 self.obstacle.velocity = -100, 0 self.obstacle.speed = 50 self.add(self.obstacle, z=1) self.obstacle.do(Move()) self.obstacle.cshape = cm.AARectShape(self.obstacle.position, self.obstacle.width / 3, self.obstacle.height / 2) self.collision_manager.add(self.obstacle) self.player.schedule(self.update)
def reset_level(num_lives=2): global player_ship, player_lives, game_objects, event_stack_size # 清空事件栈 while event_stack_size > 0: game_window.pop_handlers() event_stack_size -= 1 for life in player_lives: life.delete() # 创建Player的实例 player_ship = player.Player(x=400, y=300, batch=main_batch) # 加载小飞船图标 player_lives = load.player_lives(num_lives, main_batch) # 加载多个小行星 asteroids = load.asteroids(num_asteroids, player_ship.position, main_batch) # 加载多个陨石 meteorites = load.meteorites(num_meteorites, player_ship.position, main_batch) # 为了调用每一个物体的update函数,需要一个列表来存放这些物体。 game_objects = [player_ship] + asteroids + meteorites ''' 告诉pyglet 哪些实例是事件句柄(event handler) 用game_window.push_handlers()函数把它压入事件栈中 ''' for obj in game_objects: for handler in obj.event_handlers: game_window.push_handlers(handler) event_stack_size += 1
def reset_level(num_lives=2): global player_ship, player_lives, game_objects, event_stack_size # Clear the event stack of any remaining handlers from other levels while event_stack_size > 0: game_window.pop_handlers() event_stack_size -= 1 for life in player_lives: life.delete() # Initialize the player sprite player_ship = player.Player(x=400, y=300, batch=main_batch) # Make three sprites to represent remaining lives player_lives = load.player_lives(num_lives, main_batch) # Make some asteroids so we have something to shoot at asteroids = load.asteroids(num_asteroids, player_ship.position, main_batch) # Store all objects that update each frame in a list game_objects = [player_ship] + asteroids # Add any specified event handlers to the event handler stack for obj in game_objects: for handler in obj.event_handlers: game_window.push_handlers(handler) event_stack_size += 1
def create_objects(): player_ship = player.Player(x=(window_width//2)-250, y=(window_height//2), batch=main_batch) game_window.push_handlers(player_ship) game_window.push_handlers(player_ship.key_handler) game_objects = [player_ship] return player_ship, game_objects
def create_objects(): player_ship = player.Player(x=window_width//2, y=window_height//2, batch=main_batch) game_window.push_handlers(player_ship) game_window.push_handlers(player_ship.key_handler) asteroids = load.asteroids(3, player_ship.position, batch=main_batch) game_objects = [player_ship] + asteroids return player_ship, game_objects
def __init__(self): pygame.init() fpsClock = pygame.time.Clock() screen = pygame.display.set_mode((800, 600), 0, 32) pygame.display.set_caption("Tutorial") self.first = player.Player(screen, (20, 20)) self.first.direction = [0, 0] self.execute(screen, fpsClock)
def init(): global player_ship, game_objects, event_stack_size player_ship = player.Player(x=400, y=300, batch=main_batch) for handler in player_ship.event_handlers: game_window.push_handlers(handler) event_stack_size += 1 space.add(player_ship.body, player_ship.shape) h = space.add_collision_handler(1, 2) h.begin = collisionhandle
def start(self): self.client.player = player.Player(100, 100, 20, name=self.name_ib.get_text(), color=(0, 100, 200)) creds = self.ip_ib.get_text().split(':') self.client.server = server.Server(ip=creds[0], port=int(creds[1]), limit=4) self.client.server.player_class = self.client.player.__class__ threading.Thread(target=self.client.server.start_server).start() self.join()
def test_move_outside_bounds_by_2(): p1 = player.Player("Joe", [0, 0], "player1") try: p1.move(-2, 0) assert False except IllegalMoveException: assert p1.position == [0, 0] try: p1.move(0, -2) assert False except IllegalMoveException: assert p1.position == [0, 0]
def test_move_outside_bounds_by_1_up(): p1 = player.Player("Joe", [4, 14], "player1") try: p1.move(1, 0) assert False except IllegalMoveException: assert p1.position == [4, 14] try: p1.move(0, 1) assert False except IllegalMoveException: assert p1.position == [4, 14]
def one_player(self): self.playing = True self.shots = 0 p1 = player.Player(game=self) p1.place_ships() p2 = player.BotPlayer(num=2, game=self) status = [] while True: status.insert(0, p1.take_shot(p2, self.get_status(status))) status = self.clean_status(status) self.shots+=1 # Check for a winner if p2.check_end(): utils.cls() utils.head("Player 1 Won!") print(" ") print("You are victorious!") print(" ") print(p1.board.mux_boards( p1.board.print_board(title = "You"), p2.board.print_board(title = "Bot 2") )) print(" ") print("Game took 1 shot." if self.shots == 1 else "Game took {} shots.".format(self.shots)) print(" ") utils.get("Press [enter] to continue...") self.playing = False return status.insert(0, p2.take_shot(p1, self.get_status(status))) status = self.clean_status(status) self.shots+=1 # Check for a winner if p1.check_end(): utils.cls() utils.head("Player 1 Lost!") print(" ") print("You have been defeated!") print(" ") print(p1.board.mux_boards( p1.board.print_board(title = "You"), p2.board.print_board(title = "Bot 2") )) print(" ") print("Game took 1 shot." if self.shots == 1 else "Game took {} shots.".format(self.shots)) print(" ") utils.get("Press [enter] to continue...") self.playing = False return
currentDict["PlatformTypes"] = [resources.testRoomFloorImage] currentDict["PlatformPositions"] = [[160,40,0],[480,40,0],[800,40,0],[1120,40,0]] currentLevel = level_template.LevelTemplate(currentDict) else: currentDict = {} currentDict["Name"] = "Level One" currentDict["Window"] = GameWindow currentDict["Batch"] = bigBatch currentDict["Background"] = resources.absoluteBackImage currentDict["PlatformTypes"] = [resources.platformOneImage, resources.platformTwoImage] currentDict["PlatformPositions"] = [[225, 290, 1],[500, 320, 0],[740, 340, 0],[990, 380, 0],[1280, 220, 1],[1560, 210, 0],[1830, 220, 1],[2120, 270, 0]] currentLevel = level_template.LevelTemplate(currentDict) player = player.Player(img=resources.playerImage, x=225, y=680, batch=bigBatch) GameWindow.push_handlers(player.keyHandler) gameObjects = [player, currentLevel] #for object in currentLevel.platforms: # gameObjects.append(object) #def update(dt): # for platform in platforms: # if platform.x - platform.width / 2 < player.x + player.width: # if player.x < platform.x+platform.width/2 and player.y >= platform.y+platform.height/2-20 and player.y <= platform.y+platform.height/2+10: # if player.falling: # player.landed(platform.y+platform.height/2) # for object in gameObjects: # object.update(dt)
self.time += dt m, s = divmod(self.time, 60) self.label.text = '%02d:%02d' % (m, s) if m >= 1: self.label.delete() self.label = pyglet.text.Label('Game Time Over!', font_size=10, x = 600, y = 520, bold=True, batch = main_batch) self.label.color = (0, 0, 0, 100) self.running = False # Draw the Barb Fence barb_horizontal1 = pyglet.sprite.Sprite(resources.barb_horizontal,x=0,y=0, batch = main_batch) barb_horizontal2 = pyglet.sprite.Sprite(resources.barb_horizontal,x=0,y=540, batch = main_batch) barb_vertical1 = pyglet.sprite.Sprite(resources.barb_vertical,x=0,y=0, batch = main_batch) barb_vertical2 = pyglet.sprite.Sprite(resources.barb_vertical,x=740,y=0, batch = main_batch) cat_sprite = player.Player(x=300, y=400, batch=main_batch) rat_sprite = rat.Player(x=400, y=500, batch=main_batch) catfood_sprite = catfood.Player(x=400, y=500, batch=main_batch) drycatfood_sprite = drycatfood.Player(x=400, y=500, batch=main_batch) fish_sprite = fish.Player(x=400, y=500, batch=main_batch) goldfish_sprite = goldfish.Player(x=400, y=500, batch=main_batch) mice_sprite = mice.Player(x=400, y=500, batch=main_batch) milk_sprite = milk.Player(x=400, y=500, batch=main_batch) woolenball_sprite = woolenball.Player(x=400, y=500, batch=main_batch) tinfood_sprite = tinfood.Player(x=400, y=500, batch=main_batch) #lives = load.lives(0, cat_sprite.position, main_batch) life_objects = [rat_sprite, catfood_sprite, drycatfood_sprite, fish_sprite, goldfish_sprite, mice_sprite, milk_sprite, woolenball_sprite, tinfood_sprite]
### Run this File to start the game. import pyglet from pyglet.gl import glEnable, glTexParameteri, GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST from game import player from game.window import Game_Window glEnable(GL_TEXTURE_2D) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST) window = Game_Window(1280, 720, "SBLD", resizable=True) main_batch = pyglet.graphics.Batch() player_character = player.Player(x=400, y=200, batch=main_batch) window.push_handlers(player_character) window.push_handlers(player_character.key_handler) game_objects = [player_character] @window.event def on_draw(): window.clear() main_batch.draw() def update(dt): for obj in game_objects: obj.update(dt)
This file tests all methods in game.py and player.py """ import unittest import os, sys, inspect from unittest import MagicMock, MockResponse currentdir = os.path.dirname( os.path.abspath(inspect.getfile(inspect.currentframe))) parentdir = os.path.dirname(currentdir) sys.path.insert(0, parentdir) # stuff required for testing import random from game import game, player, scenario, game_io test_unset_player = player.Player() test_strong_player = player.Player("stronk", 20, 20, 20, 20, 20, 0) SCENARIO_NAME = "beginning" SEQ_INPUT = [] class GameTestCase(unittest.TestCase): def test_game(self): # Mock scenario game.scenario = MagicMock( return_value=MockResponse({test_unset_player, "end"})) self.assertEqual(game("test"), "test") class PlayerTestCase(unittest.TestCase): def test_player_attack(self):
def reset(self): while self.eventstacksize > 0: self.pop_handlers() self.eventstacksize -= 1 # for to_remove in self.gameobjects: # print(type(to_remove)) # to_remove.delete() # self.gameobjects.remove(to_remove) # print(dir(self.enemybatch)) self.dinobatch = pyglet.graphics.Batch() self.enemybatch = pyglet.graphics.Batch() self.backgroundbatch = pyglet.graphics.Batch() self.dino = player.Player(x=45, y=0, batch=self.dinobatch) self.ground = floor.Floor(x=-10, y=0, batch=self.backgroundbatch) self.enemy = [] self.gameobjects.clear() self.gameobjects = [self.dino, self.ground] + self.enemy for obj in self.gameobjects: for handler in obj.event_handlers: self.push_handlers(handler) self.eventstacksize += 1 self.push_handlers(self.keyboard) self.clouds = False self.birds = False self.score = 0 self.gameovertext = 0 self.gamespeed = 1 self.collided = False self.successcount = 0 self.gameover = False if not self.playercontrolled: for k, v in self.dinoinputs().items(): self.nninput[k] = v self.nninput['gamespeed'] = self.gamespeed self.scoretext = pyglet.text.Label( text=f"{int(round(self.score, 0)):05d}", x=1260, y=330, font_name='Press Start 2P', font_size=25, color=(83, 83, 83, 255), batch=self.backgroundbatch) self.gameovertext = pyglet.text.Label(text=f"GAME OVER", x=self.width / 2, y=self.height / 2 + 20, font_name='Press Start 2P', font_size=25, color=(83, 83, 83, 0), anchor_x='center', anchor_y='center', batch=self.backgroundbatch) self.pressanykey = pyglet.text.Label( text=f"SPACE to play again ESC to exit", x=self.width / 2, y=self.height / 2 - 20, font_name='Press Start 2P', font_size=10, color=(83, 83, 83, 0), anchor_x='center', anchor_y='center', batch=self.backgroundbatch) self.speed = pyglet.text.Label(text=f"{self.gamespeed}", x=20, y=310, font_name='Press Start 2P', font_size=15, color=(83, 83, 83, 255), batch=self.backgroundbatch) self.countertext = pyglet.text.Label(text=f"{self.successcount}", x=1260, y=290, font_name='Press Start 2P', font_size=25, color=(83, 83, 83, 255), batch=self.backgroundbatch) self.ow = pyglet.text.Label(text=f"ow", x=500, y=200, font_name='Press Start 2P', font_size=25, color=(255, 255, 255, 0), batch=self.backgroundbatch)
anchor_x='center', batch=main_batch, font_size=48) # bg = pyglet.sprite.Sprite(resources.road_image) # bg.scale=0.7 # bg.x = 250 # bg.y = 300 PLAYER_SIZE = 100 score = 1 game_objects = [] gamers = [] # blocks = [] car_ship = player.Player(x=309, y=379, batch=main_batch) # replaced name of asteroids by blocks blocks = [] #load.gen_enemies(3,batch= main_batch) # print("length at start of block is "+str(len(blocks))) # # Store all objects that update each frame in a list blocks = [car_ship] + blocks # # Tell the main window that the player object responds to events game_window.push_handlers(car_ship) rewardArr = [] rewardArr.append((235, 446, 389, 421, 1)) rewardArr.append((229, 478, 411, 459, 0)) rewardArr.append((229, 518, 388, 497, 0)) rewardArr.append((236, 558, 400, 534, 0))
def test_player(): test_obj = player.Player("testPlayer", [0, 0]) assert (test_obj.name == "testPlayer") assert (test_obj.health == 100) assert (test_obj.position == [0, 0])
import pyglet from game import player, monster, resources from random import randint from config import WIDTH, HEIGHT # Set up a window game_window = pyglet.window.Window(WIDTH, HEIGHT) main_batch = pyglet.graphics.Batch() # Set up the two top labels score_label = pyglet.text.Label(text="Caught 0", x=10, y=575, batch=main_batch) # Initialize the player sprite hero = player.Player(x=400, y=300, batch=main_batch) goblin = monster.Monster(x=randint(0, WIDTH), y=randint(0, HEIGHT), batch=main_batch) # Store all objects that update each frame in a list game_objects = [hero, goblin] # Tell the main window that the player object responds to events game_window.push_handlers(hero.key_handler) @game_window.event def on_draw(): game_window.clear() resources.background.blit(0, 0)
char_green.CharGreen(map_x=x, map_y=y, group=foreground, batch=game_batch)) # def make_ai_chars_dict(ai_chars_list): # return {(int(char.x), int(char.y)): char for char in ai_chars_list} # max_width_ai_chars, max_height_ai_chars = util.get_max_dims(r.ai_char_imgs) protagonist = player.Player(c_img=r.player_image, l_img=r.player_left_image, r_img=r.player_right_image, c_ani=r.player_animation, l_ani=r.player_left_animation, r_ani=r.player_right_animation, x=game_window.width // 2, y=game_window.height // 2, map_x=map_obj.half_width, map_y=map_obj.half_height, batch=game_batch, group=foreground) for handler in [ item for row in [getattr(obj, "event_handlers") for obj in (protagonist, game_hud)] for item in row ]: game_window.push_handlers(handler) # @game_window.event # def on_resize(width, height):
y=5, batch=main_batch) egg_num_label = pyglet.text.Label(text="Egg: 0", bold=True, x=100, y=5, batch=main_batch) level_label = pyglet.text.Label(text="Lucky Bird", bold=True, x=conf.windows_width / 2, y=conf.windows_height - 20, anchor_x='center', batch=main_batch) player_ship = player.Player(x=conf.windows_width / 2, y=conf.windows_height / 2, batch=main_batch) angrybirds = load.angrybirds(conf.bird_num, player_ship.position, main_batch) eggs = load.eggs(1, player_ship.position, main_batch) player_lives = load.player_lives(conf.player_life_num, main_batch) mywin.push_handlers(player_ship.key_handler) mywin.push_handlers(player_ship) @mywin.event def on_draw(): mywin.clear() main_batch.draw()
def setUpClass(self): self.m = Map(8) self.p = player.Player("SebastianDenAndreo", "knight", self.m.get_room(0, 1)) self.v = view.View() self.m.get_room(2, 2).has_exit = True
# Set up a window game_window = pyglet.window.Window(800, 600) main_batch = pyglet.graphics.Batch() # Set up the two top labels score_label = pyglet.text.Label(text="Score: 0", x=10, y=575, batch=main_batch) level_label = pyglet.text.Label(text="Version 3: Basic Collision", x=400, y=575, anchor_x='center', batch=main_batch) # Initialize the player sprite player_ship = player.Player(x=400, y=300, batch=main_batch) # Make three sprites to represent remaining lives player_lives = load.player_lives(2, main_batch) # Make three asteroids so we have something to shoot at asteroids = load.asteroids(3, player_ship.position, main_batch) # Store all objects that update each frame in a list game_objects = [player_ship] + asteroids # Tell the main window that the player object responds to events game_window.push_handlers(player_ship.key_handler) @game_window.event
image.anchor_x = image.width // 2 image.anchor_y = image.height // 2 #center player center_image(testplayer) center_image(flipplayer) #make stuff drawable sprites #player should be instance of Sprite subclass --> make player PhysicalObject #which is child of Sprite to access motion styff #physicalobject.PhysicalObject(etc.) --> make player Player object #which is child of PhysicalObject to access player control floor = pyglet.sprite.Sprite(img=ground, x=0, y=0, batch=main_batch) startMenu = pyglet.sprite.Sprite(img=menu, x=0, y=0) terrorist = player.Player(img=testplayer, x=400, y=300, batch=main_batch) #game_window handles events #make player into list of itself to concatenate into game_objects module; they #must all be an instance of or child of PhysicalObject game_objects = [terrorist] #get some labels going; add to main batch score_label = pyglet.text.Label(text="Days: " + str(days), x=10, y=520, batch=main_batch) release_label = pyglet.text.Label(text="version1 alpha", x=10, y=550,