M
M
Maxim Siomin2020-06-22 15:07:18
Python
Maxim Siomin, 2020-06-22 15:07:18

Why does the character disappear?

I tried to remove everything from the game that does not relate to the problem, here is the code that comes out

import pygame as pg

class player(object):
    def __init__(self, x, y, width, heigth):
        self.x = x
        self.y = y
        self.width = width
        self.heigth = heigth
        self.spd = 2
        self.left = False
        self.right = True
        self.walkCount = 0
        self.isJump = False
        self.jumpCount = 10
        self.standing = True
        self.hitbox = (self.x + 17, (int(self.y) + 11), 29, 52)
        self.walkLeft = []
        self.walkRight = []
        self.createSpriteList('Left')
        self.createSpriteList('Right')
        self.health = 100
        self.total_damage = 0
        self.left_for_jumping = False
        self.right_for_jumping = False

        
    def draw(self, win):
        if self.walkCount + 1 >= 27:
            self.walkCount = 0

        if not self.standing:
            if self.left:
                win.blit(self.walkLeft[self. walkCount//3], (self.x, int(self.y)))
                self.walkCount += 1
        
            elif self.right:
                win.blit(self.walkRight[self.walkCount//3], (self.x, int(self.y)))
                self.walkCount += 1

        else:
            if self.left:
                win.blit(self.walkLeft[0], (self.x, int(self.y)))
                
            elif self.right:
                win.blit(self.walkRight[0], (self.x, int(self.y)))
                
        self.hitbox = (self.x + 17, (int(self.y) + 11), 29, 52)

    def hit(self):
        self.x = 50
        self.y = 475
        self.walkCount = 0
        self.jumpCount = 10
        self.isJump = False




def redraw():
    win.blit(bg, (0, 0))
    text = font.render('Damage dealed: ' + str(man.total_damage), 1, (0, 0, 0))
    win.blit(text, (10, 10))
    man.draw(win)
    goblin.draw(win)
    for bullet in bullets:
        bullet.draw(win) 
    
    pg.display.update()




def mainloop():
    global run
    global leftMouseButton
    global shot_time

    pg.mixer.music.play(-1)

    while run:
        clock.tick(fps)

        if man.hitbox[1] < goblin.hitbox[1] + goblin.hitbox[3] and man.hitbox[1] + man.hitbox[3] > goblin.hitbox[1]:
            if man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]:
                man.hit()
                man.health -= goblin.damage

        leftMouseButton = False
        for event in pg.event.get():
            if event.type == pg.QUIT:
                run = False
        
            if event.type == pg.MOUSEBUTTONDOWN:
                if event.button == 1:
                    leftMouseButton = True     
                    
        for bullet in bullets:
            if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius > goblin.hitbox[1]:
                if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius < goblin.hitbox[0] + goblin.hitbox[2]:
                    goblin.hit()
                    
                    bullets.pop(bullets.index(bullet))
            if bullet.x < 1600 and bullet.x > 0:
                bullet.x += bullet.spd
            else:
                bullets.pop(bullets.index(bullet))

        
        keys = pg.key.get_pressed()

        if leftMouseButton:
            time_now = 1
            leftMouseButton = False
            if man.left:
                facing = -1
            elif man.right:
                facing = 1
            
            if (time.time() - shot_time) > selectedWeapon.firing_speed:
                shot_time = time.time()
                bulletSound.play()
                bullets.append(projectile((man.x + man.width // 2), int(round(man.y)) + (man.heigth // 2), (6), (0, 0, 0), (facing)))
                        

                    
        
        if keys[pg.K_a] and man.x > man.spd:
            man.x -= man.spd
            man.left = True
            man.right = False
            man.standing = False

        elif keys[pg.K_d] and man.x < monitor.current_w - man.width - man.spd:
            man.x += man.spd
            man.right = True
            man.left = False
            man.standing = False

        else:
            man.standing = True
            man.walkCount = 0
 

        if keys[pg.K_SPACE] and not man.isJump:
            man.isJump = True
            man.left = False
            man.right = False
            man.walkCount = 0

        if man.isJump:
            if man.jumpCount >= -10:
                if man.jumpCount < 0:
                    neg = -1
                else:
                    neg = 1
                
                man.y -= (man.jumpCount ** 2) * 0.10 * neg
                man.jumpCount -= 1

            else:
                man.isJump = False
                man.jumpCount = 10


        redraw() 
        

win.fill((255, 255, 255))
pg.display.update()

mainloop()

pg.quit()


When I press the spacebar the character just disappears, if I press the right or left, the character reappears

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Siomin, 2020-06-22
@MaxSiominDev

I figured it out. everything is ok
In the mainloop, here in this place:

if keys[pg.K_SPACE] and not man.isJump:
            man.isJump = True
            man.left = False
            man.right = False
            man.walkCount = 0

I removed
man.left = False
man.right = False
man.walkCount = 0

and everything started working

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question