在Pygame中绘制一个又一个的图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在Pygame中绘制一个又一个的图像相关的知识,希望对你有一定的参考价值。

我正在编写一个程序,在pygame窗口中绘制100个随机(大小和颜色)圆圈。我希望每次绘制一个圆圈。出于某种原因,圆圈仅在最后绘制。有没有人看到这个原因?

import pygame, random, util

pygame.init()

side = 600
win = pygame.display.set_mode((side, side))
win.fill(util.white)
pygame.display.update()

for i in range(100):
    radius = random.randrange(2, 15)
    r = random.randrange(256)
    g = random.randrange(256)
    b = random.randrange(256)
    x = random.randrange(0 + radius, side - radius)
    y = random.randrange(0 + radius, side - radius)

    pygame.draw.circle(win, (r, g, b), (x, y), radius)
    pygame.display.update()
    print(i)


util.wait_for_quit()
答案

你可以在pygame.time.get_ticks()循环中使用while来控制何时绘制元素。这样,您可以绘制具有不同延迟的不同元素,您仍然可以使用键来停止它。

import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

WHITE = (255, 255, 255)
SIDE = 600

# --- main ---

# - init -

pygame.init()

win = pygame.display.set_mode((SIDE, SIDE))

win.fill(WHITE)
pygame.display.update()

# - objects -

circles_number = 100

next_circle = pygame.time.get_ticks() + 500 # 500ms = 0.5s

# - mainloop -

clock = pygame.time.Clock()

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            running = False

    if circles_number > 0:
        # get current time
        current = pygame.time.get_ticks()

        # check if it is time to draw next circle
        if current >= next_circle:    

            radius = random.randrange(2, 15)
            r = random.randrange(256)
            g = random.randrange(256)
            b = random.randrange(256)
            x = random.randrange(0 + radius, SIDE - radius)
            y = random.randrange(0 + radius, SIDE - radius)

            pygame.draw.circle(win, (r, g, b), (x, y), radius)
            pygame.display.update()

            # time for next circle
            next_circle = pygame.time.get_ticks() + 500 # 500ms = 0.5s

            # counting down circles
            circles_number -= 1                    

    clock.tick(25)

pygame.quit()

如果你需要以相同的延迟绘制所有,那么你可以使用clock.tick(2)来控制帧之间的时间。但是密钥也将以相同的延迟进行检查。如果延迟太长会导致问题,因为密钥会延迟太长时间。

import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

WHITE = (255, 255, 255)
SIDE = 600

# --- main ---

# - init -

pygame.init()

win = pygame.display.set_mode((SIDE, SIDE))

win.fill(WHITE)
pygame.display.update()

# - objects -

circles_number = 100

# - mainloop -

clock = pygame.time.Clock()

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            running = False

    if circles_number > 0:

        radius = random.randrange(2, 15)
        r = random.randrange(256)
        g = random.randrange(256)
        b = random.randrange(256)
        x = random.randrange(0 + radius, SIDE - radius)
        y = random.randrange(0 + radius, SIDE - radius)

        pygame.draw.circle(win, (r, g, b), (x, y), radius)
        pygame.display.update()

        # counting down circles
        circles_number -= 1                    

    # 2 frames per seconds = 2 circles per second
    clock.tick(2)

pygame.quit()

您还可以使用事件定期执行将绘制它的代码。

import pygame
import random

# --- constants --- (UPPER_CASE_NAMES)

WHITE = (255, 255, 255)
SIDE = 600

DRAW_CIRCLE_EVENT = pygame.USEREVENT

# --- main ---

# - init -

pygame.init()

win = pygame.display.set_mode((SIDE, SIDE))

win.fill(WHITE)
pygame.display.update()

# - objects -

circles_number = 10

# run event periodically
pygame.time.set_timer(DRAW_CIRCLE_EVENT, 500) # 500ms = 0.5s

# - mainloop -

clock = pygame.time.Clock()

running = True

while running:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            running = False
        if event.type == DRAW_CIRCLE_EVENT:

            if circles_number > 0:

                radius = random.randrange(2, 15)
                r = random.randrange(256)
                g = random.randrange(256)
                b = random.randrange(256)
                x = random.randrange(0 + radius, SIDE - radius)
                y = random.randrange(0 + radius, SIDE - radius)

                pygame.draw.circle(win, (r, g, b), (x, y), radius)
                pygame.display.update()

                # counting down circles
                circles_number -= 1
            else:
                # disable events
                pygame.time.set_timer(DRAW_CIRCLE_EVENT, 0)

    clock.tick(25)

pygame.quit()
另一答案

您可以使用time.sleep()来延迟绘图

for i in range(100):
    radius = random.randrange(2, 15)
    r = random.randrange(256)
    g = random.randrange(256)
    b = random.randrange(256)
    x = random.randrange(0 + radius, side - radius)
    y = random.randrange(0 + radius, side - radius)

    pygame.draw.circle(win, (r, g, b), (x, y), radius)
    pygame.display.update()
    print(i)
    time.sleep(0.001)

import module, moduleb也不是很好,你可以改成它

import module
import moduleb

编辑:这是一些更新的代码,不需要任何“Util”,并且更适合pygame

import pygame
import random
import time

pygame.init()

side = 600
win = pygame.display.set_mode((side, side))
win.fill((255, 255, 255))
loop = True
on_start = True
while loop:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            loop = False
    if on_start:
        for i in range(100):
            radius = random.randrange(2, 15)
            r = random.randrange(256)
            g = random.randrange(256)
            b = random.randrange(256)
            x = random.randrange(0 + radius, side - radius)
            y = random.randrange(0 + radius, side - radius)

            pygame.draw.circle(win, (r, g, b), (x, y), radius)
            pygame.display.update()
            time.sleep(0.01)
            print(i)
        on_start = False

pygame.quit()

以上是关于在Pygame中绘制一个又一个的图像的主要内容,如果未能解决你的问题,请参考以下文章

使用pygame绘制图像的好方法是啥?

python 在pygame中绘制TMX图块并将其保存为图像

PyGame快速入门

Pygame - 为动态绘制的对象获取矩形

在mac下初次使用pygame踩坑纪实(卡死)

pygame 的精灵使用