1

When SPACE or DOWN is held, then the sprite disappears because of the scrolling background. When the background is a still image, it works. Furthermore the animation is fast so I used a timer to make it slower, but when the DOWN button is let go of, the screen speeds up.

I have tried searching up syntax online and it works when the background image is still.

from time import sleep as s
import random
import pygame
pygame.init()
import time

window = pygame.display.set_mode((1000, 500))
pygame.display.set_caption("Practice Game")
image = pygame.image.load('pixel6.png')
image = pygame.transform.scale(image, (1000, 500))

jump1 = [pygame.image.load('adventurer-jump-00.png'),pygame.image.load('adventurer-jump-01.png'),pygame.image.load('adventurer-jump-02.png'),pygame.image.load('adventurer-jump-03.png'), pygame.image.load('adventurer-smrslt-00.png'),pygame.image.load('adventurer-smrslt-01.png'),pygame.image.load('adventurer-smrslt-02.png'),pygame.image.load('adventurer-smrslt-03.png')]

run2 = [pygame.image.load('adventurer-run-00.png'), pygame.image.load('adventurer-run-01.png'),pygame.image.load('adventurer-run-02.png'),pygame.image.load('adventurer-run-03.png')]

slide1 = [pygame.image.load('adventurer-slide-00.png'),pygame.image.load('adventurer-slide-01.png'),pygame.image.load('adventurer-stand-00.png'),pygame.image.load('adventurer-stand-01.png'),pygame.image.load('adventurer-stand-02.png')]

#attack = [pygame.image.load('
imagex = 0
imagex2 = image.get_width()
clock = pygame.time.Clock()

run = True
run1 = True
jump2 = True
slide2 = True
imagess = True
x = 40
y = 391
FPS = 60
speed = 0.6
jumpcount = 10
jumpcount1 = 0
runcount = 0
slide = 0                                                                                                                                                             
isJump = False
down = False

class obstacles(object):
    img = [pygame.image.load('img.png')]

    def __init__(self, x,y, width, height):
        self.x = x
        self.y =y
        self.width = width
        self.height = height
        self.hitbox = (x,y,width,height)
        self.count = 0

    def draw(self, window):
        self.hitbox = (self.x + 5, self.y + 5, self.width, self.height)
        if self.count >= 8:
            self.count = 0
        self.count +=1
        window.blit(pygame.transform.scale(self.img[self.count//1000], (150,100)), (self.x, self.y)) 
        pygame.draw.rect(window, (255,0,0), self.hitbox, 2)

objects = []
def keepdrawing():
    global runcount, slide, run1,jumpcount1
    window.blit(image, (imagex,0))
    window.blit(image, (imagex2,0))
    for object1 in objects:
        object1.draw(window)

    if runcount >= 3:
        runcount = 0

    if run1 == True:
        window.blit(run2[runcount],(int(x),int(y)))
        runcount +=1

    if event.type == pygame.KEYDOWN:
        if event.key == pygame.K_DOWN and y == 391:
            run1 = False
            if slide >= 4:
                slide = 0

            if slide2:  
                window.blit(slide1[slide],(int(x),int(y)))
                slide +=1

        if event.key == pygame.K_SPACE:
            run1 = False
            if jumpcount1 >= 7:
                jumpcount1 = 0 
            if jump2 and y!=391:  
                window.blit(jump1[jumpcount1],(int(x),int(y)))
                jumpcount1 +=1

    if event.type == pygame.KEYUP:
        if event.key == pygame.K_DOWN:
            run1 = True

        if event.key == pygame.K_SPACE:
            run1=True


    pygame.display.update()

pygame.time.set_timer(pygame.USEREVENT+1, 500)
pygame.time.set_timer(pygame.USEREVENT+2, random.randrange(1000,2000))
obstacles = obstacles(1050,300,64,64)

while run:
    clock.tick(60)
    imagex -= 2
    imagex2 -= 2
    if imagex < image.get_width() * -1:
        imagex = image.get_width()
    if imagex2 < image.get_width() * -1:
        imagex2 = image.get_width()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
        if event.type == pygame.USEREVENT+1:
            speed += 1
        if event.type == pygame.USEREVENT+2:
            objects.append(obstacles)

    for object1 in objects:
        object1.x -= 1
        if object1.x < -100:
            objects.pop(objects.index(object1))

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

    if not(isJump):

        if keys[pygame.K_SPACE]:
            isJump =True

    else:
        if jumpcount >= -10:
            neg=1
            if jumpcount <0:
                neg = -1
                s(0.01)
            y-= (jumpcount**2)*0.3*neg
            s(0.01)
            jumpcount -=1

        else:
            isJump = False
            jumpcount = 10
    keepdrawing() 
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
  • Good chance that the "disappearing" you are observing is just the sprite being drawn before the background (which will put the background on top of the sprite, thus hiding it). I haven't paid close attention, so I could totally be wrong, but this line ```window.blit(run2[runcount],(int(x),int(y)))``` is being called conditionally in a place separate from your other blits. – Christian Reall-Fluharty May 23 '19 at 19:59

1 Answers1

0

I cannot test your game due to the lot of images I do not have, but I think you are misunderstanding how the event system works, or at least you have neglected a case.

When you press a key, the KEYDOWN is emitted, and when you lift the key, the KEYUP is emitted.
These event are instantaneous (well, not really, but they are very fast), and are catched by the event manager only when the status of the button changes (from pressed to unpressed and viceversa). You must not rely on them to check if a button is kept pressed. Have a look at this post for a better understanding.

With this in mind, let's see what happening to your keepdrawing function. Here the relevant parts:

def keepdrawing():
    #...
    if run1 == True:
        #blitting
    if event.type == pygame.KEYDOWN:
        run1 = False
        #stuff and blitting

    if event.type == pygame.KEYUP:
        run1 = True

As you can see, when you hold the button pressed (that is, between KEYDOWN and KEYUP events) the boolean run1 is False. So nothing is blit. That's why your image disappear.
You should still be able to see the beginning of the movement: the frame when you press the button the KEYDOWN event is catched and that part of the code is executed. But the frame later, nothing is blit because run1 is False and there is no event to be catched.

I do not have a working solution, as I said I cannot test the game, but I would suggest at least to be sure that keepdrawing always draw something. Try to figure what you should draw when run1 is False and add a couple of lines about it in keepdrawing.

As a more general advice, do not use the pygame event system in the keepdrawing function. Just check if a button is pressed like you do here:

keys = pygame.key.get_pressed()
Valentino
  • 7,291
  • 6
  • 18
  • 34