Forums

ForumsProgramming Forum

Python help!

Posted Sep 18, '12 at 3:36pm

KingLemon

KingLemon

617 posts

So my professor gave us an assignment and I cannot figure out at all how to get it to work, he apparently gave us a hint yesterday but I was deathly ill. He doesn't really help and the book doesn't tell you either. So I know I'm new to this forum but can someone help? Here's the assignment:
http://i.imgur.com/oUQH4.png

This is all I have right now and I'm so lost, I can't even get the thing to draw a single rectangle!:

http://i.imgur.com/t4rP0.png

 

Posted Sep 18, '12 at 4:10pm

KingLemon

KingLemon

617 posts

Okay I figured out how I get it to do the columns and rows correctly.
I can't seem to figure out how to make the checkerboard effect like in the picture. And on top of that I need to make another image that is inside the rectangles D:
I am now officially stuck.

http://i.imgur.com/u8LnS.png

 

Posted Sep 18, '12 at 6:42pm

thewolf52

thewolf52

28 posts

 

Posted Sep 18, '12 at 6:50pm

PixelSmash

PixelSmash

579 posts

Well I know nothing of Python, but as far as a 'general' (java/javascript/actionscript) approach, I'd probably do something like the following pseudocode. I'm assuming draw.rect's 0,0 means starting at x,y - so here goes:

for h in range (0, num_column) :
for w in range (0, num_row) :
color = color1
if((h + w) % 2 == 1) color = color2

pygame.draw.rect(screen, color, (w * rect_Width, h * rect_Height, rect_Width, rect_Height))
else:
else:

As I said before, this is pseudocode and I know nothing about Python *I had to look up the for code, and there's a big chance I'm not using it 100% correct* - but it should give you some pointers. Here's a generic breakdown of what it does:
- The double for-loop makes sure you call the code for each rectangle
- the if statement leaves the color either at color1 or sets it to color2, by checking if h + w is uneven (1) - this is some pretty heavy pseudocode, beware!
- you then use the draw.rect with the width and height params you created in the for loops.

 

Posted Sep 27, '12 at 8:22pm

thepunisher93

thepunisher93

1,863 posts

import pygame
import random
import time
win_width= 600
win_height = 600
#pygame initialize
pygame.display.init()
screen = pygame.display.set_mode((win_width, win_height))
chkrboard = pygame.Surface(win_width, win_height)
#Chkerboard
num_row=12
num_col=12
color1 = (0, 0, 0)
color2 = (256, 256, 256)
#draw rect
rect_w = 600/12
rect_h = 600/12
x= rect_h+rect_w
if x%2==1:
pygame.draw.rect(screen, color1, rect_w, rect_h)
else:
pygame.draw.rect(screen, color2, rect_w, rect_h)
time.sleep(10)
pygame.display.quit()

 

Posted Oct 6, '12 at 8:54am

dfghj123

dfghj123

20 posts

Hmm, the above code is wrong (according to Python):

Traceback (most recent call last):
File "something.py", line 10, in
chkrboard = pygame.Surface(win_width, win_height)
ValueError: size needs to be (int width, int height)

 

Posted Oct 6, '12 at 7:50pm

thepunisher93

thepunisher93

1,863 posts

Hmm, the above code is wrong (according to Python):

Traceback (most recent call last):
File "something.py", line 10, in
chkrboard = pygame.Surface(win_width, win_height)
ValueError: size needs to be (int width, int height)

Pygame.Surface(surface, (width, height))
 
Reply to Python help!

You must be logged in to post a reply!