0
我正在努力在pygame的屏幕上移動一個繪製的矩形,我試圖創建一個Snake遊戲。我對Python和麪向對象編程一般都很陌生,所以這可能是一個愚蠢的錯誤。下面的代碼。Python PyGame移動矩形
#X coordinate of snake
lead_x = 300
#sets window position on screen
import os
os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (320,240)
#imports pygame module
import sys, pygame
#initialises pygame module
pygame.init()
#changes background colour to green
background_colour = 155, 188, 15
blue =(0,0,255)
red = (100,40,20)
#sets screen size
screen = pygame.display.set_mode((640, 480))
#changes the background colour
screen.fill(background_colour)
#creates rectangle on the screen
pygame.draw.rect(screen, blue, [lead_x,lead_x,10,10])
#updates display to show new background colour
pygame.display.update()
#Sets the window title to 'Python'
pygame.display.set_caption('Python')
#closes the window when user presses X
running = True
#if cross is clicked set running = False
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
#Controls
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
lead_x -= 10
if event.key == pygame.K_RIGHT:
lead_x += 10
#if running = False close pygame window
if running == False:
pygame.quit()
感謝您的幫助,我會嘗試這些事情,看看文檔。 – BoJ
仍然有麻煩移動繪製矩形運行循環,並把這兩件事情,但現在我得到的是一個沒有矩形的空白屏幕。 – BoJ
'pygame.draw.rect(screen,blue,[lead_x,lead_y,10,10])'''後面加了'screen.fill(background_colour)'?只要翻轉他們的立場,它應該工作。在繪製任何東西之前,您通常需要'screen.fill(background_colour)'。 –