Python PyGame使用for循环绘制矩形

时间:2014-12-23 22:02:47

标签: python pygame

我是PyGame的新手,我正在学习使用Python和PyGame开始游戏开发这本书。有一个例子(清单4-9),作者说一个脚本将在PyGame屏幕上绘制十个随机放置的,随机颜色的矩形。以下是本书的代码:

import pygame 
from pygame.locals import *
from sys import exit
from random import *

pygame.init()

screen = pygame.display.set_mode((640, 480), 0,32)
while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    screen.lock()
    for count in range(10):
        random_color = (randint(0,255), randint(0,255), randint(0,255))
        random_pos = (randint(0,639), randint(0,479))
        random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))
        pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))
    screen.unlock()
    pygame.display.update()

当我这样做时(这是我期望在逻辑上发生的事情)发生的事情是它绘制了无限多个矩形。它只是继续执行for循环,因为while循环始终为True。我在网上搜索过这个,我尝试移动显示更新,但那些东西不起作用。这让我抓狂!

谢谢!

4 个答案:

答案 0 :(得分:2)

看起来你已经知道它为什么用矩形绘制无限。

我想你想绘制10个随机大小,pos和颜色的随机矩形。

然后你可以这样做:

import pygame 
from pygame.locals import *
from sys import exit
from random import *

pygame.init()

screen = pygame.display.set_mode((640, 480), 0,32)

class Rectangle:
    def __init__(self, pos, color, size):
        self.pos = pos
        self.color = color
        self.size = size
    def draw(self):
        pygame.draw.rect(screen, self.color, Rect(self.pos, self.size))

rectangles = []     

for count in range(10):
    random_color = (randint(0,255), randint(0,255), randint(0,255))
    random_pos = (randint(0,639), randint(0,479))
    random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))

    rectangles.append(Rectangle(random_pos, random_color, random_size))



while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    screen.lock()
    for rectangle in rectangles:
        rectangle.draw()
    screen.unlock()
    pygame.display.update()

答案 1 :(得分:1)

代码确实无限运行。 但是,内部循环确实会创建10个矩形。 因此从技术上讲,脚本会绘制十个随机放置的随机彩色矩形。 它只是做了无数次。

注意:这是绘制十个随机放置的随机颜色矩形的部分。

for count in range(10):
        random_color = (randint(0,255), randint(0,255), randint(0,255))
        random_pos = (randint(0,639), randint(0,479))
        random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))
        pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))

答案 2 :(得分:0)

它将绘制许多矩形,并且它是无穷无尽的。它没有绘制10次,因为没有参数说除了-sys exit-之外打破循环。所以你必须为此定义一个变量。

  

通常 event 语句中应该有 if 语句,这会将的布尔值更改为 F em>它将停止循环。

     

您只需将代码更改为:

即可
import pygame 
from pygame.locals import *
from sys import exit
from random import *

pygame.init()

screen = pygame.display.set_mode((640, 480), 0,32)
count=0 #new variable for stop the loop
while count<10: #new limit
    for event in pygame.event.get():
        if event.type == QUIT:
            exit()
    screen.lock()

    random_color = (randint(0,255), randint(0,255), randint(0,255))
    random_pos = (randint(0,639), randint(0,479))
    random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))
    pygame.draw.rect(screen, random_color, Rect(random_pos, random_size))
    count+=1 #variable updating after drawing each rectangle

    screen.unlock()
    pygame.display.update()

它可能是我不知道的书中的错误拼写,但是这个循环是无限的,所以你可以改成它:)

答案 3 :(得分:0)

是的,这是一个写得不好的例子,但并没有“破坏”。到目前为止,每个人都忽略了这一点:

for event in pygame.event.get():
    if event.type == QUIT:
        exit()

PyGame包含处理事件的概念。第一行获取您的游戏可以访问的所有事件,并查看每个事件。如果其中一个是“QUIT”事件,则会直接调用exit()(通常称为sys.exit(0)),这会立即结束应用程序。

退出应用程序时会生成“QUIT”类型事件,例如单击红色X.可能还有其他方法可以结束应用程序,这也会生成此事件,但我不确定它们是什么。还有一些方法可以结束不会生成此事件的应用程序。所以,是的,如果你永远不会退出应用程序,那么它将永远运行。

编写此功能的更好方法如下:

done = False
while not done:
    for event in pygame.event.get():
        if event.type == QUIT: # or other types of events
            done = True
    //do other game logic and drawing stuff after this
    pygame.display.update()

这确保循环处理一致,并且出口点始终位于相同的位置:循环的顶部。如果只有一个地方可以退出循环,那么您可以更轻松地了解有多少代码正在运行(所有代码)。它还允许您使用一些更复杂的方法来决定何时以及是否结束循环。

然而,即使这将是一个非常令人困惑的“例子”因为(就像你和@ user3679917所说的那样)似乎做了一些不同于描述的事情。 似乎就像它只是永远地绘制随机矩形一样。让我们看看我们是否可以让它更具可预测性......

//pre-roll the random numbers before the while loop
random_attributes = []
for count in range(10):
    random_color = (randint(0,255), randint(0,255), randint(0,255))
    random_pos = (randint(0,639), randint(0,479))
    random_size = (639-randint(random_pos[0], 639), 479-randint(random_pos[1],479))
    random_attributes.append(random_color, random_pos, random_size)

//then when you draw, just draw what you already rolled each time, without rolling new stats
done = False
while not done:
    for event in pygame.event.get():
        if event.type == QUIT: # or other types of events
            done = True
    //do other game logic and drawing stuff after this
    for color, position, size in random_attributes:
        pygame.draw.rect(screen, color, Rect(position, size))
    pygame.display.update()

这将无限地绘制相同的东西,直到你关闭应用程序。顺便说一句,这也是(几乎)所有视觉应用程序一直在做的事情,即每次都绘制所有内容。这并不是说你可以在屏幕上画一次它就会停留。每次画出画面时,屏幕都会询问所有人“好人,现在屏幕上发生了什么”,除非你的应用程序说“画出这些东西”,否则它不会被画出来,即使它是上次绘制的。

希望这有帮助。