Home > OS >  Writing a function that draws a cube. I can't manage to make a full cube, just the y axis cube
Writing a function that draws a cube. I can't manage to make a full cube, just the y axis cube

Time:05-16

def graphics ():
window = tkinter.Tk()
window.geometry("800x800")  #You enter the window dimensions for 
canvas = tkinter.Canvas(window, bg = 'white', width=800, height=800)
#Code for creating graphical shapes
#  note for me: im going to make a graphical drawing that draws a the sizes of the cubes user wants ! (so if there's a 2x2 cube, 3x3, and 4x4, ima draw those (2d only)
user_size = input("What size cube would you like to have drawn? (3x3, 2x2, ...): ")
user_size.split()
cube_size = int(user_size[0])
cube_size  = 1
counter = 0
cube_y = 800
cube_y2 = 700
cube_x = 100
cube_x2 = 200
counter = 1
e = 'red'
 
while (counter != cube_size):

    y = canvas.create_polygon(100,cube_y,200,cube_y,200,cube_y2,100,cube_y2, fill = e, outline = 'black', width = 7)
 
    
    cube_y = cube_y - 100
    cube_y2 = cube_y2 - 100
   
    
    print(counter)
    counter = 1

#Flips the shapes from memory onto the monitor
canvas.pack()


window.mainloop()

as the title says, I want to make a program for school that draws rubiks cube. Essentially, user enters size of cube (3x3, 4x4, ...) and the program reads the first number of the entered size. So if user writes 3x3, program will read the number '3' at the beginning.

From there, the program will run y = canvas.create_polygon(100,cube_y,200,cube_y,200,cube_y2,100,cube_y2, fill = e, outline = 'black', which creates a red block 100x100 px wide. For as many times as the loop runs, the block will go up by 100 px (it'll go up y-axis). For example, if user enters 3x3, the y code block will be made 3 times, each time it'll go up 100 px. this is what it looks like if i enter 3x3

What I require help with is dealing with the x axis. I've managed to get the program to draw the cube on the vertical axis, but i can't manage on the horizontal axis. My plan was to essentially have the code y = canvas.create_polygon(100,cube_y,200,cube_y,200,cube_y2,100,cube_y2, fill = e, outline = 'black' run as many times as the counter, but with each time, the x points of the polygon goes 100 px right.

So for ex. if I were to ask for 4x4 cube to be drawn, the program would firstly draw the 4 cubes going up/vertical/y-axis using the y = canvas.create_polygon() code i mentioned before, then it would draw the same shape but this time the entire code would be moved 100 px right, on the x axis, horizontally. The final product should look something like this (pardon the shitty drawing i did this on google jamboard using mouse)

If anyone has any idea on how to do so, please let me know. And apologies for the wall of text in advance! Thx!

CodePudding user response:

This is the code I have written, this is to give you a start, it isn't perfect but you can adjust the sizes of the cube how you want, and the window.

I implemented a loop which loops over columns and rows, which is set to the cube size. If the cube size is set to 3, the loop will go from 1 to 3 and draw each shape. The shape is draw using canvas.create_rectangle(tileSize * col, tileSize * row, tileSize * col tileSize, tileSize * row tileSize, fill='red').

tileSize * col = 50*1, 50*2, 50*3 == 50, 100, 150 and it is the same for tileSize * row

the second x and y are the x and y above tileSize which makes each rectangle 50x50 pixels. You can adjust the sizes and placement to your liking.

Adjust this part: canvas.create_rectangle(tileSize * col, tileSize * row, tileSize * col tileSize, tileSize * row tileSize, fill='red') to change where the cube starts to draw.

Adjust this part: canvas.create_rectangle(tileSize * col, tileSize * row, tileSize * col tileSize, tileSize * row tileSize, fill='red') for the size of each square.

from tkinter import *


def graphics():
    window = Tk()
    window.geometry("800x800")  # You enter the window dimensions for

    # tile/window sizes
    tileSize = 50
    x = tileSize * 5
    y = tileSize * 5

    canvas = Canvas(window, bg='white', width=x, height=y)

    cols = int(input('What size cube do you want?: '))
    rows = cols


    for col in range(cols):
        for row in range(rows):
            canvas.create_rectangle(tileSize * col, tileSize * row, tileSize * col   tileSize, tileSize * row   tileSize, fill='red')

    # Flips the shapes from memory onto the monitor
    canvas.pack()

    window.mainloop()


graphics()
  • Related