SFML community forums
Bindings - other languages => Python => Topic started by: KevinZ on September 03, 2011, 09:31:12 pm
-
I'm doing a 2d Rubik's cube application, but I have some problems when changing colors.
This is main.py
#!/usr/bin/env python
from PySFML import sf
from cube import Cube
def main():
window = sf.RenderWindow(sf.VideoMode(800, 600), "Rubik's Cube")
cube = Cube(0, 0)
running = True
while running:
event = sf.Event()
while window.GetEvent(event):
if event.Type == sf.Event.Closed:
running = False
elif event.Type == sf.Event.MouseButtonPressed and event.MouseButton.Button == sf.Mouse.Left:
cube.changeColor(event.MouseButton.X, event.MouseButton.Y)
window.Clear()
cube.draw(window)
window.Display()
if __name__ == '__main__':
main()
this is cube.py
from PySFML import sf
import const
class LittleFace(object):
def __init__(self, x, y, color):
self.color = color
self.shape = sf.Shape.Rectangle(x, y, x + const.width, y + const.width, const.colors[color], 1)
def nextColor(self):
self.color = (self.color + 1) % 6
self.shape.SetColor(const.colors[self.color])
def draw(self, window):
window.Draw(self.shape)
class Face(object):
def __init__(self, x, y, color):
self.littleFaces = []
for i in range(9):
a = x + i%3 * const.width
b = y + i/3 * const.width
self.littleFaces.append(LittleFace(a, b, color))
def draw(self, window):
for littleFace in self.littleFaces:
littleFace.draw(window)
def changeColor(self, x, y):
xx = x
yy = y
self.littleFaces[xx + yy * 3].nextColor()
class Cube(object):
def __init__(self, x, y):
self.x = x
self.y = y
self.faces = [Face(x + const.xs[i], y + const.ys[i], i) for i in range(6)]
def changeColor(self, mx, my):
xx = (mx - self.x) / const.width
yy = (my - self.y) / const.width
if xx < 3 :
if 3 <= yy and yy < 6:
self.faces[2].changeColor(xx, yy - 3)
elif xx < 6:
if yy < 3:
self.faces[1].changeColor(xx - 3, yy)
elif yy < 6:
self.faces[0].changeColor(xx - 3, yy - 3)
elif yy < 9:
self.faces[4].changeColor(xx - 3, yy - 6)
elif yy < 12:
self.faces[5].changeColor(xx - 3, yy - 9)
elif xx < 9:
if 3 <= yy and yy < 6:
self.faces[3].changeColor(xx - 6, yy - 3)
def draw(self, window):
for face in self.faces:
face.draw(window)
and this is const.py
from PySFML import sf
colors = [sf.Color.Red,
sf.Color.Green,
sf.Color.Blue,
sf.Color.Yellow,
sf.Color.White,
sf.Color(255, 127, 0)
]
width = 40
d3 = width*3
xs = (d3, d3, 0 , d3*2, d3 , d3 )
ys = (d3, 0 , d3, d3 , d3*2, d3*3)
When I click in a littleFace it should change it color to the next one, but the only group of littleFaces that work properly are the ones that start with white, the other ones change to black (or are not drawn), other colors, or not change when I click.
I don't where the problem is.
I'm using python 2.6.7 and sfml 1.6.
Thanks for your help, and sorry for the bad English.
EDIT: fixed unrelated error
-
I made it work
Instead of changing the color in the shape I just create a new shape.
Maybe this is a bug in PySFML?
-
The default points color (the one that you pass to sf.Shape.Rectangle) is not the global color (the one you pass to SetColor), these two colors are modulated (multiplied) together. You should create your shape with white points and play only with SetColor, if all points have the same color.
-
Thanks for the information.
So I have to create all my rectangles with white.