PyGame - 渲染形状

时间:2014-07-16 17:41:09

标签: python pygame rendering shapes

我正在编写一款名为“馅饼游戏”的游戏来自一本名为“为绝对初学者创建更多Python编程”的书,但这些形状不会呈现,并且会出现黑屏。

我想要它显示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 个答案:

答案 0 :(得分:0)

首先,按键没有效果的原因是因为绘制弧的代码不在主循环中。它必须缩进一个标签空间;然而,这导致两个:

pygame.display.update() 

函数调用。您必须删除第一个,因为它应该在主循环结束时找到。

其次,你没有一个名为 screen 的表面到 blit 的文字。在这里,您必须使用 SURAFCE 变量。

这些编辑使程序运行,但它可能没有您想要的功能。以下是有效的更新代码的链接:http://pastebin.com/GFfP2hX4