第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

為什么我的程序沒(méi)有顯示我告訴它的命中框?

為什么我的程序沒(méi)有顯示我告訴它的命中框?

楊__羊羊 2022-05-24 16:35:33
我正在嘗試為我的 pygame 游戲中的每個(gè)對(duì)象繪制一個(gè)碰撞箱,但碰撞箱沒(méi)有顯示。這是我定義hitbox和敵人其他方面的類。class Enemy:    def __init__(self, y, width, height):        self.width = width        self.height = height        self.vel = 1.5        self.y = y        self.x = random.randrange(screen_width - 64 * 2)        self.index = random.choice(number)        self.hitboxes = [(self.x + 68, self.y + 68, self.width - 10, self.height - 14),                         (self.x + 38, self.y + 47, self.width + 20, self.height - 5),                         (self.x + 18, self.y + 12, self.width + 32, self.height + 30),                         (self.x + 20, self.y + 32, self.width + 16, self.height + 5),                         (self.x + 4, self.y + 7, self.width - 24, self.height - 31)]  # hitbox list        self.hitbox = self.hitboxes[self.index]  # selecting hitbox from list      def draw(self, win):        win.blit(asteroids[self.index], (self.x, self.y))        pygame.draw.rect(win, (255, 0, 0), self.hitbox, 2)  # trying to draw the hitbox 這是re_draw功能def re_draw():    win.fill((0, 0, 0))    [...]    for a in asteroids_on_screen:        a.draw(win)  # this is where I draw the object     [...]    pygame.display.update()這是主循環(huán)和與我想在其上顯示 hitbox 的敵人/對(duì)象關(guān)聯(lián)的變量。asteroids = [pygame.image.load('rock0.png'), pygame.image.load('rock1.png'), pygame.image.load('rock2.png'),             pygame.image.load('rock3.png'), pygame.image.load('rock4.png')]number = [0, 1, 2, 3, 4]asteroids_on_screen = []rock = Enemy(-140, 64, 64)[...]run = Truewhile run:    last = pygame.time.get_ticks()    for event in pygame.event.get():        if event.type == pygame.QUIT:            run = False        elif event.type == my_event_id:            x = random.randrange(screen_width - 64 * 2)            index = random.choice(number)            asteroids_on_screen.append(Enemy(rock.y, rock.width, rock.height))
查看完整描述

1 回答

?
一只甜甜圈

TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超5個(gè)贊

碰撞箱不會(huì)隨著小行星“移動(dòng)”。如果你想讓這種方法起作用,那么當(dāng)小行星的位置改變時(shí),你必須改變 hitbox 的 y 坐標(biāo)。例如:


for a in asteroids_on_screen:

    if -141 < a.y < 500:

        a.y += a.vel

        a.hitbox = (a.hitbox[0], a.hitbox[1]+a.vel, a.hitbox[2], a.hitbox[3])

    else:

        asteroids_on_screen.pop(asteroids_on_screen.index(a)) 

但我建議在draw. 例如創(chuàng)建一個(gè)屬性 currenthitbox,它返回當(dāng)前的命中框:


class Enemy:

    def __init__(self, y, width, height):

        self.width = width

        self.height = height

        self.vel = 1.5

        self.y = y

        self.x = random.randrange(screen_width - 64 * 2)

        self.index = random.choice(number)

        self.hitboxes = [(68, 68, - 10, - 14),

                         (38, 47, + 20, - 5),

                         (18, 12, + 32, + 30),

                         (20, 32, + 16, + 5),

                         (4,  7,  - 24, - 31)] 

        self.hitbox = self.hitboxes[self.index] 


    @property

    def currenthitbox(self):

        return (self.hitbox[0]+self.x, self.hitbox[1]+self.y, self.hitbox[2]+self.width, self.hitbox[3]+self.height)


    def draw(self, win):

        win.blit(asteroids[self.index], (self.x, self.y))

        pygame.draw.rect(win, (255, 0, 0), self.currenthitbox, 2) 


查看完整回答
反對(duì) 回復(fù) 2022-05-24
  • 1 回答
  • 0 關(guān)注
  • 134 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)