I have this bit of python code that imports pygames to draw lines. It auto creates lines when two or more points are clicked on. I'm bad at explaining but you'll see when you run the code. It's pretty simple but I find it kind of fun to mess with and I was wondering if it was possible to have it change colors either through a button or more simply just randomly or just at every five clicks.

This is the code:

##Set up
import pygame,sys
from pygame.locals import *

pygame.init()

screen = pygame.display.set_mode((800,500),0,32)

##Drawing lines
color = (100,150,50)
points=[]

##Enabling System Quit
while True:
    
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == MOUSEBUTTONDOWN:
            points.append(event.pos)
            
    if len(points)>1:
        pygame.draw.lines(screen, color, True, points, 5)

##False means won't auto create line when two or more points
##5 is the width of the line

##Display fix
    pygame.display.update()

Thanks a lot if you can help!

Sam.

Recommended Answers

All 2 Replies

Its easy, just have some code to alter the (color = (x,x,x)) and then the lines will be drawn with different colors. If you want a button to do it, then just have the code in your event loop.

Pygame uses (red, green, blue) tuples for colors. The values of the red, green, blue variables are integers between 0 and 255.

So
color_red = (255, 0, 0)
color_blue = (0, 0, 255)
and values between, just experiment a little.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.