def __init__(self): vsync = config.IS_VSYNC if config.IS_FULLSCREEN: self.__window = window.Window( fullscreen=True, vsync=vsync ) else: width,height = config.WIN_SIZE self.__window = window.Window( width=width, height=height, fullscreen=False, vsync=vsync ) self.__winSize = winSize = ( self.__window.width, self.__window.height ) self.__camera = Camera( winSize ) self.__hud = HUD( winSize ) self.__inputManager = InputManager() self.__window.on_key_press = self.__inputManager.key_pressed self.__window.on_key_release = self.__inputManager.key_released if config.IS_FPS_LIMIT: clock.set_fps_limit( FPS_LIMIT ) glDepthFunc( GL_LEQUAL ) glEnable( GL_DEPTH_TEST ) self.__world = GameWorld(self) # to musi być na końcu
def __init__(self): vsync = config.IS_VSYNC if config.IS_FULLSCREEN: self.__window = window.Window(fullscreen=True, vsync=vsync) else: width, height = config.WIN_SIZE self.__window = window.Window(width=width, height=height, fullscreen=False, vsync=vsync) self.__winSize = winSize = (self.__window.width, self.__window.height) self.__camera = Camera(winSize) self.__hud = HUD(winSize) self.__inputManager = InputManager() self.__window.on_key_press = self.__inputManager.key_pressed self.__window.on_key_release = self.__inputManager.key_released if config.IS_FPS_LIMIT: clock.set_fps_limit(FPS_LIMIT) glDepthFunc(GL_LEQUAL) glEnable(GL_DEPTH_TEST) self.__world = GameWorld(self) # to musi być na końcu
def main(): # Game constants SCREEN_WIDTH = 500 SCREEN_HEIGHT = 500 BLACK_COLOUR = (0, 0, 0) WHITE_COLOUR = (255, 255, 255) BLOCK_LENGTH = 20 # Game variables is_game_over = False jump = BLOCK_LENGTH x_change = 0 y_change = 0 speed = 10 # Basic set up pygame.init() pygame.display.set_caption("Simple Snake") screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) # Set up score text font = pygame.font.Font("assets/Arial.ttf", 24) score_text_position = (10, 10) # Set up snake and fruit game_world = GameWorld(screen, BLOCK_LENGTH) i = 0 # Main game loop while True: i += 1 # Handle events for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() quit() elif event.type == pygame.KEYDOWN: if not is_game_over: if event.key == pygame.K_LEFT: if x_change == 0: x_change = -jump y_change = 0 elif event.key == pygame.K_RIGHT: if x_change == 0: x_change = jump y_change = 0 elif event.key == pygame.K_DOWN: if y_change == 0: y_change = jump x_change = 0 elif event.key == pygame.K_UP: if y_change == 0: y_change = -jump x_change = 0 else: if event.key == pygame.K_SPACE: score = 0 is_game_over = False x_change = 0 y_change = 0 game_world = GameWorld(screen, BLOCK_LENGTH) # Update states if not is_game_over: if (i % speed) == 0: game_world.update(x_change, y_change) i = 0 # Check for collisions is_game_over = game_world.check_if_game_over() # Render graphics screen.fill(BLACK_COLOUR) # Draw snake game_world.snake.draw(pygame, screen) # Draw fruit game_world.fruit.draw(pygame, screen) # Draw texts if is_game_over: end_game_text = font.render( "Game over! Final score: {}".format(game_world.score), 0, WHITE_COLOUR) play_again_text = font.render("Press SPACE to play again", 0, WHITE_COLOUR) end_game_text_rect = end_game_text.get_rect() end_game_text_rect.center = (SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) - 25) play_again_text_rect = play_again_text.get_rect() play_again_text_rect.center = (SCREEN_WIDTH / 2, (SCREEN_HEIGHT / 2) + 25) screen.blit(end_game_text, end_game_text_rect) screen.blit(play_again_text, play_again_text_rect) else: score_text = font.render("Score: {}".format(game_world.score), 1, WHITE_COLOUR) screen.blit(score_text, score_text_position) pygame.display.update()
class App(object): def __init__(self): vsync = config.IS_VSYNC if config.IS_FULLSCREEN: self.__window = window.Window(fullscreen=True, vsync=vsync) else: width, height = config.WIN_SIZE self.__window = window.Window(width=width, height=height, fullscreen=False, vsync=vsync) self.__winSize = winSize = (self.__window.width, self.__window.height) self.__camera = Camera(winSize) self.__hud = HUD(winSize) self.__inputManager = InputManager() self.__window.on_key_press = self.__inputManager.key_pressed self.__window.on_key_release = self.__inputManager.key_released if config.IS_FPS_LIMIT: clock.set_fps_limit(FPS_LIMIT) glDepthFunc(GL_LEQUAL) glEnable(GL_DEPTH_TEST) self.__world = GameWorld(self) # to musi być na końcu def get_window_coords(self): """ Zwraca współrzędne czworokąta, w którym można rysować. """ return self.__camera.windowCoords def register_input_observer(self, obj): self.__inputManager.register_observer(obj) def get_window_dim(self): """ Zwraca wymiary okna (szerokość, wysokość) """ return (float(self.__window.width), float(self.__window.height)) def get_window_draw_dim(self): """ Zwraca współrzędne, w których należy rysować (szerokość, wysokość). """ return (1000.0, 750.0) # return (800.0, 600.0) def main_loop(self): """ Pętla główna gry. """ self.__timeElapsed = 0.0 # czas jaki upłynął od początku gry splash = Splash(self, self.__camera, config.SPLASH_DISPLAY_TIME) while not self.__window.has_exit: self.__window.dispatch_events() # update świata i HUDa dt = clock.tick() * config.DTIME_MULTIPLY self.__timeElapsed += dt if config.PRINT_FPS and dt > 0.001: print "FPS:", 1.0 / dt # ustaw kamerę self.__camera.set3d() # narysuj splash screen albo grę if self.__timeElapsed < config.SPLASH_DISPLAY_TIME: splash.update(dt) splash.draw() else: self.__world.update(dt) # self.__hud.update( dt ) # narysuj świat self.__camera.set3d() self.__world.draw() # # narysuj HUD # self.__camera.set2d() # self.__hud.draw() self.__window.flip()
class App(object): def __init__(self): vsync = config.IS_VSYNC if config.IS_FULLSCREEN: self.__window = window.Window( fullscreen=True, vsync=vsync ) else: width,height = config.WIN_SIZE self.__window = window.Window( width=width, height=height, fullscreen=False, vsync=vsync ) self.__winSize = winSize = ( self.__window.width, self.__window.height ) self.__camera = Camera( winSize ) self.__hud = HUD( winSize ) self.__inputManager = InputManager() self.__window.on_key_press = self.__inputManager.key_pressed self.__window.on_key_release = self.__inputManager.key_released if config.IS_FPS_LIMIT: clock.set_fps_limit( FPS_LIMIT ) glDepthFunc( GL_LEQUAL ) glEnable( GL_DEPTH_TEST ) self.__world = GameWorld(self) # to musi być na końcu def get_window_coords(self): ''' Zwraca współrzędne czworokąta, w którym można rysować. ''' return self.__camera.windowCoords def register_input_observer(self, obj): self.__inputManager.register_observer(obj) def get_window_dim(self): ''' Zwraca wymiary okna (szerokość, wysokość) ''' return (float(self.__window.width), float(self.__window.height)) def get_window_draw_dim(self): ''' Zwraca współrzędne, w których należy rysować (szerokość, wysokość). ''' return (1000.0, 750.0) # return (800.0, 600.0) def main_loop(self): ''' Pętla główna gry. ''' self.__timeElapsed = 0.0 # czas jaki upłynął od początku gry splash = Splash(self, self.__camera, config.SPLASH_DISPLAY_TIME) while not self.__window.has_exit: self.__window.dispatch_events() # update świata i HUDa dt = clock.tick() * config.DTIME_MULTIPLY self.__timeElapsed += dt if config.PRINT_FPS and dt>0.001: print "FPS:", 1.0/dt # ustaw kamerę self.__camera.set3d() # narysuj splash screen albo grę if self.__timeElapsed < config.SPLASH_DISPLAY_TIME: splash.update(dt) splash.draw() else: self.__world.update( dt ) # self.__hud.update( dt ) # narysuj świat self.__camera.set3d() self.__world.draw() # # narysuj HUD # self.__camera.set2d() # self.__hud.draw() self.__window.flip()