Your code above, as written, does not run.
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
2) The
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
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.
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()