vendredi 18 février 2022

Random images changing at every frame

The falling object image, which I pick randomly under "necessary enemy values" and assign at "random image" will change every frame or so. I only want it to change when it has come up to Y=0 again. I have no idea why this is happening. Somebody please help!

import pygame, random

#initialise pygame
pygame.init()

#window variables
win = pygame.display.set_mode((384,256))
pygame.display.set_caption("Dungeon Run")

#necessary player values
x = 160
y = 128
width = 16
height = 16
vel = 8

#necessary enemy values
png1 = pygame.image.load("1.png")
png2 = pygame.image.load("2.png")
png3 = pygame.image.load("3.png")
e_types = [png1, png2, png3]
e_y = 0

#game loop (main code here)
run = True
while run:
    pygame.time.delay(64)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    
    keys = pygame.key.get_pressed()

    if keys[pygame.K_LEFT] and x > 10:
        x -= vel

    if keys[pygame.K_RIGHT] and x < 354:
        x += vel

    #gravity
    e_y = e_y + 8
    if e_y > 256:
        e_y = 0

    #random image
    e_type = random.choice(e_types)
    
    #window stuff
    win.fill((0, 0, 0))
    player = pygame.Rect(pygame.draw.rect(win, (255, 255, 255), (x, y, width, height)))
    win.blit(e_type, (0,e_y))
    pygame.display.update()
            
pygame.quit()



Aucun commentaire:

Enregistrer un commentaire