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.

Dani AI

Generated

Nice little sketcher, . Building on ’s and @Ene Uran’s points: simply changing the color variable in your current loop will recolor the entire polyline because you redraw all points each frame. If you want different colors per segment, draw only the newest segment when you click and choose a color at that moment. That also avoids re-stroking the whole shape every tick.

Here’s a minimal pattern that changes color either every 5 clicks (palette cycling) or randomly each click. It draws only the last segment, so each segment keeps the color it had when created. Press C to manually cycle colors.

import pygame, sys, random
pygame.init()
screen = pygame.display.set_mode((800, 500))
screen.fill((25, 25, 25))

points, clicks = [], 0
palette = [(255, 99, 71), (30, 144, 255), (60, 179, 113), (238, 130, 238)]
color = palette[0]

running = True
while running:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False

        elif event.type == pygame.MOUSEBUTTONDOWN:
            points.append(event.pos)
            clicks += 1

            # every 5 clicks:
            color = palette[(clicks // 5) % len(palette)]
            # or per-click random:
            # color = (random.randrange(256), random.randrange(256), random.randrange(256))

            if len(points) >= 2:
                changed = pygame.draw.line(screen, color, points[-2], points[-1], 5)
                pygame.display.update(changed)

        elif event.type == pygame.KEYDOWN and event.key == pygame.K_c:
            i = (palette.index(color) + 1) % len(palette)
            color = palette[i]

pygame.quit(); sys.exit()

Tips:

  • Want a closed shape like your original? When finished, draw one extra segment from points[-1] to points[0] in your current color.
  • For smoother performance, update only the returned rect from draw.line (as shown) instead of the full screen each frame.

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.