1
Python / Problem with colors
« on: September 04, 2011, 11:26:11 pm »
Thanks for the information.
So I have to create all my rectangles with white.
So I have to create all my rectangles with white.
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#!/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()
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)
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)
#include <SFML/Graphics.hpp>
class SDerived : public sf::Sprite
{
public:
SDerived(sf::Image SDImg) : sf::Sprite(SDImg){}
};