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?
#5
Your code above, as written, does not run.
Error:
Traceback (most recent call last): File "spam.py", line 62, in <module> main() File "spam.py", line 59, in main makeSprite() File "spam.py", line 31, in makeSprite pygame.draw.rect(mainWindow, (white),(pixelx,pixely,pixel,pixel)) NameError: name 'white' is not defined
I've taken your code, and made a few modifications for performance and clarity reasons.
1) Instead of 0/1, I've defined the sprite as _/1, that way it's more clear what the image will look like when rendered (it's easier to edit right in the source if you can see it better). This could be improved further by using strings, so there's no space between the pixels. ie: instead of _,_,_,1,1,_,_,_,, you could have " 11 "
2) The makeSprite() function now generates a pygame Surface instead of drawing to the screen. Now, it should only be called once, when the sprite is spawned.
3) The x/y values are no longer globals, since they're not relevant to sprite spawning, so they've been moved to local variables of the main() function.
4) Sprite generation has been removed from the hot loop (what happens every frame). Instead, all we do is keep track of where it should be drawn, and then draw it.
5) I replaced pygame.time.delay() with a pygame.time.Clock(). delay() will delay your game by that much time, but Clock() will only delay by the number given less how long it took to render the frame. So if your computer starts slowing down (due to other things running, or if you did a lot of processing that frame), it'll delay less. That way there's a more consistent experience. ie: instead of always waiting 100ms between frames, it'll delay by however much it needs to in order to keep the framerate whatever you set it to.
6) With this layout, it should be fairly easily to pass different sprites to makeSprite(), to generate different kinds of aliens, in case you wanted to build more of invaders.
import math
import pygame
pygame.init()
 
mainWindow=pygame.display.set_mode((500,500))
black = (0, 0, 0)
white = (255,255,255)

#size of pixels in screen pixels
pixel = 10
# width of sprites, in pixels
width = 8
 
_ = None  # use underscore to try to make the image more visible
#binary data used to generate sprite
sprite = [
    _,_,_,1,1,_,_,_,
    _,_,1,1,1,1,_,_,
    _,1,1,1,1,1,1,_,
    1,1,_,1,1,_,1,1,
    1,1,1,1,1,1,1,1,
    _,_,1,_,_,1,_,_,
    _,1,_,1,1,_,1,_,
    1,_,1,_,_,1,_,1,
]
 
#function that renders sprite based on binary sequence
def makeSprite(sprite):
    surface = pygame.Surface((width * pixel, math.floor(len(sprite) / width) * pixel))
    surface.fill(black)
    for pos, value in enumerate(sprite):
        if value:
            pos_x = pos % width
            pos_y = math.floor(pos / width)
            surface.fill(white, (pos_x * pixel, pos_y * pixel, pixel, pixel))
    return surface

#main loop so that sprite can be moved using arrow keys
def main():
    #position of sprite
    x=20
    y=25
    surf = makeSprite(sprite)
    clock = pygame.time.Clock()

    while True: 
        for event in pygame.event.get():
            if event.type==pygame.QUIT:
                pygame.quit()
                return
        key=pygame.key.get_pressed()
        if key[pygame.K_UP]:
            y -= 1
        if key[pygame.K_DOWN]:
            y += 1
        if key[pygame.K_RIGHT]:
            x += 1
        if key[pygame.K_LEFT]:
            x -= 1
 
        mainWindow.fill((0,0,0))
        mainWindow.blit(surf, (x * pixel, y * pixel))
        pygame.display.flip()
        clock.tick(10) # target fps

if __name__ == "__main__":
    main()
Reply


Messages In This Thread
RE: I found a way to generate sprites without .blit. Is it effecient? - by nilamo - Dec-05-2019, 06:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  pygame, sprites, and rects menator01 12 2,278 Dec-07-2023, 02:37 AM
Last Post: Benixon
  [PyGame] Sprites just randomly appear and dissapear in my pygame.sprite.GoupeSingle trueShadoWrr 2 2,107 Feb-13-2023, 09:34 AM
Last Post: Vadanane
  [PyGame] pygame blit() method syafiq14 1 5,029 Oct-30-2020, 04:46 PM
Last Post: Russ_CW
  index error when using lists to blit rotated images codefun45 7 3,700 Sep-03-2020, 11:11 PM
Last Post: codefun45
  moving image with blit rwahdan 2 3,108 Jul-10-2019, 06:24 PM
Last Post: nilamo
  [PyGame] Having 4 players(Sprites) all being able to jump ElijahCastle 5 4,138 May-07-2019, 05:04 PM
Last Post: SheeppOSU
  Sprites and Actor error ajlconsulting 6 9,552 Jan-30-2019, 12:50 AM
Last Post: metulburr
  draw not showing all sprites ethanstrominger 0 2,679 Jan-25-2019, 10:10 PM
Last Post: ethanstrominger
  error with survace.blit joemcsk1 3 4,564 Aug-06-2018, 12:23 AM
Last Post: metulburr
  [PyGame] move randomly sprites reutB 4 8,366 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