Pygame не рисует линии - PullRequest
0 голосов
/ 12 мая 2018

Каждый раз, когда я запускаю программу, экран pygame открывается сразу же.Я пытаюсь открыть экран pygame, как только пользовательский ввод будет завершен, однако, если я изменю положение screen = pygame.display.set_mode((1024, 768)), программа больше не будет рисовать линии.Есть ли способ открыть экран только после того, как пользовательский ввод будет собран?

import pygame
from pygame.locals import *

gray = (100,100,100)
lightgray = (200,200,200)
red = (255,0,0)
blue = (0,0,255)

p = [] #empty points array
#gets 4 control points from user input
def get_points():
    #loops through 4 times to get 4 control points
    for i in range(4):
        while True:
            #user input
            p_input = input("Enter X,Y Coordinates for p" + str(i) + ":")
            #splits the string into x and y coordinates
            p_components = p_input.split(',')
            #checks to see if user hasnt entered two coordinates
            if len(p_components) != 2:
                print("Missing coordinate please try again.")
                p_input = input("Enter X,Y Coordinates for p" + str(i) + ":")
                p_components = p_input.split(',')
            #checks to see if the values can not be converted into floats
            try:
                x = float(p_components[0])
                y = float(p_components[1])
            except ValueError:
                print("Invalid coordinates", p_components, "please try again.")
            #appends the x and y coordinates as a 2 dimensional array
            else:
                p.append([float(p_components[0]), float(p_components[1])])
                break
#gets parameter 't' interval from user input
def get_interval():
    while True: 
        try:
            i = int(input("Please enter an interval for the parameter t:"))
        except ValueError:
            print("Invalid interval, please try again")
        else:
            i = abs(i)
            break
    return i
#calculates required coordinates for plotting bezier curve.
def bezier():
    result = [] #empty result array, which will store values x and y values from the bezier curve equation
    get_points() #gets the 4 control points
    i = get_interval() #gets the parameter 't' interval 
    for x in range(i+1): #i+1 so that it includes the last value
        t = x/i #x/i due to python not being able to have a step value of a float, so this is a work around
        x=(p[0][0]*(1-t)**3+p[1][0]*3*t*(1-t)**2+p[2][0]*3*t**2*(1-t)+p[3][0]*t**3) #calculates x coordinate
        y=(p[0][1]*(1-t)**3+p[1][1]*3*t*(1-t)**2+p[2][1]*3*t**2*(1-t)+p[3][1]*t**3) #calculates y coordinate
        result.append((int(x), int(y))) #appends coordinates to result array.
    return result

def main(): 
    pygame.init()
    screen = pygame.display.set_mode((1024, 768))
    points = bezier()
    clock = pygame.time.Clock()

    #draws the control points
    for i in p:
            pygame.draw.circle(screen, blue, (int(i[0]), int(i[1])), 4)
    #draws the lines between control points
    pygame.draw.lines(screen, lightgray, False, p)
    #draws the bezier curve
    pygame.draw.lines(screen, pygame.Color("red"), False, points, 2)
    pygame.display.flip()
    clock.tick(100)


if __name__ == "__main__":
    main()

1 Ответ

0 голосов
/ 12 мая 2018

Окно будет создано, как только вы позвоните pygame.display.set_mode.Если вы хотите открыть его после ввода точек, назовите его ниже bezier().

Кроме того, поместите свой код рисования в цикл while, в противном случае вы запустите этот код только один раз, и программа остановится впоследствии.

def main():
    pygame.init()

    points = bezier()
    screen = pygame.display.set_mode((1024, 768))
    clock = pygame.time.Clock()

    done = False
    while not done:
        for event in pygame.event.get():
            # Close the window by pressing the x button.
            if event.type == pygame.QUIT:
                done = True

        #draws the control points
        for i in p:
            pygame.draw.circle(screen, blue, (int(i[0]), int(i[1])), 4)
        #draws the lines between control points
        pygame.draw.lines(screen, lightgray, False, p)
        #draws the bezier curve
        pygame.draw.lines(screen, pygame.Color("red"), False, points, 2)
        pygame.display.flip()
        clock.tick(100)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...