转移图纸Pygame

时间:2018-11-25 21:16:56

标签: python graphics timer pygame

我有3种不同的绘制功能,分别是字母,矩形(黄色和红色)和更新矩形位置。

enter image description here

我的功能

def DrawText():
Alphabet = list(string.ascii_uppercase + string.digits)

HorizontalIterator = 0
VerticalIterator = 0
for i in range(len(Alphabet)):
    font = pygame.font.SysFont('Arial', 250, True, False)
    font = pygame.font.Font(None, 225)
    text = font.render(Alphabet[i], True, BLACK)
    screen.blit(text, [20 + HorizontalIterator, 100 + VerticalIterator])
    HorizontalIterator = HorizontalIterator + 190
    if HorizontalIterator >= 1700:
        HorizontalIterator = 0
        VerticalIterator = VerticalIterator + 180



def Fill(Iterator):
RectangleHorizontal = pygame.Surface((140, 700), pygame.SRCALPHA, 32)
RectangleHorizontal.fill(((255, 255, 0, 150)))
screen.blit(RectangleHorizontal, (10 + Iterator, 95))

# Vertical Rectangle
RectangleVertical = pygame.Surface((1650, 140), pygame.SRCALPHA, 32)
RectangleVertical.fill(((255, 0, 0, 150)))
screen.blit(RectangleVertical, (10, 95))

return RectangleHorizontal, RectangleVertical

这2是我设置显示器时的主要功能

此功能更新矩形的新位置

def NewPositionOfRectangle(RectangleHorizontal, RectangleVertical, Iterator):
RectangleHorizontal = pygame.Surface((140, 700), pygame.SRCALPHA, 32)
RectangleHorizontal.fill(((255, 255, 0, 150)))
screen.blit(RectangleHorizontal, (10 + Iterator, 95))

# Vertical Rectangle
RectangleVertical = pygame.Surface((1650, 140), pygame.SRCALPHA, 32)
RectangleVertical.fill(((255, 0, 0, 150)))
screen.blit(RectangleVertical, (10, 635))

我有一个for循环,每次迭代我的矩形位置。但是,每当我移动矩形时,我都想显示每个步骤。 (例如:从第一行到第二行,从第二列到第三列,等等。)

问题是,在我的for循环中,我尝试了很多事情,但无法解决。有什么建议可以做到这一点?我尝试过,定时器,等待,睡眠,显示更新,也许我做错了。

这是我的循环

    for Iterator in range(0, 1530, 190):

    NewPositionOfRectangle(LeftRightMove, UpdownMove, Iterator)

    screen.fill(WHITE)
    DrawText()
    Fill(Iterator)

    if Iterator == 1520:
        Iterator = 0

总而言之,我想每秒更新一次我的红色和黄色矩形位置。例如,黄色矩形位于I,R 0 9上,一秒钟后它将转到H Q Z8。

如果您想查看我的所有代码:

import pygame
import string
import sys
import time
import threading

Clock = pygame.time.Clock()
Iterator = 0


def DrawText():
    Alphabet = list(string.ascii_uppercase + string.digits)

    HorizontalIterator = 0
    VerticalIterator = 0
    for i in range(len(Alphabet)):
        font = pygame.font.SysFont('Arial', 250, True, False)
        font = pygame.font.Font(None, 225)
        text = font.render(Alphabet[i], True, BLACK)
        screen.blit(text, [20 + HorizontalIterator, 100 + VerticalIterator])
        HorizontalIterator = HorizontalIterator + 190
        if HorizontalIterator >= 1700:
            HorizontalIterator = 0
            VerticalIterator = VerticalIterator + 180


def Fill(Iterator):
    RectangleHorizontal = pygame.Surface((140, 700), pygame.SRCALPHA, 32)
    RectangleHorizontal.fill(((255, 255, 0, 150)))
    screen.blit(RectangleHorizontal, (10 + Iterator, 95))

    # Vertical Rectangle
    RectangleVertical = pygame.Surface((1650, 140), pygame.SRCALPHA, 32)
    RectangleVertical.fill(((255, 0, 0, 150)))
    screen.blit(RectangleVertical, (10, 95))

    return RectangleHorizontal, RectangleVertical


def NewPositionOfRectangle(RectangleHorizontal, RectangleVertical, Iterator):
    RectangleHorizontal = pygame.Surface((140, 700), pygame.SRCALPHA, 32)
    RectangleHorizontal.fill(((255, 255, 0, 150)))
    screen.blit(RectangleHorizontal, (10 + Iterator, 95))

    # Vertical Rectangle
    RectangleVertical = pygame.Surface((1650, 140), pygame.SRCALPHA, 32)
    RectangleVertical.fill(((255, 0, 0, 150)))
    screen.blit(RectangleVertical, (10, 635))


# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
DARKBLUE = (0, 0, 128)

pygame.init()

# Set the width and height of the screen [width, height]
size = (1700, 950)
screen = pygame.display.set_mode(size)

pygame.display.set_caption("Poligram")

# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates


# -------- Main Program Loop -----------
while not done:
    # --- Main event loop

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

    # --- Game logic should go here

    # --- Screen-clearing code goes here

    # Here, we clear the screen to white. Don't put other drawing commands
    # above this, or they will be erased with this command.

    # If you want a background image, replace this clear with blit'ing the
    # background image.

    screen.fill(WHITE)

    # --- Drawing code should go here

    LeftRightMove, UpdownMove = Fill(Iterator)

    for Iterator in range(0, 1530, 190):

        NewPositionOfRectangle(LeftRightMove, UpdownMove, Iterator)

        screen.fill(WHITE)
        DrawText()
        Fill(Iterator)

        if Iterator == 1520:
            Iterator = 0

    pygame.display.flip()

    # --- Limit to 60 frames per second

# Close the window and quit.
pygame.quit()

0 个答案:

没有答案