Python pygame.Surface() Examples

The following are 30 code examples of pygame.Surface(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module pygame , or try the search function .
Example #1
Source File: polygon.py    From kvae with MIT License 8 votes vote down vote up
def __init__(self, dt=0.2, res=(32, 32), init_pos=(3, 3), init_std=0, wall=None):
        pygame.init()

        self.dt = dt
        self.res = res
        if os.environ.get('SDL_VIDEODRIVER', '') == 'dummy':
            pygame.display.set_mode(res, 0, 24)
            self.screen = pygame.Surface(res, pygame.SRCCOLORKEY, 24)
            pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, res[0], res[1]), 0)
        else:
            self.screen = pygame.display.set_mode(res, 0, 24)
        self.gravity = (0.0, 0.0)
        self.initial_position = init_pos
        self.initial_std = init_std
        self.space = pymunk.Space()
        self.space.gravity = self.gravity
        self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
        self.clock = pygame.time.Clock()
        self.wall = wall
        self.static_lines = None

        self.dd = 2 
Example #2
Source File: box.py    From kvae with MIT License 7 votes vote down vote up
def __init__(self, dt=0.2, res=(32, 32), init_pos=(3, 3), init_std=0, wall=None, gravity=(0.0, 0.0)):
        pygame.init()

        self.dt = dt
        self.res = res
        if os.environ.get('SDL_VIDEODRIVER', '') == 'dummy':
            pygame.display.set_mode(res, 0, 24)
            self.screen = pygame.Surface(res, pygame.SRCCOLORKEY, 24)
            pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, res[0], res[1]), 0)
        else:
            self.screen = pygame.display.set_mode(res, 0, 24)
        self.gravity = gravity
        self.initial_position = init_pos
        self.initial_std = init_std
        self.space = pymunk.Space()
        self.space.gravity = self.gravity
        self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
        self.clock = pygame.time.Clock()
        self.wall = wall
        self.static_lines = None

        self.dd = 2 
Example #3
Source File: no_rendering_mode.py    From scenario_runner with MIT License 7 votes vote down vote up
def __init__(self):
        def make_surface(tl):
            w = 40
            surface = pygame.Surface((w, 3 * w), pygame.SRCALPHA)
            surface.fill(COLOR_ALUMINIUM_5 if tl != 'h' else COLOR_ORANGE_2)
            if tl != 'h':
                hw = int(w / 2)
                off = COLOR_ALUMINIUM_4
                red = COLOR_SCARLET_RED_0
                yellow = COLOR_BUTTER_0
                green = COLOR_CHAMELEON_0
                pygame.draw.circle(surface, red if tl == tls.Red else off, (hw, hw), int(0.4 * w))
                pygame.draw.circle(surface, yellow if tl == tls.Yellow else off, (hw, w + hw), int(0.4 * w))
                pygame.draw.circle(surface, green if tl == tls.Green else off, (hw, 2 * w + hw), int(0.4 * w))
            return pygame.transform.smoothscale(surface, (15, 45) if tl != 'h' else (19, 49))
        self._original_surfaces = {
            'h': make_surface('h'),
            tls.Red: make_surface(tls.Red),
            tls.Yellow: make_surface(tls.Yellow),
            tls.Green: make_surface(tls.Green),
            tls.Off: make_surface(tls.Off),
            tls.Unknown: make_surface(tls.Unknown)
        }
        self.surfaces = dict(self._original_surfaces) 
Example #4
Source File: CHOICE.py    From Games with MIT License 6 votes vote down vote up
def __init__(self, position=(400, 150)):
		pygame.sprite.Sprite.__init__(self)
		self.img_1 = pygame.Surface((285, 100))
		self.img_1_front = pygame.Surface((281, 96))
		self.img_1.fill((255, 255, 255))
		self.img_1_front.fill((0, 0, 0))
		self.img_1.blit(self.img_1_front, (2, 2))
		self.img_2 = pygame.Surface((285, 100))
		self.img_2_front = pygame.Surface((281, 96))
		self.img_2.fill((255, 255, 255))
		self.img_2_front.fill((24, 196, 40))
		self.img_2.blit(self.img_2_front, (2, 2))
		self.text = 'easy'
		self.font = pygame.font.Font('./resource/fonts/m04.ttf', 42)
		self.textRender = self.font.render(self.text, 1, (255, 255, 255))
		self.img_1.blit(self.textRender, (60, 29))
		self.img_2.blit(self.textRender, (60, 29))
		self.image = self.img_1
		self.rect = self.image.get_rect()
		self.rect.center = position 
Example #5
Source File: CHOICE.py    From Games with MIT License 6 votes vote down vote up
def __init__(self, position=(400, 300)):
		pygame.sprite.Sprite.__init__(self)
		self.img_1 = pygame.Surface((285, 100))
		self.img_1_front = pygame.Surface((281, 96))
		self.img_1.fill((255, 255, 255))
		self.img_1_front.fill((0, 0, 0))
		self.img_1.blit(self.img_1_front, (2, 2))
		self.img_2 = pygame.Surface((285, 100))
		self.img_2_front = pygame.Surface((281, 96))
		self.img_2.fill((255, 255, 255))
		self.img_2_front.fill((24, 30, 196))
		self.img_2.blit(self.img_2_front, (2, 2))
		self.text = 'medium'
		self.font = pygame.font.Font('./resource/fonts/m04.ttf', 42)
		self.textRender = self.font.render(self.text, 1, (255, 255, 255))
		self.img_1.blit(self.textRender, (15, 29))
		self.img_2.blit(self.textRender, (15, 29))
		self.image = self.img_1
		self.rect = self.image.get_rect()
		self.rect.center = position 
Example #6
Source File: Game12.py    From Games with MIT License 6 votes vote down vote up
def loadLevel(self, game_level):
		with open(os.path.join(self.levels_path, game_level), 'r') as f:
			lines = f.readlines()
		# 游戏地图
		self.game_map = gameMap(max([len(line) for line in lines]) - 1, len(lines))
		# 游戏surface
		height = Config.get('block_size') * self.game_map.num_rows
		width = Config.get('block_size') * self.game_map.num_cols
		self.game_surface = pygame.Surface((width, height))
		self.game_surface.fill(Config.get('bg_color'))
		self.game_surface_blank = self.game_surface.copy()
		for row, elems in enumerate(lines):
			for col, elem in enumerate(elems):
				if elem == 'p':
					self.player = pusherSprite(col, row)
				elif elem == '*':
					self.game_map.addElement('wall', col, row)
				elif elem == '#':
					self.game_map.addElement('box', col, row)
				elif elem == 'o':
					self.game_map.addElement('target', col, row) 
Example #7
Source File: scene.py    From Games with MIT License 6 votes vote down vote up
def __init__(self, imagepath, position, size=(11, 13), is_highest=False, bg_color=None, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		# 导入图片
		self.images = []
		image = pygame.image.load(imagepath)
		for i in range(12):
			self.images.append(pygame.transform.scale(image.subsurface((i*20, 0), (20, 24)), size))
		if is_highest:
			self.image = pygame.Surface((size[0]*8, size[1]))
		else:
			self.image = pygame.Surface((size[0]*5, size[1]))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = position
		# 一些必要的变量
		self.is_highest = is_highest
		self.bg_color = bg_color
		self.score = '00000' 
Example #8
Source File: level.py    From QPong with Apache License 2.0 5 votes vote down vote up
def setup(self, scene, ball):
        """Setup a level with a certain level number"""
        scene.qubit_num = self.level
        self.circuit_grid_model = CircuitGridModel(scene.qubit_num, CIRCUIT_DEPTH)

        # the game crashes if the circuit is empty
        # initialize circuit with identity gate at the end of each line to prevent crash
        # identity gate are displayed by completely transparent PNG
        for i in range(scene.qubit_num):
            self.circuit_grid_model.set_node(i, CIRCUIT_DEPTH - 1, CircuitGridNode(node_types.IDEN))

        self.circuit = self.circuit_grid_model.compute_circuit()
        self.statevector_grid = StatevectorGrid(self.circuit, scene.qubit_num, 100)
        self.right_statevector = VBox(WIDTH_UNIT * 90, WIDTH_UNIT * 0, self.statevector_grid)
        self.circuit_grid = CircuitGrid(0, ball.screenheight, self.circuit_grid_model)

        # computer paddle

        self.left_paddle.image = pygame.Surface([WIDTH_UNIT, int(round(ball.screenheight / 2 ** scene.qubit_num))])
        self.left_paddle.image.fill((255, 255, 255))
        self.left_paddle.image.set_alpha(255)
        self.left_paddle.rect = self.left_paddle.image.get_rect()
        self.left_paddle.rect.x = 9 * WIDTH_UNIT

        # player paddle for detection of collision. It is invisible on the screen

        self.right_paddle.image = pygame.Surface([WIDTH_UNIT, int(round(ball.screenheight / 2 ** scene.qubit_num))])
        self.right_paddle.image.fill((255, 0, 255))
        self.right_paddle.image.set_alpha(0)
        self.right_paddle.rect = self.right_paddle.image.get_rect()
        self.right_paddle.rect.x = self.right_statevector.xpos 
Example #9
Source File: ball.py    From QPong with Apache License 2.0 5 votes vote down vote up
def __init__(self):
        super().__init__()

        # get ball screen dimensions
        self.screenheight = round(WINDOW_HEIGHT * 0.7)
        self.screenwidth = WINDOW_WIDTH
        self.width_unit = WIDTH_UNIT

        self.left_edge = self.width_unit
        self.right_edge = self.screenwidth - self.left_edge

        self.top_edge = self.width_unit * 0
        self.bottom_edge = self.screenheight - self.top_edge

        # define the ball sizes
        self.height = self.width_unit
        self.width = self.width_unit

        # create a pygame Surface with ball size
        self.image = pygame.Surface([self.height, self.width])

        self.image.fill(WHITE)

        self.rect = self.image.get_rect()

        self.x = 0
        self.y = 0
        self.speed = 0
        self.initial_speed_factor = 0.8
        self.direction = 0

        # initialize ball action type, measure and bounce flags
        self.ball_action = NOTHING
        self.measure_flag = NO

        # initialize ball reset on the left
        self.reset_position = LEFT
        self.reset()

        self.sound = Sound()
        self.score = Score() 
Example #10
Source File: statevector_grid.py    From QPong with Apache License 2.0 5 votes vote down vote up
def __init__(self, circuit, qubit_num, num_shots):
        pygame.sprite.Sprite.__init__(self)
        self.image = None
        self.rect = None
        self.ball = Ball()
        self.block_size = int(round(self.ball.screenheight / 2 ** qubit_num))
        self.basis_states = comp_basis_states(circuit.width())
        self.circuit = circuit

        self.paddle = pygame.Surface([WIDTH_UNIT, self.block_size])
        self.paddle.fill(WHITE)
        self.paddle.convert()

        self.paddle_before_measurement(circuit, qubit_num, num_shots) 
Example #11
Source File: statevector_grid.py    From QPong with Apache License 2.0 5 votes vote down vote up
def update(self):
        self.image = pygame.Surface([(self.circuit.width() + 1) * 3 * WIDTH_UNIT, self.ball.screenheight])
        self.image.convert()
        self.image.fill(BLACK)
        self.rect = self.image.get_rect() 
Example #12
Source File: circuit_grid.py    From QPong with Apache License 2.0 5 votes vote down vote up
def __init__(self, circuit_grid_model):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.Surface([GRID_WIDTH * (18 + 2),
                                     GRID_HEIGHT * (3 + 1)])
        self.image.convert()
        self.image.fill(WHITE)
        self.rect = self.image.get_rect()
        pygame.draw.rect(self.image, BLACK, self.rect, LINE_WIDTH)

        for wire_num in range(circuit_grid_model.max_wires):
            pygame.draw.line(self.image, BLACK,
                             (GRID_WIDTH * 0.5, (wire_num + 1) * GRID_HEIGHT),
                             (self.rect.width - (GRID_WIDTH * 0.5), (wire_num + 1) * GRID_HEIGHT),
                             LINE_WIDTH) 
Example #13
Source File: zengame.py    From ConvolutionalEmotion with MIT License 5 votes vote down vote up
def __init__(self,pos,image=pygame.Surface([24,24])):
        pygame.sprite.Sprite.__init__(self)
        if not isinstance(image, pygame.Surface):
            self.image = load_image(image)
        else:
            self.image = image
        self.rect = self.image.get_rect()
        self.position = pos
        self.rect.center = self.position 
Example #14
Source File: evolve_interactive.py    From neat-python with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def make_image_from_data(self, image_data):
        # For mono and grayscale, we need a palette because the evaluation function
        # only returns a single integer instead of an (R, G, B) tuple.
        if self.scheme == 'color':
            image = pygame.Surface((self.thumb_width, self.thumb_height))
        else:
            image = pygame.Surface((self.thumb_width, self.thumb_height), depth=8)
            palette = tuple([(i, i, i) for i in range(256)])
            image.set_palette(palette)

        for r, row in enumerate(image_data):
            for c, color in enumerate(row):
                image.set_at((r, c), color)

        return image 
Example #15
Source File: box_gravity.py    From kvae with MIT License 5 votes vote down vote up
def __init__(self, dt=0.2, res=(32, 32), init_pos=(3, 3), init_std=0, wall=None, gravity=(0.0, 0.0)):
        pygame.init()

        self.dt = dt
        self.res = res
        if os.environ.get('SDL_VIDEODRIVER', '') == 'dummy':
            pygame.display.set_mode(res, 0, 24)
            self.screen = pygame.Surface(res, pygame.SRCCOLORKEY, 24)
            pygame.draw.rect(self.screen, (0, 0, 0), (0, 0, res[0], res[1]), 0)
        else:
            self.screen = pygame.display.set_mode(res, 0, 24)
        self.gravity = gravity
        self.initial_position = init_pos
        self.initial_std = init_std
        self.space = pymunk.Space()
        self.space.gravity = self.gravity
        self.draw_options = pymunk.pygame_util.DrawOptions(self.screen)
        self.clock = pygame.time.Clock()
        self.wall = wall
        self.static_lines = None

        self.dd = 2 
Example #16
Source File: PAUSE.py    From Games with MIT License 5 votes vote down vote up
def update(self, screen):
		clock = pygame.time.Clock()
		background = pygame.Surface(screen.get_size())
		count = 0
		flag = True
		while True:
			count += 1
			clock.tick(60)
			self.components.clear(screen, background)
			self.components.update()
			if count % 10 == 0:
				count = 0
				flag = not flag
			if flag:
				self.components.draw(screen)
			else:
				screen.blit(self.PI.image, self.PI.rect)
			pygame.display.flip()
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					sys.exit(0)
					pygame.quit()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if event.button == 1:
						mouse_pos = pygame.mouse.get_pos()
						if self.RB.rect.collidepoint(mouse_pos):
							return True 
Example #17
Source File: END.py    From Games with MIT License 5 votes vote down vote up
def update(self, screen):
		clock = pygame.time.Clock()
		background = pygame.Surface(screen.get_size())
		count = 0
		flag = True
		while True:
			count += 1
			clock.tick(60)
			self.components.clear(screen, background)
			self.components.update()
			if count % 10 == 0:
				count = 0
				flag = not flag
			if flag:
				self.components.draw(screen)
			else:
				screen.blit(self.EI.image, self.EI.rect)
			pygame.display.flip()
			for event in pygame.event.get():
				if event.type == pygame.QUIT:
					sys.exit(0)
					pygame.quit()
				elif event.type == pygame.MOUSEBUTTONDOWN:
					if event.button == 1:
						mouse_pos = pygame.mouse.get_pos()
						if self.CB.rect.collidepoint(mouse_pos):
							return True 
Example #18
Source File: CHOICE.py    From Games with MIT License 5 votes vote down vote up
def __init__(self, position=(400, 475)):
		pygame.sprite.Sprite.__init__(self)
		self.ori_image = pygame.Surface((625, 200))
		self.ori_image.fill((255, 255, 255))
		self.ori_image_front = pygame.Surface((621, 196))
		self.ori_image_front.fill((0, 0, 0))
		self.ori_image.blit(self.ori_image_front, (2, 2))
		self.rect = self.ori_image.get_rect()
		self.rect.center = position 
Example #19
Source File: scenes.py    From Games with MIT License 5 votes vote down vote up
def __init__(self, position, imagepath, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface((24, 24))
		for i in range(2):
			for j in range(2):
				self.image.blit(pygame.image.load(imagepath), (12*i, 12*j))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = position 
Example #20
Source File: scenes.py    From Games with MIT License 5 votes vote down vote up
def __init__(self, position, imagepath, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface((24, 24))
		for i in range(2):
			for j in range(2):
				self.image.blit(pygame.image.load(imagepath), (12*i, 12*j))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = position 
Example #21
Source File: scenes.py    From Games with MIT License 5 votes vote down vote up
def __init__(self, position, imagepath, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface((24, 24))
		for i in range(2):
			for j in range(2):
				self.image.blit(pygame.image.load(imagepath), (12*i, 12*j))
		self.rect = self.image.get_rect()
		self.rect.left, self.rect.top = position 
Example #22
Source File: Game14.py    From Games with MIT License 5 votes vote down vote up
def showText(screen, font, is_clearance, flag=False):
	clock = pygame.time.Clock()
	msg = 'Game Over!' if not is_clearance else 'Congratulations, you won!'
	positions = [[235, 233], [65, 303], [170, 333]] if not is_clearance else [[145, 233], [65, 303], [170, 333]]
	surface = pygame.Surface((400, 200))
	surface.set_alpha(10)
	surface.fill((128, 128, 128))
	screen.blit(surface, (100, 200))
	texts = [font.render(msg, True, WHITE),
			 font.render('Press ENTER to continue or play again.', True, WHITE),
			 font.render('Press ESCAPE to quit.', True, WHITE)]
	while True:
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				sys.exit()
				pygame.quit()
			if event.type == pygame.KEYDOWN:
				if event.key == pygame.K_RETURN:
					if is_clearance:
						if not flag:
							return
						else:
							main(initialize())
					else:
						main(initialize())
				elif event.key == pygame.K_ESCAPE:
					sys.exit()
					pygame.quit()
		for idx, (text, position) in enumerate(zip(texts, positions)):
			screen.blit(text, position)
		pygame.display.flip()
		clock.tick(10) 
Example #23
Source File: Sprites.py    From Games with MIT License 5 votes vote down vote up
def __init__(self, x, y, width, height, color, bg_color, **kwargs):
		pygame.sprite.Sprite.__init__(self)
		self.image = pygame.Surface([width, height])
		self.image.fill(bg_color)
		self.image.set_colorkey(bg_color)
		pygame.draw.ellipse(self.image, color, [0, 0, width, height])
		self.rect = self.image.get_rect()
		self.rect.left = x
		self.rect.top = y 
Example #24
Source File: flappybird.py    From flappy-bird-pygame with MIT License 5 votes vote down vote up
def image(self):
        """Get a Surface containing this bird's image.

        This will decide whether to return an image where the bird's
        visible wing is pointing upward or where it is pointing downward
        based on pygame.time.get_ticks().  This will animate the flapping
        bird, even though pygame doesn't support animated GIFs.
        """
        if pygame.time.get_ticks() % 500 >= 250:
            return self._img_wingup
        else:
            return self._img_wingdown 
Example #25
Source File: views.py    From FreqShow with MIT License 5 votes vote down vote up
def __init__(self, model, controller):
		super(WaterfallSpectrogram, self).__init__(model, controller)
		self.color_func = gradient_func(freqshow.WATERFALL_GRAD)
		self.waterfall = pygame.Surface((model.width, model.height)) 
Example #26
Source File: road.py    From Reinforcement-Learning-for-Self-Driving-Cars with Apache License 2.0 5 votes vote down vote up
def draw_sky(self, frame):
        view = pygame.Surface((self.sky_width, self.sky_height))
        # Resize sky
        if frame % 40 == 0:
            self.sky_image = pygame.transform.scale(self.sky_image, (self.sky_image.get_size()[0]+2, self.sky_image.get_size()[1]+1))
            self.sky_image.blit(self.sky_image, ((-1, -2), (self.sky_width, self.sky_height)))
        view.blit(self.sky_image, ((0, 0), (self.sky_width, self.sky_height)))
        view.blit(self.hill_image, ((0, self.sky_height - 49), (self.sky_width, 49)))
        self.surface.blit(view, ((self.sky_x, self.sky_y), (self.sky_width, self.sky_height))) 
Example #27
Source File: road.py    From Reinforcement-Learning-for-Self-Driving-Cars with Apache License 2.0 5 votes vote down vote up
def draw_cars(self, subject_car):
        view = pygame.Surface((1010, ROAD_HEIGHT), pygame.SRCALPHA, 32)
        view = view.convert_alpha()
        camera_lane = subject_car.lane
        cars = subject_car.get_subjective_vision()
        for car in cars:
            lane = car[0]
            y = car[1]
            relative_y = y - 42
            if relative_y < 0:
                continue

            image = self.object_car_middle_image
            ratio = 231.0 / 328.0
            if lane != camera_lane:
                if lane > camera_lane:
                    image = self.object_car_right_image
                else:
                    image = self.object_car_left_image
            pt_top_left = (lane - 1) * 100.0 / 7 + 455
            pt_top_right = lane * 100.0 / 7 + 455
            pt_bottom_left = -337.0 * camera_lane + 673.33 + (lane - 1) * 337.0
            pt_bottom_right = -337.0 * camera_lane + 673.33 + lane * 337.0
            target_y = ROAD_HEIGHT * relative_y / 28.0
            target_bottom_left_x = pt_top_left + target_y / ROAD_HEIGHT * (pt_bottom_left - pt_top_left)
            target_bottom_right_x = pt_top_right + target_y / ROAD_HEIGHT * (pt_bottom_right - pt_top_right)
            image_width = int(target_bottom_right_x - target_bottom_left_x - 10)
            image_width = image_width if image_width > 0 else 0
            image_height = int(image_width * ratio)
            target_top_left_x = int(target_bottom_left_x + 5)
            target_top_left_y = int(target_y - image_height)
            a = pygame.transform.scale(image, (image_width, image_height))
            view.blit(a, (target_top_left_x, target_top_left_y))
        self.surface.blit(view, ((self.origin_x, self.origin_y), (self.width, self.height))) 
Example #28
Source File: road.py    From Reinforcement-Learning-for-Self-Driving-Cars with Apache License 2.0 5 votes vote down vote up
def draw_road(self, frame, lane=1):
        self.road_view = pygame.Surface((1010, ROAD_HEIGHT), pygame.SRCALPHA, 32)
        self.road_view = self.road_view.convert_alpha()

        pygame.draw.polygon(self.road_view, COLOR['black'],
                            [
                                (-337.0 * lane + 673.33, ROAD_HEIGHT),
                                (455, 0),
                                (555, 0),
                                (-336.67 * lane + 3032, ROAD_HEIGHT)
                            ])

        left = -337.0 * lane + 673.33
        for i in range(8):
            if i == 0 or i == 7:
                gfxdraw.filled_polygon(self.road_view, (
                    (int(int(i * 100.0 / 7 + 455)), 0),
                    (int(int(i * 100.0 / 7 + 455)) + 7, 0),
                    (int(left) + 7, int(ROAD_HEIGHT)),
                    (int(left), int(ROAD_HEIGHT))
                ), COLOR['white'])
            else:
                draw_dashed_line_delay(self.road_view,
                                 COLOR['white'],
                                 (i * 100.0 / 7 + 455, 0),
                                 (left, ROAD_HEIGHT),
                                 width=5,
                                 dash_length=40,
                                 delay=frame % 3)
            left += 337.0

        self.surface.blit(self.road_view, ((self.origin_x, self.origin_y), (self.width, self.height))) 
Example #29
Source File: display.py    From twitchslam with MIT License 5 votes vote down vote up
def __init__(self, W, H):
    pygame.init()
    self.screen = pygame.display.set_mode((W, H), DOUBLEBUF)
    self.surface = pygame.Surface(self.screen.get_size()).convert() 
Example #30
Source File: environment.py    From 2019-OSS-Summer-RL with MIT License 5 votes vote down vote up
def __init__(self, maze_name="Maze2D", maze_file_path=None,
                 maze_size=(30, 30), screen_size=(600, 600),
                 has_loops=False, num_portals=3, enable_render=True):
        pygame.init()
        pygame.display.set_caption(maze_name)
        self.clock = pygame.time.Clock()
        self.__game_over = False
        self.__enable_render = enable_render

        self.__maze = Maze(maze_cells=Maze.load_maze())

        self.maze_size = self.__maze.maze_size
        if self.__enable_render is True:
            self.screen = pygame.display.set_mode(screen_size)
            self.__screen_size = tuple(map(sum, zip(screen_size, (-1, -1))))

        self.__entrance = np.zeros(2, dtype=int)

        self.__goal = np.array(self.maze_size) - np.array((1, 1))

        self.__robot = self.entrance

        if self.__enable_render is True:
            self.background = pygame.Surface(self.screen.get_size()).convert()
            self.background.fill((255, 255, 255))

            self.maze_layer = pygame.Surface(self.screen.get_size()).convert_alpha()
            self.maze_layer.fill((0, 0, 0, 0,))

            self.__draw_maze()
            self.__draw_portals()
            self.__draw_robot()
            self.__draw_entrance()
            self.__draw_goal()