I am trying to make a program in pygame on how to draw a star using a set of points which are variables

Viewed 1469

1.Question:

I want to learn how to make a star using multiple coordinates as variables and using lines to draw them, what I have done so far is correct but if someone could help me with the other variables and apply them to my code that would be great :D

2 code:

import pygame
from pygame.locals import *

pygame.init
white = (255,255,255)
coordinate2 = 250
coordinate1 = 0
coordinate4 = 500
coordinate3 = 250
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("AppliedShapes")

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit
            exit()
        pygame.draw.line(screen,white,(coordinate2,coordinate1),(coordinate4,coordinate3),5)
    pygame.display.update()

3 request:

This is what I have so far and it is correct but I want to make more variables and make more lines to make a star. Sooooo if someone could tell me the variables and apply them that would be great!

1 Answers

You can continue like you have, simply defining more coordinateX variables. However, this will eventually become a problem. Imagine having hundreds of separate variables!

A nicer way would be to use a Python list to hold all the co-ordinates:

point_list = [ (250, 0),  (500, 250) ]

(There's lots of online tutorials about Python lists.)

Each element can be accessed (or assigned) by using square-bracket notation with an index-number (starting at zero), so point_list[0] is (250, 0); and point_list[1] is (500, 250). To get the individual x,y parts of each co-ordinate, you can add another pair of square brackets, or get all the parts at once:

first_x = points_list[0][0]   # 250
first_y = points_list[0][1]   #   0 

first_x, first_y = points_list[0]    # 250, 0

Lists can hold huge amounts of points, for example, a simple 3-point star:

point_list = [ (148, 170), (200, 20), (252, 170), (356, 290), (200, 260), (44, 290) ]

The other nice thing about keeping them in a list is that you can use the PyGame function pygame.draw.polygon() to simply draw them to the screen.

pygame.draw.polygon( window, YELLOW, point_list, 1 )

Or you can "iterate" through the list, drawing the lines:

for i in range( len( points_list ) - 1 ):
    pygame.draw.line( screen, white, points_list[i], points_list[i+1], 5 )

Note that I looped with the length-of-list len() minus one, so we didn't go over the end of the list when referencing [i+1].

Here's a quick example I wrote using the above ideas:

screen-shot

Code:

import pygame

# Window size
WINDOW_WIDTH    = 400
WINDOW_HEIGHT   = 400

DARK_BLUE  = (   3,   5,  54)
STARRY     = ( 230, 255, 80 )


### initialisation
pygame.init()
window = pygame.display.set_mode( ( WINDOW_WIDTH, WINDOW_HEIGHT ) )
pygame.display.set_caption("Star")


# Define a star
centre_coord = ( WINDOW_WIDTH//2, WINDOW_HEIGHT//2 )
star_points  = [ (165, 151), (200, 20), (235, 151), (371, 144), (257, 219),
                 (306, 346), (200, 260), (94, 346), (143, 219), (29, 144)   ]

### Main Loop
clock = pygame.time.Clock()
done = False
while not done:

    # Handle user-input
    for event in pygame.event.get():
        if ( event.type == pygame.QUIT ):
            done = True

    # Re-draw the window
    window.fill( DARK_BLUE )                             # background fill
    pygame.draw.polygon( window, STARRY, star_points )   # Draw the star
    pygame.display.flip()                                # Update the display

    # Clamp FPS
    clock.tick_busy_loop(60)

pygame.quit()

Or perhaps a 16-point star of 32 co-ordinates:

point_list = [ (173, 63), (200, 20), (227, 63), (269, 34), (278, 84), 
               (327, 73), (316, 122), (366, 131), (337, 173), (380, 200), 
               (337, 227), (366, 269), (316, 278), (327, 327), (278, 316), 
               (269, 366), (227, 337), (200, 380), (173, 337), (131, 366),
               (122, 316), (73, 327), (84, 278), (34, 269), (63, 227), 
               (20, 200), (63, 173), (34, 131), (84, 122), (73, 73), 
               (122, 84), (131, 34) ]
Related