[英]Pygame drawing not showing in Pygame window
所以我正在測試 pygame,我想畫一個簡單的矩形。 當我運行代碼時沒有錯誤消息,但矩形沒有顯示在 window 中。我看到的是一個空白的白色 Pygame window 彈出窗口。 有誰知道為什么? 目前在我的 Mac 上使用 Python3 和 Pygame 1.9.4。 這是我的代碼,
import pygame
import pygame.font
pygame.init()
# Colours
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
GREEN = ( 0,255, 0)
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)
# Loop Switch
done = False
# Screen Update Speed (FPS)
clock = pygame.time.Clock()
# ------- Main Program Loop -------
while not done:
# --- Main Event Loop ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
screen.fill(GREEN)
pygame.display.flip()
#Setting FPS
clock.tick(60)
#Shutdown
pygame.quit()
你不想每 60 個刻度用綠色填充屏幕
要解決這個問題,只需將screen.fill(GREEN)
放在 Main 循環之外。
您唯一需要在 while 循環中使用screen.fill
時間是在您的程序中添加運動時。
我強烈建議你創建一個叫做 draw 的函數,並在你的 while 循環之外繪制東西。
我發現了問題:首先,Glitchd 是正確的,但您忘記更新:
import pygame
import pygame.font
pygame.init()
# Colours
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
GREEN = ( 0,255, 0)
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)
# Loop Switch
done = False
# Screen Update Speed (FPS)
clock = pygame.time.Clock()
# ------- Main Program Loop -------
while not done:
# --- Main Event Loop ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
screen.fill(GREEN)
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
pygame.display.flip()
pygame.display.update()
#Setting FPS
clock.tick(60)
#Shutdown
pygame.quit()
你正在畫一個形狀然后用綠色覆蓋它,交換
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
screen.fill(GREEN)
身邊的那兩個
問題是你繪制了形狀,然后用綠色“填充”(覆蓋)它,所以嘗試這樣的事情:
screen.fill(GREEN) #first fill the screen with green
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) #and after that draw the rectangle
該錯誤很明顯,因為您首先繪制一個形狀,然后用 color 覆蓋它。 您的代碼是正確的,但需要重新排列。
screen.fill("your color") # 首先你應該用顏色填充屏幕
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5) # 然后你應該畫任何形狀
先前給出的每個答案都未能正確說明為什么會出現此問題。 它與繪制、填充操作的順序無關; 這是關於你調用pygame.display.flip
函數的時機,或者很快:更新屏幕。 您的代碼繪制一個矩形,用綠色填充屏幕,然后更新屏幕。 它應該做的是繪制矩形,更新屏幕,然后用綠色填充屏幕。 這樣,在屏幕填充綠色之前繪制矩形后屏幕會更新,因此您可以看到它:
import pygame
import pygame.font
pygame.init()
# Colours
BLACK = ( 0, 0, 0)
WHITE = (255,255,255)
GREEN = ( 0,255, 0)
RED = (255, 0, 0)
BLUE = ( 0, 0,255)
# Dimensions of screen
size = (400,500)
WIDTH = 500
HEIGHT = 400
screen = pygame.display.set_mode(size)
# Loop Switch
done = False
# Screen Update Speed (FPS)
clock = pygame.time.Clock()
# ------- Main Program Loop -------
while not done:
# --- Main Event Loop ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
#In each case you draw the rectangle and then fill the screen with green
pygame.draw.rect(screen,(78,203,245),(0,0,250,500),5)
pygame.display.flip()
screen.fill(GREEN)
#Setting FPS
clock.tick(60)
#Shutdown
pygame.quit()
簡而言之,您應該在繪制矩形后更新。
您應該在while not done
循環中添加pygame.display.update()
。 pygame.display.update
更新屏幕。 你有這個問題是因為你畫了所有的圖但沒有更新屏幕。
您應該首先用綠色覆蓋屏幕,然后繪制您的形狀,否則它會被覆蓋。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.