Forum Archive

[SOLVED!] Help with colour

Pythonistapro777

So, I'm making a much simpler tic tac toe game, the ones on here are too complicated :P. When you run the game you will see the tic tac toe rubric and 3 buttons underneath. The one on the left turn the draw colour blue, one in the middle turns the draw colour red. The one on the right, erases everything.
If you play with it you'll notice that tapping the button changes the draw colour but I need it to change the new draw colour and leave the old colour as it is.

Thanks in advance!

Here's the code:

from scene import *

class MyScene (Scene):
    def setup(self):
        global count
        count=0
        self.lines = []
        self.button = Button(Rect(self.size.w/2+85, self.size.h/2-245, 75, 50))
        self.button.background = Color(1,1,1)
        self.button.stroke = Color(1,1,1)
        self.button.action = self.add_clicks
        self.add_layer(self.button)
        self.redbutton = Button(Rect(self.size.w/2-35, self.size.h/2-245, 75, 50))
        self.redbutton.background = Color(1,0,0)
        self.redbutton.stroke = Color(1,1,1)
        self.redbutton.action = self.turnred
        self.add_layer(self.redbutton)
        self.bluebutton = Button(Rect(self.size.w/2-155, self.size.h/2-245, 75, 50))
        self.bluebutton.background = Color(0,0,1)
        self.bluebutton.stroke = Color(1,1,1)
        self.bluebutton.action = self.turnblue
        self.add_layer(self.bluebutton)

    def add_clicks(self):
        run(MyScene())

    def turnred(self):
        global count
        count=1
        self.redbutton.background = Color(1,0,0)

    def turnblue(self):
        global count
        count=2
        self.bluebutton.background = Color(0,0,1)

    def draw(self):
        background(0,0,0)
        self.button.draw()
        self.redbutton.draw()
        self.bluebutton.draw()
        fill(1,1,1)
        rect(0,180,320,15)
        rect(0,290,320,15)
        rect(92,87,15,310)
        rect(208,87,15,310)
        #draw colour
        global count
        if count==0:
            one=1
            two=1
            three=1
        if count==1:
            one=1
            two=0
            three=0
        if count==2:
            one=0
            two=0
            three=1
        stroke(int(one), int(two), int(three))
        stroke_weight(4)
        for l in self.lines:
            line(*l)

    def touch_moved(self, touch):
            x = touch.location.x
            y = touch.location.y
            ppos = touch.prev_location
            self.lines.append((ppos.x, ppos.y, x, y))


run(MyScene())

If I'm not explicit enough, please let me know.

ccc
  • Remove the global count and replace it with self.count.
  • Change the if, if, if statement to an if, elif, elif statement.
  • Change one=1 ; two=1 ; three=1 to stroke(1, 1, 1)
  • Accept (or reject) the GitHub pull requests so that progress can be preserved.
techteej

Really with most projects, GitHub is the way to go.

ccc

When you capture the line, you also need to capture the current stroke color. See colored_lines.py.

Pythonistapro777

@ccc I get what you mean, but don't know how to implement it.

ccc

Your script above has a line that reads:

self.lines.append((ppos.x, ppos.y, x, y))
# and you need to change it to read:
self.colored_lines.append(ColoredLine(self.stroke_color, (ppos.x, ppos.y, x, y)))

The rest of the code needed is either in the script above or in colored_lines.py. You can do it.

Edit: Added ColoredLine().

Pythonistapro777

@ccc I've started it but I've encountered 3 errors everytime.

All 3 at the end of the code. Could you please help in fixing them.

import scene
import collections, scene

g_color_red = scene.Color(1, 0, 0)
g_color_blue = scene.Color(0, 0, 1)
g_color_white = scene.Color(1, 1, 1)

ColoredLine = collections.namedtuple('ColoredLine', 'color line')

class MyScene(scene.Scene):
    def __init__(self):
        scene.run(self)

    def setup(self):
        self.colored_lines = [
        ColoredLine(scene.Color(1, 0, 0), (  0,   0,  49,  49)),
        ColoredLine(scene.Color(0, 1, 0), ( 50,  50,  99,  99)),
        ColoredLine(scene.Color(0, 0, 1), (100, 100, 149, 149)),
        ColoredLine(scene.Color(1, 1, 1), (150, 150, 199, 199)) ]
        # print(self.colored_lines[0])  # if you want to see what one looks like
        self.button_blue = self.make_button_blue()
        self.button_red = self.make_button_red()
        self.add_layer(self.button_blue)
        self.add_layer(self.button_red)
        self.add_layer(self.make_button_restart())


    def make_button_blue(self):
        button = scene.Button(scene.Rect(self.size.w/2-155, self.size.h/2-245, 75, 50))
        button.background = g_color_blue
        button.stroke = g_color_white
        button.action = self.turn_blue
        return button

    def make_button_red(self):
        button = scene.Button(scene.Rect(self.size.w/2-35, self.size.h/2-245, 75, 50))
        button.background = g_color_red
        button.stroke = g_color_white
        button.action = self.turn_red
        return button

    def make_button_restart(self):
        button = scene.Button(scene.Rect(self.size.w/2+85, self.size.h/2-245, 75, 50))
        button.background = g_color_white
        button.stroke = g_color_white
        button.action = self.restart
        return button

    def restart(self):
        MyScene()

    def turn_red(self):
        self.colored_lines = [1]

    def turn_blue(self):
        self.colored_lines = [2]

    def draw(self):
        scene.background(0, 0, 0)
        self.root_layer.update(self.dt)
        self.root_layer.draw()
        self.colored_lines = [3]
        scene.stroke(*self.stroke_color)
        scene.stroke_weight(4)
        scene.rect(0, 180, 320, 15)
        scene.rect(0, 290, 320, 15)
        scene.rect(92, 87, 15, 310)
        scene.rect(208, 87, 15, 310)

        for colored_line in self.colored_lines:
            scene.stroke(*colored_line.color)
            scene.line(*colored_line.line)


    def touch_moved(self, touch):
        x = touch.location.x
        y = touch.location.y
        ppos = touch.prev_location
        self.colored_lines.append(self.stroke_color, (ppos.x, ppos.y, x, y))

MyScene()
ccc
  • You are importing scene twice. Remove one or the other so that scene is only imported once.
  • In __init__() add the line self.stroke_color = g_color_blue
  • Comment out or remove the line self.colored_lines = [3]

Now the script should run. Test it to make sure it does. After it runs, to make forward progress again...

  • Add ColoredLine() to the second last line of the script so that it is self.colored_lines.append(ColoredLine(self.stroke_color, (ppos.x, ppos.y, x, y)))
  • Change turn_red() and turn_blue() to be exactly like they are in stroke_color.py on GitHub
  • Remove the four ColoredLine() calls in setup() so that the first line reads just self.colored_lines = []
  • Also remove the commented out print() line that follows.
Pythonistapro777

@ccc THANKS A BUNCH!