2017-12-02 168 views
-1

標題很自我解釋。我試圖找出如何使用Python的這個俄羅斯方塊遊戲的數組做一個矩形。如何使用數組製作一個矩形,以便我可以同時在屏幕上顯示多個?

下面的代碼:

screen = pygame.display.set_mode((400,800)) 

#Rectangle Variables 
x = 200 
y = 0 
width = 50 
height = 50 
thickness = 5 
speed = 1 
#Colors 
red = (255,0,0) 
white = (255,255, 255) 
green = (0,255,0) 
blue = (0,0,255) 
while(True): 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit(); sys.exit(); 
    #These lines^make the user able to exit out of the game window 
    y = y+1 
    pygame.draw.rect((screen) , red, (x,y,width,height), thickness) 
    pygame.display.update() 
+0

請隔離你有,而不是傾銷代碼塊的問題,並說如何做這個? – mickmackusa

+0

沒問題我剛換了它。 –

+0

你必須說明什麼是孤立的問題。我們不會爲您編寫新代碼,您會分離出一個問題/錯誤,並解決該問題。 – mickmackusa

回答

0

如果你想要做的就是添加矩形到一個數組,你可能只是這樣做:

rectangles = [] 
while(True): 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit(); sys.exit(); 
    #These lines^make the user able to exit out of the game window 
    y = y+1 
    rectangles.append(pygame.draw.rect((screen) , red, (x,y,width,height), thickness)) 
    pygame.display.update() 
+0

是的,我的維度和變量列在我現有的代碼中的其他地方。我想知道如何設置矩陣與矩陣,以便我可以一次創建多個, –

+0

您需要添加更多的代碼,然後顯示給我們。如果你在一個循環中迭代你設置的X Y線,你可以預先製作它們。 –

+0

我添加了我的變量 –

0

如果你有一個位置列表,然後使用for環路畫出來。

這裏的位置是在像素

# --- constants --- (UPPER_CASE_NAMES) 

WIDTH = 50 
HEIGHT = 50 
RED = (255,0,0) 

# --- main --- 

rectangles_XY = [ (0, 0), (50, 0), (100, 0) ] 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      # PLEASE, don't put all in one line 
      # it makes code less readable. 
      pygame.quit() 
      sys.exit() 

    for x, y in rectangles_XY: 
     pygame.draw.rect(screen, RED, (x, y, WIDTH, HEIGHT), 0) 

    pygame.display.update() 

這裏位置在小區現在的位置(列,行)

# --- constants --- (UPPER_CASE_NAMES) 

WIDTH = 50 
HEIGHT = 50 
RED = (255,0,0) 

# --- main --- 

rectangles = [ (0, 0), (1, 0), (2, 0) ] 

while True: 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      # PLEASE, don't put all in one line 
      # it makes code less readable. 
      pygame.quit() 
      sys.exit() 

    for column, row in rectangles: 
     x = column * WIDTH 
     y = row * HEIGHT 
     pygame.draw.rect(screen, RED, (x, y, WIDTH, HEIGHT), 0) 

    pygame.display.update() 
相關問題