亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何對沖突進行編程

如何對沖突進行編程

郎朗坤 2022-09-13 17:45:35
我想編寫沖突代碼。我有2個類,如果它們碰撞其中一個應該取消繪制1秒#Laden der Pygame Bibliothekimport pygameimport timeimport random#Initialisierung der Pygame Bibliothekpygame.init()# Spiel-Fenster erstellensize = [700, 500]screen = pygame.display.set_mode(size)screen.fill((255,255,255))# Noetig um die fps zu begrenzenclock = pygame.time.Clock()# Speichert ob das Spiel-Fenster geschlossen wurdedone = False生成只能向左和向右移動的對象的第一個類class Schlitten():    def __init__(self, px, py, pscreen):        self.FARBE1 = (139,87,66)        self.FARBE2 = (139,90,43)        self.braun = (104,73,71)        self.x = px        self.grau = (118,122,121)        self.y = py        self.red = (255,0,0)        self.screen = pscreen        self.hit = False        def draw(self):        if self.hit == False:            pygame.draw.rect(self.screen, self.FARBE2, [self.x,self.y,5,75])            pygame.draw.rect(self.screen, self.FARBE2, [self.x+29,self.y,5,75])            pygame.draw.rect(self.screen, self.braun, [self.x+5,self.y+20,24,3])            pygame.draw.rect(self.screen, self.braun, [self.x+5,self.y+55,24,3])            pygame.draw.rect(self.screen, self.FARBE1, [self.x+6,self.y+15,3,50])            pygame.draw.rect(self.screen, self.FARBE1, [self.x+12,self.y+15,3,50])            pygame.draw.rect(self.screen, self.FARBE1, [self.x+18,self.y+15,3,50])            pygame.draw.rect(self.screen, self.FARBE1, [self.x+24,self.y+15,3,50])            pygame.draw.rect(self.screen, self.grau, [self.x+5,self.y+10,24,2])    def kollision(self):        self.hit = True    def movemint(self):        keys = pygame.key.get_pressed()        if keys [pygame.K_LEFT] :            self.x -= 4        if keys [pygame.K_RIGHT] :            self.x += 4        if self.x < 0:            self.x += 4        if self.x > 665:            self.x -= 4    def left(self):        return self.x    def right(self):        return self.x+34    def up(self):        return self.y    def down(self):        return self.y+75
查看完整描述

1 回答

?
HUH函數

TA貢獻1836條經驗 獲得超4個贊

好的,首先是對這些問題的標準回答:如果你使用PyGame Sprite函數,在最初做一些額外的工作之后,你的程序將更容易編寫和維護。


要在任意對象上進行良好的碰撞,首先需要一個“邊界框”。這是一個圍繞您的對象的矩形。


查看施利滕/雪橇的代碼,我必須從各種繪圖坐標中計算出這一點(但我只是在做一個快速/粗略的工作)。它看起來像從 和 渲染擴展了另外 31 像素和 75 像素在 .您可能希望臨時添加一些代碼來繪制邊界框以進行檢查。Schlitten.xSchlitten.yxy


因此,要定義碰撞函數,我們需要一個 PyGame 矩形。


class Schlitten:

    def __init__( self ):

        self.x      = px

        self.y      = py        

        self.width  = 31

        self.height = 75

        self.rect   = pygame.Rect( px, py, self.width, self.height )

從代碼中可以看出,PyGame 矩形只需要坐標。需要隨著對象的移動而更新,但我們可以在碰撞測試之前執行此操作。我們添加了,因此我們的代碼不會到處都是無意義的數字。此外,如果雪橇的繪圖發生變化,則只需在一個地方調整這些數字。.rectself.widthself.height


無論如何,現在對于碰撞函數:


class Schlitten:

    ...


    def collideWith( self, other_rect ):

        """ Has the sleigh collided with the other object """

        self.rect.x = self.x              # update the rect position 

        self.rect.y = self.y

        collision   = self.rect.colliderect( other_rect )

        return collision

對類進行類似的更改 - 至少到代碼具有 .Baumbaum.rect


class Baum():

    def __init__( self, pos_x, pos_y, pscreen, pschlitten ):

        self.x      = pos_x

        self.y      = pos_y

        self.width  = 50

        self.height = 95

        self.rect   = pygame.Rect( pos_x, pos_y, self.width, self.height )


    def bewegung( self ):

        self.y      += 5

        self.rect.y += 5


    def spawn( self ):

        if self.y > 600:

            self.y      = -50

            self.x      = random.randrange(0,700)

            self.rect.x = x

            self.rect.y = y

然后,這允許代碼快速輕松地檢查沖突:


alles_baumen = [ Baum1, Baum2, Baum3 ]    # all tree objects


# main loop

while not exiting:

    ...


    for baum in alles_baumen:                       # for each tree

        baum.draw()

        baum.bewegung()

        baum.spawn()

        if ( speiler1.collideWith( baum.rect ) ):   # player hits tree?

            speiler1.kollision()                    # Oh-noes!

注: 樹繪圖功能將繪制樹,就好像坐標是左下角一樣。我所做的更改沒有考慮到這一點,因此要么更改繪圖代碼,要么更改 的位置以適應此負 y 布局。yBaum.rect


查看完整回答
反對 回復 2022-09-13
  • 1 回答
  • 0 關注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號