ForumsProgramming ForumPython help!

6 11571
KingLemon
offline
KingLemon
600 posts
Nomad

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

  • 6 Replies
KingLemon
offline
KingLemon
600 posts
Nomad

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

thewolf52
offline
thewolf52
28 posts
Nomad
PixelSmash
offline
PixelSmash
566 posts
Nomad

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.

thepunisher93
offline
thepunisher93
1,825 posts
Nomad

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()

dfghj123
offline
dfghj123
20 posts
Nomad

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)

thepunisher93
offline
thepunisher93
1,825 posts
Nomad

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))
Showing 1-6 of 6