简体   繁体   English

在pygame中慢慢画一条线,而其他线保持静止

[英]Slowly drawing a line in pygame while other lines remain static

In my last question i made a crucial mistake by not providing all the information and just editing it would result in an entirely different topic so i created a new question.在我的最后一个问题中,我犯了一个严重的错误,没有提供所有信息,只是编辑它会导致一个完全不同的主题,所以我创建了一个新问题。

What i didnt say is that i already have some lines printed on the screen (they serve playground for the game) and only in specific cases i want to add slowly drawn lines to the screen.我没有说的是我已经在屏幕上打印了一些线条(它们为游戏提供了操场)并且只有在特定情况下我才想在屏幕上添加慢慢绘制的线条。 pygame.update.flip() function seems to mess up with the game as my playground starts to blink.当我的操场开始闪烁时, pygame.update.flip()函数似乎搞砸了游戏。 The reason i didnt provide this explanation is that i didnt think the solution would have anything to do with formatting of other parts of my code.我没有提供这个解释的原因是我认为该解决方案与我的代码其他部分的格式没有任何关系。

I see two solutions here.我在这里看到两个解决方案。 Either i need a way to draw a line slowly without disturbing my "playground lines" or i need to find a way draw my "playground lines" so that they are not disturbed by pygame.update.flip() .要么我需要一种在不打扰我的“操场线”的情况下慢慢画线的方法,要么我需要找到一种方法来画我的“操场线”,这样它们就不会被pygame.update.flip()打扰。

line_start = [ (100, 0), (200, 0), (0, 100), (0, 200) ]
line_end = [ (100, 300), (200, 300), (300, 100), (300, 200) ]

def draw_lines(line_start, line_end):
    for idx in range(len(line_start)):
        pygame.draw.line(screen, BLACK, line_start[idx], line_end[idx])

This creates a fild used for Tic Tac Toe game这将创建一个用于 Tic Tac Toe 游戏的字段

def draw_red_line(i):
    y = 0
    while y < 300:
        pygame.draw.line(screen, RED, (i*100+50, 0), (i*100+50, y))
        pygame.display.flip()
        pygame.event.get()
        y+=1

This solution given by Selcuk messes up the game in a way i explained and other solutions are based around same method. Selcuk给出的这个解决方案以我解释的方式弄乱了游戏,其他解决方案基于相同的方法。

Create a function which can draw a line from a point s to a point e , dependent on a value p in range [0.0, 1.0].创建一个函数,该函数可以根据范围 [0.0, 1.0] 中的值p从点s到点e绘制一条线。 If the value is 0, then no line is drawn.如果值为 0,则不绘制线。 If the value is 1 the complete line is drawn.如果值为 1,则绘制完整的线。 Else a part of the line is drawn:否则绘制了一部分线:

def draw_red_line(s, e, p):
    x = s[0] * (1-p) + e[0] * p
    y = s[1] * (1-p) + e[1] * p
    pygame.draw.line(screen, RED, s, (round(x), round(y)))

You've 4 lines.你有 4 行。 Use a counter ( count ) which counts up from 0 to 4 by a small step (eg 0.01).使用计数器( count )从 0 到 4 以一小步(例如 0.01)递增。 The integral part of the counter states the number of lines which have to be draw completely and have to be persistent (remain):计数器的组成部分说明必须完全绘制且必须持久(保留)的线数:

for i in range(int(count)):
    draw_red_line(line_start[i], line_end[i], 1)

If the counter is less then 4, then the fraction part of the counter is used to draw the next line partially:如果计数器小于 4,则计数器的小数部分用于部分绘制下一行:

if count < 4:
    i = int(count)
    draw_red_line(line_start[i], line_end[i], count-i)
    count += 0.01

See the short example:请参阅简短示例:

import pygame

pygame.init()
screen = pygame.display.set_mode((300,300))
clock = pygame.time.Clock()
RED = (255, 0, 0)

line_start = [(100, 0),   (200, 0),   (0, 100),   (0, 200)]
line_end   = [(100, 300), (200, 300), (300, 100), (300, 200)]

def draw_red_line(s, e, p):
    x = s[0] * (1-p) + e[0] * p
    y = s[1] * (1-p) + e[1] * p
    pygame.draw.line(screen, RED, s, (round(x), round(y)))

count=0
run = True
while run:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False

    screen.fill(0)

    for i in range(int(count)):
        draw_red_line(line_start[i], line_end[i], 1)
    if count < 4:
        i = int(count)
        draw_red_line(line_start[i], line_end[i], count-i)
        count += 0.01

    pygame.display.flip()

pygame.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM