0

So here is my code, What I want to happen is that when I move left with the arrow key, the rectangle, which is named as variable z, changes into the image bg.png

if keys[pygame.K_LEFT]:
    x -= vel
    win.blit(z, bg (0,0))

Not sure how I would do it

import pygame
pygame.init()

win = pygame.display.set_mode((500,480))        
pygame.display.set_caption("a")
bg = pygame.image.load('bg.png')
x = 50
y = 50
vel = 5
height = 50
width = 40
def redrawGameWindow():
    win.blit(bg, (0,0))
    z = pygame.draw.rect(win, (255, 0, 0), (x, y, width, height ))
    # use variable as pygame z to change sprites
    pygame.display.update()

run = True
while run:
    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_LEFT]:
        x -= vel
        win.blit(z, bg (0,0))
    if keys[pygame.K_RIGHT]:
        x += vel
    if keys[pygame.K_UP]:
        y -= vel
    if keys[pygame.K_DOWN]:
        y += vel

    redrawGameWindow()
asdwd wdad
  • 65
  • 3
  • 7
  • The question is unclear. Please explain exactly what should happen. – skrx Jun 18 '18 at 20:52
  • @skrx What I want to happen is that when I move left with the arrow key, the rectangle, which is named as variable z, changes into the image bg. – asdwd wdad Jun 18 '18 at 20:59
  • Can you help me? – asdwd wdad Jun 18 '18 at 21:00
  • what change are you talking about, do you want to transform/flip the image or replace it ? – PRMoureu Jun 18 '18 at 21:13
  • @PRMoureu I want to use sprites, so when you go left, the rectangle turns into a image, a sprite. – asdwd wdad Jun 18 '18 at 21:14
  • Please try to fix your question, you're speaking *now* about some sprite, but your code contains only a background image and a rectangle (?) How the sprite should behave ? what is not working as you expect ? – PRMoureu Jun 18 '18 at 21:32
  • If I understand you correctly, you want to draw the red rect except if the player moves to the left, then you want to show the `bg` image instead of the rect. Is that correct? – skrx Jun 18 '18 at 22:05

0 Answers0