Reputation:
I got this code from a tutorial, but I added some stuff to make it into my own thing. A while back I was messing around and managed to make the controllable object an image. So I could move an image around the screen with WASD, but I changed it and forgot how to fix it now, and can't manage to blit
it onto the screen. It's just a rectangle which I can move around the screen. How do I make it an image again?
import pygame
pygame.init()
win = pygame.display.set_mode((500,500))
pygame.display.set_caption("Altoria")
mc_front = pygame.image.load('bernie front.jpg')
mc_front = pygame.transform.scale(mc_front, (100, 160))
block = pygame.image.load('block.png')
block = pygame.transform.scale(block,(100,60))
block_x = 100
block_y = 50
x = 50
y = 400
width = 40
height = 60
vel = 20
run = True
while run:
win.blit(block, [block_x, block_y])
pygame.display.update()
pygame.time.delay(100)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_a] and x > vel: # Making sure the top left position of our character is greater than our vel so we never move off the screen.
x -= vel
if keys[pygame.K_d] and x < 500 - vel - width: # Making sure the top right corner of our character is less than the screen width - its width
x += vel
if keys[pygame.K_w] and y > vel: # Same principles apply for the y coordinate
y -= vel
if keys[pygame.K_s] and y < 500 - height - vel:
y += vel
win.fill((0,0,0))
pygame.draw.rect(win, (250,0,0), (x, y, width, height))
pygame.display.update()
pygame.quit()
Upvotes: 2
Views: 828
Reputation: 981
In the end of your mainloop, you have it draw a rectangle, not an image. To blit the image onto the screen, the end of your mainloop would have to look like this:
win.fill((0,0,0)) #clear screen
win.blit(mc_front, (x, y)) #add player image
pygame.display.flip() #update the display
Upvotes: 1
Reputation: 211096
A pygame.Surface
(A
) can drawn on another Surface (B
) by B.blit(A, (x, y))
. e.g:
win.fill((0,0,0))
#pygame.draw.rect(win, (250,0,0), (x, y, width, height))
win.blit(mc_front, (x, y))
pygame.display.update()
Upvotes: 1