Python Forum
[PyGame] I found a way to generate sprites without .blit. Is it effecient?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyGame] I found a way to generate sprites without .blit. Is it effecient?
#9
You can also use strings.
import pygame

def create_sprite(color, pixel_size, data):
    size = len(data[0]) * pixel_size, len(data) * pixel_size
    surface = pygame.Surface(size, pygame.SRCALPHA)
    surface.fill((0, 0, 0, 0))
    for y, row in enumerate(data):
        for x, value in enumerate(row):
            if value == "x":
                surface.fill(color, (x * pixel_size, y * pixel_size, pixel_size, pixel_size))

    return surface

def main():
    pygame.init()
    # basic pygame setup
    pygame.display.set_caption("Sprite Creation Example")
    surface = pygame.display.set_mode((500, 500))
    rect = surface.get_rect()
    clock = pygame.time.Clock()
    delta = 0
    fps = 60

    squid = create_sprite(pygame.Color("white"), 10,
        ("   xx   ",
         "  xxxx  ",
         " xxxxxx ",
         "xx xx xx",
         "xxxxxxxx",
         "  x  x  ",
         " x xx x ",
         "x x  x x"))

    squid_rect = squid.get_rect(center=rect.center)
    squid_vector = pygame.Vector2(squid_rect.topleft)
    speed = 0.05

    # main loop
    running = True
    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False

        key = pygame.key.get_pressed()
        if key[pygame.K_UP]:
            squid_vector.y -= speed * delta
        if key[pygame.K_DOWN]:
            squid_vector.y += speed * delta
        if key[pygame.K_RIGHT]:
            squid_vector.x += speed * delta
        if key[pygame.K_LEFT]:
            squid_vector.x -= speed * delta

        squid_rect.topleft = squid_vector

        # Draw
        surface.fill(pygame.Color('black'))
        surface.blit(squid, squid_rect)
        pygame.display.flip()
        delta = clock.tick(fps)

if __name__ == "__main__":
    main()
99 percent of computer problems exists between chair and keyboard.
Reply


Messages In This Thread
RE: I found a way to generate sprites without .blit. Is it effecient? - by Windspar - Dec-06-2019, 12:23 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 2,218 Dec-07-2023, 02:37 AM
Last Post: Benixon
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 2,074 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] pygame blit() method syafiq14 1 5,002 Oct-30-2020, 04:46 PM
Last Post: Russ_CW
  index error when using lists to blit rotated images codefun45 7 3,673 Sep-03-2020, 11:11 PM
Last Post: codefun45
  moving image with blit rwahdan 2 3,086 Jul-10-2019, 06:24 PM
Last Post: nilamo
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,109 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,502 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 2,658 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  error with survace.blit joemcsk1 3 4,532 Aug-06-2018, 12:23 AM
Last Post: metulburr
  [PyGame] move randomly sprites reutB 4 8,341 Mar-29-2017, 01:12 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020