Different colors for shapes using iterations in Python / Pygame?

I am new to stackoverflow, but was hoping for a little understanding of more advanced programmers. I will turn to computer scientists next semester, and I will take an intro class that will learn Python programming for beginners. I already finished the program below (the task was to make a program that draws ovals on the surface of the window, filling some of the professor’s code is not so bad), but I wanted to add something else: as you can see, my oval color is set as random, but it remains unchanged until the program is restarted completely, i.e. all ovals are a special color for the length of the program. With code written as it is, I cannot figure out how to change the color for each oval. Keep in mind, this is all for kicks, but if someone feels particularly helpful or creative,I'm curious to see what you say. Let me know if I can state anything. Thank!


import pygame, random, sys

WINDOWWIDTH = 700
WINDOWHEIGHT = 700
BACKGROUNDCOLOR = (150,160,100)
#A different color every run
OVAL_COLOR = (random.randint (0,255),random.randint (0,255),
                    random.randint (0,255))

pygame.init()
windowSurface = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))
pygame.display.set_caption("Mobile Ovals")

#The draw variable is used later to indicate the mouse is still pressed
ovals = []
completedOvals = []
finished = False
draw = False
startXY = (-1, -1)

while not finished:
    for event in pygame.event.get():
        if event.type == pygame.QUIT or (event.type == pygame.KEYUP and
                    event.key == pygame.K_ESCAPE):
            finished = True

        elif event.type == pygame.KEYDOWN:
            pressed = pygame.key.get_pressed()
            if pressed[pygame.K_F4] and (pressed[pygame.K_LALT] or
                        pressed[pygame.K_RALT]):
                finished  = True

        elif event.type == pygame.MOUSEBUTTONDOWN:
            startXY = event.pos
            draw = True

        elif event.type == pygame.MOUSEBUTTONUP:
            draw = False
            for oval in ovals:
                completedOvals.append (oval)

        if draw == True:
            del ovals [:]

            #The above function ensures only one oval is onscreen at any given time
            endXY = event.pos
            width = (abs(endXY[0]-startXY[0]))
            height = (abs(endXY[1]-startXY[1]))

            #The code below allows the user to drag any direction
            if endXY[0] < startXY[0]:
                left = endXY[0]
            else:
                left = startXY[0]
            if endXY[1] < startXY[1]:
                top = endXY[1]
            else:
                top = startXY[1]

            ovals.append (pygame.Rect (left, top, width, height))

            windowSurface.fill(BACKGROUNDCOLOR)

            for oval in ovals:
                pygame.draw.ellipse(windowSurface, OVAL_COLOR, oval)

            for completedOval in completedOvals:
                pygame.draw.ellipse(windowSurface, OVAL_COLOR, completedOval)

            pygame.display.update()

pygame.quit()
+3
2

. OVAL_COLOR . , OVAL_COLOR, , RGB, .

, , , , draw ​​ true. OVAL_COLOR for, , , , .

, OVAL_COLOR, . , , . , , , , .


, . , , .

    elif event.type == pygame.MOUSEBUTTONDOWN:
        startXY = event.pos
        OVAL_COLOR = (random.randint (0,255),random.randint (0,255),
                            random.randint (0,255))
        draw = True

, , .

    elif event.type == pygame.MOUSEBUTTONUP:
        draw = False
        # print len(ovals) # (always ==1)
        completedOvals.append ((ovals[-1], OVAL_COLOR)) 

, .

        for (completedOval, color) in completedOvals:
            pygame.draw.ellipse(windowSurface, color, completedOval)
+2

Oval(), .

import pygame
from pygame.locals import * 

class Oval(object):
    """handle, and draw basic ovals. stores Rect() and Color()"""
    def __init__(self, startXY, endXY):
        self.color = Color(random.randint(0,255), random.randint(0,255), random.randint(0,255))
        self.rect = Rect(0,0,1,1)
        self.coord_to_oval(startXY, endXY)

    def draw(self):
        pygame.draw.ellipse(windowSurface, self.color, self.rect)

    def coord_to_oval(self, startXY, endXY):
        width = (abs(endXY[0]-startXY[0]))
        height = (abs(endXY[1]-startXY[1]))

        #The code below allows the user to drag any direction
        if endXY[0] < startXY[0]:
            left = endXY[0]
        else:
            left = startXY[0]
        if endXY[1] < startXY[1]:
            top = endXY[1]
        else:
            top = startXY[1]

        self.rect = Rect(left, top, width, height)

# main loop
while not finished:
    for event in pygame.event.get():
       # events, and creation:
       # ... your other events here ...

       elif event.type == MOUSEBUTTONDOWN:
            startXY = event.pos
            draw = True

        elif event.type ==MOUSEBUTTONUP:
            # on mouseup, create instance.
            endXY = event.pos
            oval_new = Oval(startXY, endXY)
            completedOvals.append(oval_new)

        # draw them:
        for oval in ovals:
                oval.draw()
        for oval in completedOvals:
                oval.draw()

. ?

+1

All Articles