def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCR_RECT.width, SCR_RECT.height)) pygame.display.set_caption("Rappy-AI") self.font = pygame.font.Font(None, 48) self.clock = pygame.time.Clock() self.img_dict = {} # 画像は辞書型で登録 load_img(self.img_dict, os.path.join("img", "wall.png"), "wall") # wallで画像を登録 load_img(self.img_dict, os.path.join("img","rappy.png"), "bird") # birdで画像を登録 self.game_scene = GameScene(img_dict=self.img_dict, font=self.font, SCR_RECT=SCR_RECT) self.score = 0 # 行動空間の定義(0, 1) self.action_space = spaces.Discrete(2) # 状態空間の最大と最小を定義 # low = np.array([0, -WINDOW_HEIGHT]) # high = np.array([WINDOW_WIDTH, WINDOW_HEIGHT]) # ラッピーのy座標、y方向速度、壁の隙間の座標x,yの順番 low = np.array([0, -GameScene.PLAYER_SPEED_MAX, 0, 0]) high = np.array([DIVIDE_NUM_Y, GameScene.PLAYER_SPEED_MAX, DIVIDE_NUM_X, DIVIDE_NUM_Y]) self.observation_space = spaces.Box(low=low, high=high) self._seed() self.screen = None self._reset()
class Screen: '''Draw actors in the scenes''' def __init__(self): self.scene = GameScene(Color(255, 255, 255)) self.display = display.set_mode((800, 600)) self.display.fill(self.scene.color) self.font = font.Font(font.match_font('bitstreamverasans'), 20) # ~ if(image.get_extended()): # ~ self.background = image.load(os.path.abspath("desert.jpg")) # ~ self.background = self.background.convert() def drawSnakes(self): self.scene.update_actors() # ~ if(image.get_extended()): self.display.blit(self.background, (0,0)) for snake in self.scene.snakes: self.display.fill(self.scene.color) for piece in snake.body: draw.rect( self.display, snake.color, [piece[0], piece[1], snake.RECT_SIZE, snake.RECT_SIZE]) self.display.blit( self.font.render(str(snake.pontuation), True, Color(255, 0, 0)), (self.display.get_width() / 2, 0)) draw.rect(self.display, self.scene.fruit.color, [ self.scene.fruit.pos[0], self.scene.fruit.pos[1], self.scene.fruit.RECT_SIZE, self.scene.fruit.RECT_SIZE ]) display.update()
def init(): """ Scenes need pygame to be initialized (in main) before their creation. """ global scene_manager scenes = { SCN_MENU: MenuScene(), SCN_GAME: GameScene(), SCN_OVER: GameOverScene() } scene_manager = _SceneManager(scenes, SCN_MENU)
def reinit(self): old_scenes = self.scenes self.last_added = pygame.time.get_ticks() self.scenes = [] for i in range(self.no_of_games): game_scene = GameScene((self.game_scene_width, self.game_scene_height)) game_scene.score_layer.generation = old_scenes[i].score_layer.generation game_scene.score_layer.mutated = old_scenes[i].score_layer.mutated game_scene.score_layer.update_image() self.scenes.append(game_scene)
def __init__(self): self.director = Director((1200, 800), "Space Invaders") self.menu_scene = MenuScene(self.director) self.game_scene = GameScene(self.director) self.scores_scene = ScoresScene(self.director) self.director.scene_list = { "menu": self.menu_scene, "game": self.game_scene, "scores": self.scores_scene } self.director.set_scene("menu")
def __init__(self): self.director = Director((512, 480), 'Super Mario') self.menu_scene = MenuScene(self.director) self.game_scene = GameScene(self.director) self.scores_scene = ScoresScene(self.director) self.director.scene_list = { 'menu': self.menu_scene, 'game': self.game_scene, 'scores': self.scores_scene } self.director.set_scene('menu')
def process_input(self, events, pressed_keys): for event in events: if event.type == pygame.MOUSEBUTTONDOWN: mouse_pos = event.pos if self.play_button.collidepoint(mouse_pos): if self.name_input_form.get_text() != '': self.save() self.switch_to_scene(GameScene()) self.blank_from = False else: self.hint_text = self.FONT.render( 'Enter name!', False, pygame.Color(RED)) if self.back_button.collidepoint(mouse_pos): from menu_scene import MenuScene self.switch_to_scene(MenuScene()) self.name_input_form.handle_event(event)
def main(): pygame.init() screen = pygame.display.set_mode((SCR_RECT.width, SCR_RECT.height)) pygame.display.set_caption("Rappy-AI") font = pygame.font.Font(None, 48) clock = pygame.time.Clock() img_dict = {} # 画像は辞書型で登録 load_img(img_dict, os.path.join("img", "wall.png"), "wall") # wallで画像を登録 load_img(img_dict, os.path.join("img", "rappy.png"), "bird") # birdで画像を登録 game_scene = GameScene(img_dict=img_dict, font=font, SCR_RECT=SCR_RECT) # while True: while not game_scene.get_exit_flag(): clock.tick(60) # 60fps game_scene.step() game_scene.render(screen)
def main(): # configure the logger before anything else happens logging.config.fileConfig('logging.conf') # The game logger is configured when the config module is imported. log = logging.getLogger('game') log.info('Main started') # load levels levels = load_level_set(LEVELS_FILENAME, LEVELS_MAXSIZE) # list pg.init() pg.display.set_caption('Sokobalt') pview.set_mode(BASE_RES) clock = pg.time.Clock() scenes = {SCN_MENU: MenuScene(levels), SCN_GAME: GameScene(levels)} cur_scene = scenes[SCN_MENU] while True: ms = clock.tick(FPS) # throttle # poll controls outcome = controller.poll() # get player input if outcome == OUT_QUIT: break elif outcome == OUT_FSCR: pview.toggle_fullscreen() cur_scene.redraw() # tick scene next_scene_id, kwargs = cur_scene.tick(ms) if next_scene_id == SCN_QUIT: # quit via dummy scene constant break elif next_scene_id is not None: # change scene cur_scene.pause() cur_scene = scenes[next_scene_id] cur_scene.resume(**kwargs)
from game_object import GameObject from game_scene import GameScene from rect import Rect from vector import Vector class DisplayScene(Scene): def __init__(self, game_scene: GameScene): self.game_scene = game_scene self.screen_rect = Rect((0, 0), (10, 10)) def on_enter(self, previous_scene: 'Scene' = None, multi_id: int = None): pygame.display.set_caption(self.game_scene.name) def draw(self, screen: pygame.Surface, multi_id: int = None): self.game_scene.render(screen, self.screen_rect, debug=True) def update(self, dt: int, multi_id: int = None): self.game_scene.game_objects[0].transform.rotation += dt / 100 test_obj = GameObject("TestObj") transform = test_obj.add_component(Transform) transform.pos = Vector(4, 4) renderer = test_obj.add_component(SolidColorRenderer) renderer.offset.x = 1 renderer.color = Color(1, 1, 0) game_scene = GameScene("Test", [test_obj], Color(1., 1., 1.)) app = Application((640, 640), "", resizable=True) app.run(DisplayScene(game_scene))
import pybullet as p import time import pybullet_data from pyxie.apputil import graphicsHelper import pyxie import pyvmath as vmath import math import random from game_scene import GameScene from scene_manager import SceneManager import imgui import sys SCREEN_WIDTH = 520 SCREEN_HEIGHT = 900 pyxie.window(True, SCREEN_WIDTH , SCREEN_HEIGHT) imgui.create_context() gameScene = GameScene() SceneManager.SetCurrentScene(gameScene) while(1): SceneManager.GetCurrentScene().Update() SceneManager.GetCurrentScene().Render() pyxie.swap() # game loop
import pygame from director import Director from game_scene import GameScene from menu_scene import MenuScene pygame.init() game_director = Director((800, 450), "Simple Ping Pong") game_scene = GameScene(game_director, True) menu_scene = MenuScene(game_director, game_scene) game_scene.set_menu_scene(menu_scene) game_director.change_scene(menu_scene) game_director.loop()
def __init__(self): self.scene = GameScene(Color(255, 255, 255)) self.display = display.set_mode((800, 600)) self.display.fill(self.scene.color) self.font = font.Font(font.match_font('bitstreamverasans'), 20)
class RappyEnv(gym.Env): metadata = { 'render.modes': ['human', 'rgb_array'], 'video.frames_per_second' : 50 } def __init__(self): pygame.init() self.screen = pygame.display.set_mode((SCR_RECT.width, SCR_RECT.height)) pygame.display.set_caption("Rappy-AI") self.font = pygame.font.Font(None, 48) self.clock = pygame.time.Clock() self.img_dict = {} # 画像は辞書型で登録 load_img(self.img_dict, os.path.join("img", "wall.png"), "wall") # wallで画像を登録 load_img(self.img_dict, os.path.join("img","rappy.png"), "bird") # birdで画像を登録 self.game_scene = GameScene(img_dict=self.img_dict, font=self.font, SCR_RECT=SCR_RECT) self.score = 0 # 行動空間の定義(0, 1) self.action_space = spaces.Discrete(2) # 状態空間の最大と最小を定義 # low = np.array([0, -WINDOW_HEIGHT]) # high = np.array([WINDOW_WIDTH, WINDOW_HEIGHT]) # ラッピーのy座標、y方向速度、壁の隙間の座標x,yの順番 low = np.array([0, -GameScene.PLAYER_SPEED_MAX, 0, 0]) high = np.array([DIVIDE_NUM_Y, GameScene.PLAYER_SPEED_MAX, DIVIDE_NUM_X, DIVIDE_NUM_Y]) self.observation_space = spaces.Box(low=low, high=high) self._seed() self.screen = None self._reset() def _seed(self, seed=None): self.np_random, seed = seeding.np_random(seed) return [seed] def _step(self, action): assert self.action_space.contains(action), "%r (%s) invalid" %(action, type(action)) # 無条件報酬(生存時間あたりの報酬) reward = 0.0 # y座標に比例して増やす報酬を下に追加したため一旦0に変更 self.game_scene.step(action) # self.state = self.game_scene.get_nearest_gap_distance() # state計算(ラッピーのy座標、yスピード、隙間のx座標、y座標) self.state = np.array([simplify_pos(self.game_scene.get_rappy_pos()[1], SCR_RECT.height, DIVIDE_NUM_Y), self.game_scene.get_rappy_speed(), simplify_pos(self.game_scene.get_nearest_gap_pos()[0], SCR_RECT.width, DIVIDE_NUM_X), simplify_pos(self.game_scene.get_nearest_gap_pos()[1], SCR_RECT.height, DIVIDE_NUM_Y)]) # ジャンプした場合にマイナスの報酬(ジャンプしすぎるので)。 # 連続でジャンプした場合に限定(2020/07/26) if action == 1 and self.game_scene.get_rappy_speed() <= -GameScene.PLAYER_SPEED_MAX + 1: #reward += -50.0 reward += -1.0 # 天井に接着しているときマイナスの報酬 if self.game_scene.is_rappy_on_top(): reward += 0.0 # スコアを獲得したときの報酬 if self.score < self.game_scene.get_score(): reward += 3000.0 self.score = self.game_scene.get_score() else: reward += 0.0 # ラッピーのy座標と隙間のy座標が近ければ近いほど報酬が多くもらえる # reward += np.round((DIVIDE_NUM_Y - (abs(self.state[3] - self.state[0]))) / 10, decimals=2) reward += DIVIDE_NUM_Y - (abs(self.state[3] - self.state[0])) done = self.game_scene.is_rappy_dead() or self.game_scene.count > 1000 if done: # 死んだときマイナスの報酬 reward += -500.0 # 死亡地点にて、隙間との距離に応じて追加報酬もあり→加点だと、壁にぶつかることで報酬が増えると勘違いされる可能性がある # reward += (DIVIDE_NUM_Y - (abs(self.state[3] - self.state[0]))) ** 2 reward += -((self.state[3] - self.state[0]) ** 2) self.game_scene.exit() return np.array(self.state), reward, done, {} def _reset(self): # とりあえず画面右端の真ん中に隙間がある状態を初期状態とする self.game_scene.init() # self.state = np.array([SCR_RECT.width / 2, 0]) self.state = np.array([DIVIDE_NUM_Y / 2, 0, DIVIDE_NUM_X / 2, DIVIDE_NUM_Y / 2]) self.steps_beyond_done = None return np.array(self.state) def _render(self): if self.screen is None: self.screen = pygame.display.set_mode((SCR_RECT.width, SCR_RECT.height)) pygame.display.set_caption("Rappy-AI") self.game_scene.render(self.screen) return
from pyglet.input import get_joysticks import config from game_scene import GameScene joysticks = get_joysticks() game_name = "Robot Warz" import patch_director patch_director.exec() from cocos.director import director window = director.init( width=config.screen_size[0], height=config.screen_size[1], caption=game_name, resizable=True ) director._usable_width = config.screen_size[0] * 2 director._usable_height = config.screen_size[1] * 2 director.show_FPS = True print("Window config: {0}".format(window.config)) director.run(GameScene())
import pygame import global_vars as gv # init game screen pygame.init() pygame.display.set_caption('PySnake!') gv.game_screen = pygame.display.set_mode((800, 600)) # this import requires an active game_screen from game_scene import GameScene # set entry scene here! active_scene = GameScene() while active_scene != None: # time between two frames globally available gv.elapsed_time = gv.clock.tick() / 1000 # clear screen gv.game_screen.fill(gv.black) active_scene.events() active_scene.update() active_scene.draw() active_scene = active_scene.next # update screen pygame.display.update()
def update(self) -> None: if pyxel.btnp(pyxel.KEY_SPACE): self.change_scene(GameScene(self.director))
def quit_game(): pygame.quit() sys.exit() if __name__ == "__main__": pygame.init() surface = pygame.display.set_mode((display_width, display_height)) pygame.display.set_caption('Vacation') fps_clock = pygame.time.Clock() font = pygame.font.Font(pygame.font.get_default_font(), 30) scene_stack = SceneStack() game_scene = GameScene(display_width, display_height, scene_stack, Difficulty.Easy) scene_stack.push_scene(game_scene) while True: for event in pygame.event.get(): if event.type == QUIT: quit_game() elif event.type == KEYDOWN: if event.key == K_RETURN and event.mod & KMOD_ALT: pygame.display.toggle_fullscreen() elif event.key == K_ESCAPE: quit_game() surface.fill(grass) scene_stack.update() scene_stack.draw(surface) fps_display = font.render(str(int(fps_clock.get_fps())), False, white)
def process_inputs(self, events, pressed_keys): if any(pressed_keys) and self.can_begin: self.switch_to_scene(GameScene())
def main(): run_game(constants.SCREEN_L, constants.SCREEN_B, 60, GameScene()) #TitleScene())
def handle_event(self, event): if event.type == KEYDOWN and event.key == K_SPACE: self.manager.switch_to(GameScene())
def ProcessInput(self, events, pressed_keys): for event in events: if event.type == pygame.KEYDOWN and event.key == pygame.K_RETURN: # Move to the next scene when the user pressed Enter self.SwitchToScene(GameScene())
def init(self): self.last_added = pygame.time.get_ticks() self.scenes = [] for i in range(self.no_of_games): game_scene = GameScene((self.game_scene_width, self.game_scene_height)) self.scenes.append(game_scene)
def handle_events(self, events): for e in events: if e.type == KEYDOWN and e.key == K_SPACE: mixer.music.stop() self.manager.go_to(GameScene())