How to use the pygame.Surface function in pygame

To help you get started, we’ve selected a few pygame examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github horstjens / ThePythonGameBook / pygame / template003_moving_sprites.py View on Github external
self.x = x
        self.y = y
        if color is None: # create random color if no color is given
            color = (random.randint(0,255), random.randint(0,255), random.randint(0,255))
        else:
            self.color = color
        if dx is None:
            self.dx = random.random() * 100 - 50 # from -50 to 50
        else:
            self.dx = dx
        if dy is None:
            self.dy = random.random() * 100 - 50
        else:
            self.dy = dy
        # create a rectangular surface for the ball 50x50
        self.image = pygame.Surface((self.width,self.height))    
        # pygame.draw.circle(Surface, color, pos, radius, width=0) # from pygame documentation
        pygame.draw.circle(self.image, color, (radius, radius), radius) # draw blue filled circle on ball surface
        # left blue eye
        pygame.draw.circle (self.image, (0,0,200) , (radius //2 , radius //2), radius// 3)
        # right yellow yey
        pygame.draw.circle (self.image, (255,255,0) , (3 * radius //2  , radius //2), radius// 3)
        # grey mouth
        pygame.draw.arc(self.image, (32,32,32), (radius //2, radius, radius, radius//2), math.pi, 2*math.pi, 1)
        # self.surface = self.surface.convert() # for faster blitting if no transparency is used. 
        # to avoid the black background, make black the transparent color:
        self.image.set_colorkey((0,0,0))
        self.image = self.image.convert_alpha() # faster blitting with transparent color
        #self.image=pygame.image.load("xx.png")
        self.rect= self.image.get_rect()
github renpy / pygame_sdl2 / old-tests / test_blit_matrix.py View on Github external
def case(src_alpha, dst_alpha, colorkey, alpha):

    if src_alpha:
        src = pygame.Surface((150, 150), pygame.SRCALPHA)
    else:
        src = pygame.Surface((150, 150), 0)

    if dst_alpha:
        dst = pygame.Surface((150, 150), pygame.SRCALPHA)
    else:
        dst = pygame.Surface((150, 150), 0)


    top_pattern(src)
    bottom_pattern(dst)

    if colorkey:
        src.set_colorkey((255, 0, 255, A))

    if alpha:
        src.set_alpha(128)

    dst.blit(src, (0, 0))


    return dst
github Mekire / pygame-raycasting-experiment / raycast.py View on Github external
def draw_rain(self, step, angle, left, ray_index):
        """
        Render a number of rain drops to add depth to our scene and mask
        roughness.
        """
        rain_drops = int(random.random()**3*ray_index)
        if rain_drops:
            rain = self.project(0.1, angle, step.distance)
            drop = pg.Surface((1,rain.height)).convert_alpha()
            drop.fill(RAIN_COLOR)
        for _ in range(rain_drops):
            self.screen.blit(drop, (left, random.random()*rain.top))
github eleurent / highway-env / highway / game.py View on Github external
vehicle = road.random_mdp_vehicle(25, ego=True)
    # road = Road.create_obstacles_road(4, 4.0)
    # vehicle = Vehicle([-20, road.get_lateral_position(0)], 0, 25, ego=True)
    # vehicle = ControlledVehicle.create_from(vehicle, road)
    road.vehicles.append(vehicle)

    t = 0
    done = False
    pause = False
    pygame.init()
    pygame.display.set_caption("Highway")
    clock = pygame.time.Clock()

    size = [SCREEN_WIDTH, SCREEN_HEIGHT]
    screen = pygame.display.set_mode(size)
    sim_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT/2))
    sim_surface = RoadSurface(sim_surface. get_size(), 0, sim_surface)
    value_surface = pygame.Surface((SCREEN_WIDTH, SCREEN_HEIGHT/2))

    while not done:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                done = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_SPACE:
                    pause = not pause
            vehicle.handle_event(event)

        if not pause:
            if t % (FPS//POLICY_FREQUENCY) == 0:
                mdp = RoadMDP(road, vehicle)
                smdp = SimplifiedMDP(mdp.state)
github Mekire / pygame-samples / four_direction_movement / four_dir_obstacles.py View on Github external
def make_image(self):
        """
        Let's not forget aesthetics.
        """
        fill_color = [random.randint(0, 255) for _ in range(3)]
        image = pg.Surface((50,50)).convert_alpha()
        image.fill(fill_color)
        image.blit(SHADE_MASK, (0,0))
        return image
github koduj-z-klasa / python101 / games_str / pong_str / pong_str3.py View on Github external
LT_BLUE = (230, 255, 255)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)

# szerokość, wysokość i pozycja paletek
PALETKA_SZER = 100
PALETKA_WYS = 20

# Inicjacja PALETEK:
# utworzenie powierzchni dla obrazka, wypełnienie jej kolorem,
# pobranie prostokątnego obszaru obrazka i ustawienie go na wstępnej pozycji

PALETKA_1_POZ = (350, 360)  # początkowa pozycja paletki gracza
paletka1_obr = pygame.Surface([PALETKA_SZER, PALETKA_WYS])
paletka1_obr.fill(BLUE)
paletka1_prost = paletka1_obr.get_rect()
paletka1_prost.x = PALETKA_1_POZ[0]
paletka1_prost.y = PALETKA_1_POZ[1]

PALETKA_2_POZ = (350, 20)  # początkowa pozycja paletki komputera
paletka2_obr = pygame.Surface([PALETKA_SZER, PALETKA_WYS])
paletka2_obr.fill(RED)
paletka2_prost = paletka2_obr.get_rect()
paletka2_prost.x = PALETKA_2_POZ[0]
paletka2_prost.y = PALETKA_2_POZ[1]

# szybkość paletki 1 (AI - ang. artificial inteligence, sztuczna
# inteligencja), czyli komputera
AI_PREDKOSC = 3
github horstjens / ThePythonGameBook / pygame / 016_layers.py View on Github external
self.dx = -100
                self.color = (0,0,255) # blue mountains, close
            elif self.type == 2:
                self._layer = -2
                self.color = (200,0,255) # pink mountains, middle
                self.dx = -75
            else:
                self._layer = -3
                self.dx = -35
                self.color = (255,0,0) # red mountains, far away
            self.groups = allgroup, mountaingroup
            pygame.sprite.Sprite.__init__(self, self.groups) # THE Line
            self.dy = 0
            x = 100 * self.type * 1.5
            y = screen.get_height() / 2 + 50 * (self.type -1)
            self.image = pygame.Surface((x,y))
            #self.image.fill((0,0,0)) # fill with black
            self.image.set_colorkey((0,0,0)) # black is transparent
            pygame.draw.polygon(self.image, self.color,
               ((0,y),
                (0,y-10*self.type), 
                (x/2, int(random.random()*y/2)),
                (x,y-10*self.type),
                (x,y),
                (9,y)),0) # width=0 fills the polygon
            self.image.convert_alpha()
            self.rect = self.image.get_rect()
            self.pos = [0.0,0.0]
            # start right side from visible screen
            self.pos[0] = screen.get_width()+self.rect.width/2
            self.pos[1] = screen.get_height()-self.rect.height/2
            self.rect.centerx = round(self.pos[0],0)
github aalex / toonloop / toonloop.py View on Github external
def _clear_onion_peal(self):
        """
        Sets all pixels in the onion peal as black.
        """
        blank_surface = pygame.Surface((self.config.image_width, self.config.image_height))
        texture_from_image(self.textures[self.TEXTURE_ONION], blank_surface)
github AGProjects / python-sipsimple / obsolete / desktopsharing.py View on Github external
winstyle = 0  # |FULLSCREEN
            if depth == 32:
                self.screen = pygame.display.set_mode(self.area.size, winstyle, 32)
            elif depth == 8:
                self.screen = pygame.display.set_mode(self.area.size, winstyle, 8)
                #default palette is perfect ;-)
                #~ pygame.display.set_palette([(x,x,x) for x in range(256)])
            #~ elif depth is None:
                #~ bestdepth = pygame.display.mode_ok((width, height), winstyle, 32)
                #~ print "bestdepth %r" % bestdepth
                #~ self.screen = pygame.display.set_mode(self.area.size, winstyle, best)
                #then communicate that to the protocol...
            else:
                #~ self.screen = pygame.display.set_mode(self.area.size, winstyle, depth)
                raise ValueError, "color depth not supported"
            self.background = pygame.Surface((self.width, self.height), depth)
            self.background.fill(0) #black
github horstjens / ThePythonGameBook / pygame / 003_static_blit_pretty_template.py View on Github external
def __init__(self, radius = 50, color=(0,0,255), x=320, y=240):
        """create a (black) surface and paint a blue ball on it"""
        self.radius = radius
        self.color = color
        self.x = x
        self.y = y
        self.dx = 0
        self.dy = 0
        # create a rectangular surface for the ball 50x50
        self.surface = pygame.Surface((2*self.radius,2*self.radius))    
        pygame.draw.circle(self.surface, color, (radius, radius), radius) # draw blue filled circle on ball surface
        self.surface = self.surface.convert() # for faster blitting. 
        # to avoid the black background, make black the transparent color: