Dec-06-2019, 12:23 AM
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.