[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] question about Rect objects



Alex,

In your situation, I would approach the problem differently. I think that the sprite module's Group class and Sprite class would save you some time.

If I were creating your GUI, I would pre-draw red, blue, and white images. Then, I would make a Sprite for each square on your grid. I would make references to the Sprite in two locations. First, I would put each Sprite in a position in a 2D array. (I would implement this 2D array as a list of lists; there's no need to be fancy.) Second, I would add every sprite to a Group. For each sprite, a Rect would represent the Sprite's position and size on the screen; this Rect would be assigned to Sprite.rect. The state of each square (hit, miss, etc.) would be indicated by changing the Sprite.image attribute to the appropriate pre-drawn image.

Jason


From: Alex Hall <mehgcap@xxxxxxxxx>
To: pygame-users@xxxxxxxx
Sent: Mon, May 17, 2010 11:24:15 PM
Subject: [pygame] question about Rect objects

Hi all,
I am new to this list. A quick intro:
I am a blind college student studying for a computer science degree. I
use Jaws for Windows from Freedom Scientific to read the content of my
computer screen. I have a bit of vision, enough to see high-contrast
colors in a big enough window (500x500 or larger is good).
I use Python2.6 with the latest release of Pygame. I am trying to add
a visual element to a Battleship game I wrote last year. Originally,
the game was all based on screen readers; the user could arrow around
and their screen reader (the program that reads text on the computer)
would announce which square they were on and the status of that
square, among other information. No visuals were needed since most
screen readers offer an API which I can interface with directly.
However, I want to expand this to a 2-player, online game so my
sighted friend and I can play against each other. Of course, for that,
I need to give him something to look at.

I am trying to generate a grid of rectangles, each rect a square on
the classic Battleship grid. All rects will start out gray, and will
then change color as the game progresses (red=hit, white=miss,
blue=enemy sighted). I have a Square class, which has variables like
firedOn or ship. That way I can examine the square on which the user
fired, change its firedOn from False to True, and say something like
"if(grid[i][j].ship!=None): #change color to red". You get the idea.

Anyway, the pygame documentation says rect cannot be subclassed. Is
there any way, then, for me to put a rect object in my Square class,
then draw the grid using my grid (a 2d array of Squares)? I guess what
I want is to say:
bg.replace_ip(red, grid[i][j].rect) #change fired-on grid square to
red and redraw it
after building my grid of rects? I am quite new to Pygame, and this is
my first time venturing into the world of generating shapes and grids;
up to now, my involvement in visual elements has been some
experimenting with wxPython and XRC, and that package does most of the
layout for you. Any help with this grid problem would be greatly
appreciated, and please tell me if my above question and explanation
do not make sense (as I doubt they will).

--
Have a great day,
Alex (msg sent from GMail website)
mehgcap@xxxxxxxxx; http://www.facebook.com/mehgcap