2014-07-16 151 views
0

我從一本名爲「絕對初學者的更多Python編程」的書中編寫了一款名爲餡餅遊戲的遊戲,但形狀不渲染,出現黑屏。PyGame - 渲染形狀

我希望它顯示4個數字,當你按下鍵盤上的1,2,3和4鍵時,餅圖/比薩/圓形麪包/任何出現的切片(1弧和2行)。

我看不出代碼有問題。

# The PieGame 
# 
# The last game for C02. Press keys 1, 2, 3 and 4 and the 
# pie pieces will change colour! :) 
# 
# The PieGame was programmed in PyGame 
# 
# Copyright by wait, who cares, this is just a really boring game 
# 
# ----------------------------------------------------------------- 

import pygame, sys, math, random 
from pygame.locals import * 

# +---+ SETUP +---+ 

pygame.init() 

SURFACE = pygame.display.set_mode((600, 500)) 
pygame.display.set_caption("The PieGame - Press keys 1, 2, 3, 4") 

FONT = pygame.font.Font(None, 60) 

# +---+ SETUP 2 +---+ 

BLACK = 0, 0, 0 
SILVER = 200, 200, 200 

RED = 220, 0, 0 
YELLOW = 0, 220, 220 
GREEN = 0, 220, 0 
BLUE = 0, 0, 220 
PINK = 220, 220, 0 

COLOUR = SILVER 

WIDTH = 4 
X = 300 
Y = 250 
RADIUS = 200 
POSITION = X-RADIUS, Y-RADIUS, RADIUS*2, RADIUS*2 

piece1 = False # state based programming 
piece2 = False 
piece3 = False 
piece4 = False 

# +---+ LOOP +---+ 

while True: 
    for event in pygame.event.get(): 
     if event.type == QUIT: 
      sys.exit() 
     elif event.type == KEYUP: 
      if event.key == K_ESCAPE: 
       sys.exit() 

      elif event.key == pygame.K_1: 
       piece1 = True 
      elif event.key == pygame.K_2: 
       piece1 = True 
      elif event.key == pygame.K_3: 
       piece1 = True 
      elif event.key == pygame.K_4: 
       piece1 = True 

    pygame.display.update() 

# +---+ MAIN CODE +---+ 

SURFACE.fill(BLACK) 

# draw numbers inside pie slices 
TEXT1 = FONT.render("1", True, COLOUR) 
screen.blit(TEXT1, (X+RADIUS/2-20, Y-RADIUS/2)) 

TEXT2 = FONT.render("2", True, COLOUR) 
screen.blit(TEXT2, (X+RADIUS/2, Y-RADIUS/2)) 

TEXT3 = FONT.render("3", True, COLOUR) 
screen.blit(TEXT3, (X+RADIUS/2, Y-RADIUS/2-20)) 

TEXT4 = FONT.render("1", True, COLOUR) 
screen.blit(TEXT4, (X+RADIUS/2-20, Y-RADIUS/2-20)) 

# if piece state flag is true, draw the piece 
if piece1: 
    start_angle = math.radians(0) 
    end_angle = math.radians(90) 
    pygame.draw.arc(SURFACE, COLOUR, POSITION, start_angle, end_angle, WIDTH) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X,Y-RADIUS), width) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X+RADIUS,Y), width) 

if piece2: 
    start_angle = math.radians(90) 
    end_angle = math.radians(180) 
    pygame.draw.arc(SURFACE, COLOUR, POSITION, start_angle, end_angle, WIDTH) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X,Y-RADIUS), width) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X-RADIUS,Y), width) 

if piece3: 
    start_angle = math.radians(180) 
    end_angle = math.radians(270) 
    pygame.draw.arc(SURFACE, COLOUR, POSITION, start_angle, end_angle, WIDTH) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X-RADIUS,Y), width) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X,Y+RADIUS), width) 

if piece4: 
    start_angle = math.radians(270) 
    end_angle = math.radians(360) 
    pygame.draw.arc(SURFACE, COLOUR, POSITION, start_angle, end_angle, WIDTH) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X,Y+RADIUS), width) 
    pygame.draw.line(SURFACE, COLOUR, (X, Y), (X+RADIUS,Y), width) 

# detect if the pie is finished 
if piece1 and piece2 and piece3 and piece4: 
    new_colour = random.randint(1,5) 
    if new_colour == 1: 
     COLOUR = RED 
    elif new_colour == 2: 
     COLOUR = YELLOW 
    elif new_colour == 3: 
     COLOUR = GREEN 
    elif new_colour == 4: 
     COLOUR = BLUE 
    elif new_colour == 5: 
     COLOUR = PURPLE 

pygame.display.update() 
+1

好主代碼不被循環調用後,檢測到鍵狀態的變化,所以形狀永遠不會被繪製 - 你確定縮進是正確的嗎? – gkusner

+0

從'#MAIN CODE'行添加註釋(如gkusner所述)到最後一行。 – furas

回答

0

首先,按鍵沒有效果的原因是因爲繪製弧的代碼不在主循環中。它必須縮進一個製表符空間;然而,這導致了兩個:

pygame.display.update() 

函數調用。您必須刪除第一個,因爲它應該在主循環的末尾找到。

其次,你沒有一個叫表面屏幕blit的文本上。在這裏你必須使用變量SURAFCE變量。

這些編輯使程序運行,但它可能沒有你想要的功能。這是一個鏈接到更新後的代碼的工作原理:http://pastebin.com/GFfP2hX4

+0

爲什麼這些數字非常接近?餡餅片也被繪製在相同的位置:| – PylonBuffering