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?
#1
I lost interest in coding with pygame for a while after I realized that I would need some type of pixel art program for future projects. Planning it out, I quickly realized that such a program would be far more complex than anything I had made before. I would literally have to figure out how to do dozens of things just to make this one program, which was excessive.

However, while watching a video by the 8-bit guy (a youtuber that talks about 80s computers), I was inspired and realized that I could write an algorithm that could generate sprites WITHOUT having to use image files!

Here is the code I came up with:

import pygame
pygame.init()

mainWindow=pygame.display.set_mode((500,500))

#position of sprite
x=200
y=250
#size of pixels in screen pixels
pixel=10

#binary data used to generate sprite
row1=[0,0,0,1,1,0,0,0]
row2=[0,0,1,1,1,1,0,0]
row3=[0,1,1,1,1,1,1,0]
row4=[1,1,0,1,1,0,1,1]
row5=[1,1,1,1,1,1,1,1]
row6=[0,0,1,0,0,1,0,0]
row7=[0,1,0,1,1,0,1,0]
row8=[1,0,1,0,0,1,0,1]

sprite=row1+row2+row3+row4+row5+row6+row7+row8

#function that renders sprite based on binary sequence
def makeSprite():
    pixelx=x
    pixely=y

    for a in sprite:
        if a==1:
            pygame.draw.rect(mainWindow, (white),(pixelx,pixely,pixel,pixel))
        pixelx=pixelx+pixel
        if (pixelx-x)%80==0:
            pixelx=x
            pixely=pixely+pixel

#main loop so that sprite can be moved using arrow keys
def main():
    global x
    global y
    while True:
        pygame.time.delay(100)

        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                quit()
        key=pygame.key.get_pressed()
        if key[pygame.K_UP]:
            y=y-pixel
        if key[pygame.K_DOWN]:
            y=y+pixel
        if key[pygame.K_RIGHT]:
            x=x+pixel
        if key[pygame.K_LEFT]:
            x=x-pixel

        mainWindow.fill((0,0,0))
        makeSprite()
        pygame.display.update()

main()
What this program is supposed to do is generate a sprite that looks like one of the aliens from space invaders. You can move the sprite around the screen using the arrow keys.

Besides not needing a way to actually make proper image files, I see a number of advantages to this. For one, it would be really easy to make other algorithms that can flip or rotate the sprite, thus giving me more control over them. I also don't need to have a bunch of loose image files laying around (note that I have yet to figure out how to compile my programs). Also, it makes achieving transparency easier. Instead of telling the program to just ignore certain colors, with my algorithm it just skips over to the next pixel as it draws the sprite pixel by pixel, completely removing the need to have code in there to create transparency.

The only downside I can see to making sprites this way is that its kinda hard to figure out the binary sequence you need to type in to make a sprite. You'll notice that I implemented each row of the sprite as a separate list and had the program just combine them into a single list. I tried to type it out as one long list, but I quickly got lost and decided to do it this way hoping it would give me an idea of what the resulting sprite would look like, which it didn't sadly, but it still made it easier for me to keep track of which row I was typing in. Obviously, doing a larger sprite (such as a 16x16) would require a lot of typing, but for that I could just map out the thing on graph paper to make it easier for me to figure out the binary sequence I need to type in. Another small issue is that the color palette would have to be limited if I wanted to keep each entry in the list one digit (if I use both numbers and letters, I could in theory have 35 colors). Honestly, I don't mind this much. I find the number of colors available to me overwhelming and unnecessary. Also, I want to make retro-style games, so the limited color palette doesn't exactly bother me at all.

Of course, I don't know what effect this would have on how much processing power my games would require. I've noticed that my snake clone takes up an insane amount of my RAM, but that may just be because its having to interpret my code on the fly since I haven't figured out a way to actually compile anything.

Is there any other fundamental difference to generating sprites this way rather than through the .blit command? Either way, I'll probably be relying on it for a while until I get advanced enough where I can make my own ideal pixel art program.
Reply


Messages In This Thread
I found a way to generate sprites without .blit. Is it effecient? - by xBlackHeartx - Dec-04-2019, 04:32 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 2,219 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,533 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