簡體   English   中英

如何停止繪制特定的矩形 pygame

[英]How to stop drawing a specific rectangle pygame

我的程序是 pygame 中的“鋼琴英雄”游戲,它的工作方式與吉他英雄相同,只是它用於計算機鍵盤並且基於彈奏鋼琴而不是吉他。 我在我的界面中使用了類似於 Synthesia 的設計,其中矩形歸結為“hitline”,您必須在正確的時間按下該鍵。

我的問題是,雖然矩形最初按預期繪制和工作,但它們似乎沒有更新,因此頂部永遠停止。 換句話說,歌曲中的每個音符都是無限長的。

我覺得這可能是錯誤所在,盡管我不是 100% 確定。

def Draw(self,hitLine):

    if self.coords[2][1]<hitLine:
        self.coords[0][1]+=2
        self.coords[1][1]+=2
        self.coords[2][1]+=2
        self.coords[3][1]+=2
    elif self.coords[2][1]>=hitLine and self.coords[0][1]<hitLine:
        self.coords[0][1]+=2
        self.coords[1][1]+=2
    else:
        self.drawing = False
    pygame.draw.polygon(screen,BLUE,self.coords,0)
    pygame.display.update()

這一行在一個 while 循環中,它一次一個地更新歌曲中的所有矩形。

for z in notes:
        if z.drawing:
            z.Draw(hitLine)

我發現你的問題很有趣,而且很有趣!

一些需要考慮的項目。

  1. 似乎沒有任何理由為您的 Note 對象使用“pygame 多邊形”,這些對象顯然是矩形。 在下面的代碼中,我使用了“pygame Rect”對象。

  2. 您的主循環不會每幀都清除屏幕。

在您的主循環中,您需要每幀清除屏幕。 在我的代碼中,我使用了 Rect 對象。 當它的頂部碰到hitLine時,Note 會停止自己繪制。

import pygame
pygame.init()

gameScreen = pygame.display.set_mode((1100, 692))

hitLine = 500

class Note:
    def __init__(self, rect):
        self.rect = rect
        self.drawing = True

    def draw(self):

        if self.rect.y < hitLine:
            self.rect.y += 2

        else:
            self.drawing = False;

        pygame.draw.rect(gameScreen, (0, 0, 255), self.rect, 0)

fNote = Note(pygame.Rect(500, -550, 80, 550))

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
            quit()

    gameScreen.fill((0, 0, 0))

    if fNote.drawing:
        fNote.draw()

    pygame.display.update()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM