Каждый раз, когда я прыгаю с моим персонажем, он говорит, что его хит, хотя он не имеет - PullRequest
0 голосов
/ 03 июня 2019

Я пытаюсь столкнуться с моей анимацией, и когда я прыгаю, он говорит, что попал в хитбокс, хотя у меня нет.

Вот ссылка на видео с проблемой: https://drive.google.com/file/d/1kHfdd2RPzU9IT9wfAss4QQXjlnwv40ut/view?usp=sharing

Я пытался изменить условия, в которых происходит столкновение, я пытался найти различные способы создания хитбоксов, таких как спрайт, но я не понимаю, как это сделать. Я довольно новичок в программировании, поэтому я не пытаюсь что-то еще попробовать.

from time import sleep as s
import random
import pygame
import time
from pygame import *
pygame.mixer.pre_init(44100,16,2,4096)
pygame.init()
pygame.mixer.music.load("Arcade Game Music Type Beat (Hip-HopRB Instrumental).mp3")
pygame.mixer.music.set_volume(0.7)
pygame.mixer.music.play(-1)

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))

jumpv1 = [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')]
jumpv2 = [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')]
jumpv3 = [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')]
jumpv4 = [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')]
jump1 = jumpv1+jumpv2+jumpv3+jumpv4

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')]

firstattack = [pygame.image.load('adventurer-attack3-00.png'),pygame.image.load('adventurer-attack3-01.png'),pygame.image.load('adventurer-attack3-02.png'),pygame.image.load('adventurer-attack3-03.png'),pygame.image.load('adventurer-attack3-04.png'),pygame.image.load('adventurer-attack3-05.png')]
secondattack = [pygame.image.load('adventurer-attack2-00.png'),pygame.image.load('adventurer-attack2-01.png'),pygame.image.load('adventurer-attack2-02.png'),pygame.image.load('adventurer-attack2-03.png'),pygame.image.load('adventurer-attack2-04.png'),pygame.image.load('adventurer-attack2-05.png')]
thirdattack = [pygame.image.load('adventurer-attack1-00.png'),pygame.image  .load('adventurer-attack1-01.png'),pygame.image.load('adventurer-attack1-02.png'),pygame.image.load('adventurer-attack1-03.png'), pygame.image.load('adventurer-attack1-04.png')]

falling = [pygame.image.load('adventurer-fall-00.png'), pygame.image.load('adventurer-fall-01.png')]                               

attack = firstattack+secondattack+ thirdattack


run = True
for i in range (20):
    jump1[i] = pygame.transform.scale(jump1[i],(90,90))
for i in range(3):
    run2[i] = pygame.transform.scale(run2[i],(90,90))
for i in range (4): 
    slide1[i] = pygame.transform.scale(slide1[i],(90,90))
for i in range (1):
    falling[i] = pygame.transform.scale(falling[i],(90,90))
for i in range(16):
    attack[i] = pygame.transform.scale(attack[i],(90,90))
imagex = 0
imagex2 = image.get_width()
clock = pygame.time.Clock()

img1 = pygame.image.load('imgg.png')
def text_objects(text, font):
    textSurface = font.render(text, True, (0,0,0))
    return textSurface, textSurface.get_rect()

def message_display(text):
    largeText = pygame.font.Font('freesansbold.ttf',115)
    TextSurf, TextRect = text_objects(text, largeText)
    TextRect.center = ((1000/2),(500/2))
    window.blit(TextSurf, TextRect)
    pygame.display.update()

def death():
    message_display('You DIED')
class obstacles(object):
    img = [pygame.image.load('imgg.png'),pygame.image.load('imgg.png')]
    def __init__(self, x,y, width1, height):
        self.x = x
        self.y =y
        self.width1 = width1
        self.height = height
        self.hitbox = (x,y,100,100)
        self.count = 0

    def draw(self, window):
        self.hitbox = (self.x+46, self.y, 45, 60)
        if self.count >=1:
            self.count = 0
        self.count +=1
        window.blit(pygame.transform.scale(self.img[self.count], (150,100)), (self.x, self.y-15)) 
        pygame.draw.rect(window, (255,0,0), self.hitbox, 2)

    def hit(self):
        print ("hit")

class obstacles2(object):
    walkleft = pygame.image.load('obstc2.png')
    def __init__(self, x,y, width2, height2):
        self.x = x
        self.y =y
        self.width2 = width2
        self.height2 = height2
        self.hitbox_enemy = (x,y,200,100)
        self.count1 = 0
    def draw(self, window):
        self.hitbox_enemy = (self.x, self.y, 65, 65)
        window.blit(pygame.transform.scale(self.walkleft, (65,65)), (self.x, self.y)) 
        pygame.draw.rect(window, (255,0,0), self.hitbox_enemy, 2)

class player(object):
    def __init__(self, x,y):
        self.vel = 0.5
        self.run1 = True
        self.jump2 = True
        self.slide2 = True
        self.attack1 = True
        self.isJump = False
        self. jumps = True
        self.falling1 = True
        self.player_x = 40
        self.player_y = 378
        self.x = 40
        self.y = 378
        self.width = 378
        self.speed = 0.6
        self.jumpcount = 10
        self.jumpcount1 = 0
        self.runcount = 0
        self.attack2 = 0
        self.slide = 0       
        self.fallspeed = 0.3
        self.falling2 = 0
        self.hitbox = (self.player_x,self.player_y,90,90)
        self.hitbox2 = (45,self.player_y+47,65,45)
        self.hitbox3 = (self.player_x+30,self.player_y,35,80)
        self.hitbox4 = (self.player_x+20,self.player_y+20,52,55)
        self.hitbox5 = (58,405,47,70)
        self.hitbox6 = (108, 405, 20, 50)


    def movement(self, window):
        pygame.time.delay(20)
        if self.runcount >= 3:
            self.runcount = 0

        if self.run1 == True:
            window.blit(run2[self.runcount],(int(self.player_x),int(self.player_y)))
            self.runcount +=1
            self.hitbox = (self.player_x+30,self.player_y+20,48,70)
            pygame.draw.rect(window,(255,0,0),self.hitbox, 2)

        if (keys[pygame.K_DOWN]) or ((keys[pygame.K_DOWN]) and keys[pygame.K_p]):
            if self.player_y == 378:
                self.run1 = False
                if self.slide >= 4:
                    self.slide = 0

                if self.slide2:  
                    window.blit(slide1[self.slide],(int(self.player_x),int(self.player_y)))
                    self.slide +=1
                    pygame.draw.rect(window,(255,0,0),self.hitbox2, 2)

        if event.type == pygame.KEYDOWN:
            if (event.key == pygame.K_DOWN )and self.player_y < self.width:

                self.run1 = False
                self.jump2 = False
                self.fallspeed += 0.2

                if self.falling2 >= 1:
                    self.falling2 = 0
                if self.falling1:
                    window.blit(falling[self.falling2], (int(self.player_x),int(self.player_y)))
                    self.hitbox3 = (self.player_x+30,self.player_y,35,80)
                    pygame.draw.rect(window,(255,0,0),self.hitbox3, 2)                    
                    self.falling2 +=1
        if keys[pygame.K_UP] and keys[pygame.K_p] :
            self.fallspeed = 0.3
            self.run1 = False
            self.jump2 = False
            self.slide2 = False
            if self.attack2 >= 16:
                self.attack2 = 0
            if self.attack1: 
                window.blit(attack[self.attack2],(int(self.player_x),int(self.player_y)))
                self.attack2 += 1
                self.hitbox5 = (self.player_x+30,self.player_y+20,38,70)
                self.hitbox6 = (self.player_x+72, self.player_y+20, 20, 50)
                pygame.draw.rect(window,(255,0,0),self.hitbox5, 2)
                pygame.draw.rect(window,(255,0,0),self.hitbox6, 2)
            if self.jumpcount1 >= 20:
                self.jumpcount1 = 0
            if self.jump2 and self.player_y < self.width:  
                window.blit(jump1[self.jumpcount1],(int(self.player_x),int(self.player_y)))
                self.hitbox4 = (int(self.player_x+20),int(self.player_y+20),52,55)
                pygame.draw.rect(window,(255,0,0),self.hitbox4, 2)
                self.jumpcount1 +=1
                self.fallspeed = 0.3

        if keys[pygame.K_UP]:
            self.fallspeed = 0.3
            self.run1 = False
            if self.jumpcount1 >= 20:
                self.jumpcount1 = 0

            if self.jump2 and self.player_y < self.width:  
                window.blit(jump1[self.jumpcount1],(int(self.player_x),int(self.player_y)))
                self.hitbox4 = (int(self.player_x+20),int(self.player_y+20),52,55)
                pygame.draw.rect(window,(255,0,0),self.hitbox4, 2)
                self.jumpcount1 +=1
                self.fallspeed = 0.3

        if keys[pygame.K_p] and not keys[pygame.K_UP]:
            self.run1 = False
            self.jump2 = False
            self.slide2 = False
            if self.attack2 >= 16:
                self.attack2 = 0

            if self.attack1:
                self.hitbox5 = (self.player_x+30,self.player_y+20,38,70)
                self.hitbox6 = (self.player_x+72, self.player_y+20, 20, 50)
                window.blit(attack[self.attack2],(int(self.player_x),int(self.player_y)))
                self.attack2 += 1
                pygame.draw.rect(window,(255,0,0),self.hitbox5, 2)
                pygame.draw.rect(window,(255,0,0),self.hitbox6, 2)
        if keys[pygame.K_DOWN] and keys[pygame.K_UP]:
            self.run1 = False

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_DOWN:
                self.run1 = True
                self.jump2 = True
                self.fallspeed = 0.3

            if event.key == pygame.K_UP:
                self.run1=True
            if event.key == pygame.K_p:
                self.run1 = True
                self.jump2 = True
                self.slide2 = True

class enemy(object):
    walkright = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')]
    walkleft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')]
    for i in range (10):
        walkright[i] = pygame.transform.scale(walkright[i],(70,70))
    for i in range(10):
        walkleft[i] = pygame.transform.scale(walkleft[i],(70,70))

    def __init__(self,enemy_x,enemy_y, end):
        self.x = enemy_x
        self.y = enemy_y             
        self.end = end

        self.path = [self.x,self.end]
        self.walkcount = 0
        self.vel = 7
        self.hitbox = (self.x,self.y-15,90,90)

    def draw_enemy(self,window):
        self.move()
        if self.walkcount+1 >= 33:
            self.walkcount = 0
        #walking right
        if self.vel > 0:
            window.blit(self.walkleft[self.walkcount//3], (self.x,self.y-15))
            self.walkcount += 1
        #walking left
        if self.vel < 0:
##            window.blit(self.walkleft[self.walkcount//3], (self.x,self.y))
            self.walkcount -= 1
        if self.x < -100:
            self.x = 1050
        self.hitbox = (self.x+25,self.y-15,41,70)
        pygame.draw.rect(window, (255,0,0),self.hitbox,2)

    def move(self):
        if self.vel > 0:
            #see if position + movement space is < the end, then it is able to move
            if self.x + self.vel < self.path[1]:
                self.x -= self.vel
            #past end point and turns 180
            else:
                self.vel = self.vel*-1
                self.walkcount = 0
        #see if position is smaller than starting position
        else:
            if self.x - self.vel > self.path[0]:
                #vel is already negative
                self.x += self.vel
            else:
               #Truns 180 agian
               self.vel = self.vel* -1
               self.walkcount = 0

objects = []
object2 = []
def keepdrawing():
    window.blit(image, (imagex,0))
    window.blit(image, (imagex2,0))
    for object1 in objects:
        object1.draw(window)
    for object3 in object2:

        object3.draw(window)
    adventurer.movement(window)
    attacker.draw_enemy(window)
    pygame.display.update()
attacker = enemy(950,407,951)
adventurer = player(40,387)
pygame.time.set_timer(pygame.USEREVENT+2,random.randint(2000, 3000))
while run2:
    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()
        keys = pygame.key.get_pressed()
        if adventurer.hitbox[0] > attacker.hitbox[0] and adventurer.hitbox[0]+30 < attacker.hitbox[0]+70:
            if  adventurer.hitbox[1] > attacker.hitbox[1] and adventurer.hitbox[1] < attacker.hitbox[1]+70:
                    window.fill((255,255,255)) 
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
        if  (keys[pygame.K_DOWN]) and adventurer.hitbox2[0] > attacker.hitbox[0] and adventurer.hitbox2[0] < attacker.hitbox[0]+70:
            if  adventurer.hitbox2[1] > attacker.hitbox[1] and adventurer.hitbox2[1]+70 > attacker.hitbox[1]:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
        if (keys[pygame.K_DOWN]) and adventurer.hitbox3[0]+70 < 391 and adventurer.hitbox3[0] > attacker.hitbox[0] and adventurer.hitbox3[0] < attacker.hitbox[0]+70:
            if  adventurer.hitbox3[1] > attacker.hitbox[1] and adventurer.hitbox3[1] < attacker.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")

        if  (keys[pygame.K_UP])and adventurer.hitbox4[0] > attacker.hitbox[0] and adventurer.hitbox4[0] < attacker.hitbox[0]+70:
            if  adventurer.hitbox4[1] > attacker.hitbox[1] and adventurer.hitbox4[1] < attacker.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
        if  (keys[pygame.K_UP])and adventurer.hitbox5[0] > attacker.hitbox[0] and adventurer.hitbox5[0] < attacker.hitbox[0]+70:
            if  adventurer.hitbox5[1] > attacker.hitbox[1] and adventurer.hitbox5[1] < attacker.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
        if  (keys[pygame.K_p] and (adventurer.hitbox6[0] > attacker.hitbox[0])) and adventurer.hitbox6[0] < attacker.hitbox[0]+70:
            if  adventurer.hitbox6[1] > attacker.hitbox[1] and adventurer.hitbox6[1] < attacker.hitbox[1]+50:
                    attacker.x = 1050 
        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                run = False
                exit()
            if event.type == pygame.USEREVENT+1:
                adventurer.speed += 1
            if event.type == pygame.USEREVENT+2:
                r = random.randint(0,1)
                if r==0:
                    new_obstacle2 = obstacles2(1050, 390, 64,64)
                    object2.append(new_obstacle2)
                if r==1:
                    new_obstacle = obstacles(1050,390,64,64)
                    objects.append(new_obstacle)
        for object3 in object2:
            if adventurer.hitbox[0] > object3.hitbox_enemy[0] and adventurer.hitbox[0] < object3.hitbox_enemy[0]+85:
                if  adventurer.hitbox[1] > object3.hitbox_enemy[1] and adventurer.hitbox[1] < object3.hitbox_enemy[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if  (keys[pygame.K_DOWN]) and adventurer.hitbox2[0] > object3.hitbox_enemy[0] and adventurer.hitbox2[0] < object3.hitbox_enemy[0]+85:
                if  adventurer.hitbox2[1] > object3.hitbox_enemy[1] and adventurer.hitbox2[1]+70 > object3.hitbox_enemy[1]:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if (keys[pygame.K_DOWN])and adventurer.hitbox3[0]+70 < 391 and adventurer.hitbox3[0] > object3.hitbox_enemy[0] and adventurer.hitbox3[0] < object3.hitbox_enemy[0]+85:
                if  adventurer.hitbox3[1] > object3.hitbox_enemy[1] and adventurer.hitbox3[1] < object3.hitbox_enemy[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")

            if  (keys[pygame.K_UP])and adventurer.hitbox4[0] > object3.hitbox_enemy[0] and adventurer.hitbox4[0] < object3.hitbox_enemy[0]+85:
                if  adventurer.hitbox4[1] > object3.hitbox_enemy[1] and adventurer.hitbox4[1] < object3.hitbox_enemy[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if  (keys[pygame.K_UP])and adventurer.hitbox5[0] > object3.hitbox_enemy[0] and adventurer.hitbox5[0] < object3.hitbox_enemy[0]+85:
                if  adventurer.hitbox5[1] > object3.hitbox_enemy[1] and adventurer.hitbox5[1] < object3.hitbox_enemy[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if  (keys[pygame.K_p] and (adventurer.hitbox6[0] > object3.hitbox_enemy[0])) and adventurer.hitbox6[0] < object3.hitbox_enemy[0]+86:
                if  adventurer.hitbox6[1] > object3.hitbox_enemy[1] and adventurer.hitbox6[1] < object3.hitbox_enemy[1]+50:
                    object2.pop(object2.index(object3))
            object3.x -=7
            if object3.x < -100:
                object2.pop(object2.index(object3))      
        for object1 in objects:
            if adventurer.hitbox[0] > object1.hitbox[0] and adventurer.hitbox[0] < object1.hitbox[0]+70:
                if  adventurer.hitbox[1] > object1.hitbox[1] and adventurer.hitbox[1] < object1.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if  (keys[pygame.K_DOWN]) and adventurer.hitbox2[0] > object1.hitbox[0] and adventurer.hitbox2[0] < object1.hitbox[0]+70:
                if  adventurer.hitbox2[1] > object1.hitbox[1] and adventurer.hitbox2[1]+70 > object1.hitbox[1]:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if (keys[pygame.K_DOWN])and adventurer.hitbox3[0]+70 < 391 and adventurer.hitbox3[0] > object1.hitbox[0] and adventurer.hitbox3[0] < object1.hitbox[0]+70:
                if  adventurer.hitbox3[1] > object1.hitbox[1] and adventurer.hitbox3[1] < object1.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")

            if  (keys[pygame.K_UP])and adventurer.hitbox4[0] > object1.hitbox[0] and adventurer.hitbox4[0] < object1.hitbox[0]+70:
                if  adventurer.hitbox4[1] > object1.hitbox[1] and adventurer.hitbox4[1] < object1.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if  (keys[pygame.K_UP])and adventurer.hitbox5[0] > object1.hitbox[0] and adventurer.hitbox5[0] < object1.hitbox[0]+70:
                if  adventurer.hitbox5[1] > object1.hitbox[1] and adventurer.hitbox5[1] < object1.hitbox[1]+50:
                    window.fill((255,255,255))
                    death()
                    pygame.display.update()
                    s(0.1)
                    print("hit")
            if  (keys[pygame.K_p] and (adventurer.hitbox6[0] > object1.hitbox[0])) and adventurer.hitbox6[0] < object1.hitbox[0]+70:
                if  adventurer.hitbox6[1] > object1.hitbox[1] and adventurer.hitbox6[1] < object1.hitbox[1]+50:
                    objects.pop(objects.index(object1))
            object1.x -= 7
            if object1.x < -100:
                objects.pop(objects.index(object1))



        if not(adventurer.isJump):

            if keys[pygame.K_UP]:
                adventurer.isJump =True

        else:

            if adventurer.jumpcount >= -10:

                neg=1
                if adventurer.player_y > adventurer.width:
                    adventurer.player_y = adventurer.width
                if adventurer.jumpcount <0:
                    neg = -1
    ##                s(0.01)
                adventurer.player_y -= (adventurer.jumpcount**2)*adventurer.fallspeed*neg*2
    ##            s(0.01)
                adventurer.jumpcount -=1

                if adventurer.player_y > adventurer.width:
                    adventurer.player_y=adventurer.width

            else:
                adventurer.isJump = False
                adventurer.jumpcount = 10
                if adventurer.player_y > adventurer.width:
                    player_y = adventurer.width
                    adventurer.fallspeed = 0.05
        keepdrawing()

Извините за длинный код, но все это необходимо, чтобы понять, как оно есть, и конечный результат должен состоять в том, что он должен показывать «ВЫ УМЕР» только тогда, когда ящики ударяются друг о друга, а не когда прыгаете сверху.

1 Ответ

0 голосов
/ 03 июня 2019

Похоже, это может быть виновником.

{if adventurer.hitbox [1]> attacker.hitbox [1] и adventurer.hitbox [1]

Вы сравниваете y-координату hitboz авантюриста с y-координатой злоумышленника, и это хорошо. Но затем вы проверяете, меньше ли координата у авантюриста на 70 единиц выше, чем у координатора злоумышленника. Итак, я думаю, что, возможно, ваш прыжок не будет достаточно высоким, чтобы учесть 70 единиц расстояния. Я не так уж и хорош с pyGame, но это был мой лучший шанс просто прочитать ваш код.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...