Real Software Forums

The forum for Real Studio and other Real Software products.
[ REAL Software Website | Board Index ]
It is currently Fri Dec 06, 2019 10:57 pm
xojo

All times are UTC - 5 hours




Post new topic Reply to topic  [ 20 posts ]  Go to page Previous  1, 2
Author Message
 Post subject: Re: Need Help with (2D) Sprites
PostPosted: Mon May 18, 2009 6:12 am 
Offline
User avatar

Joined: Wed Nov 15, 2006 3:50 pm
Posts: 2353
Location: England
In fact, here is some tested code...

Start with a 32 x 32 bitmap, white background.
Draw a piece on it in some non-white color
Save it as knight.bmp
Drag it into a new project.
Change the transparency to White in the properties window

Add a property to Window1

MyPieces as collection


Add a class
Call it ChessPiece
Give it new properties
Row as Integer
Col as Integer
Sprite as Picture
Taken as boolean



In the Open Event of Window1, add this code

dim x as integer
dim aPiece as ChessPiece
MyPieces = new Collection
for x = 0 to 3
aPiece = new ChessPiece
aPiece.Row = x
aPiece.col=x
aPiece.Sprite = knight
mypieces.add apiece
next


for x = 4 to 5
aPiece = new ChessPiece
aPiece.Row = x
aPiece.col=x+1 //just to get some on white squares
aPiece.Sprite = knight
mypieces.add apiece
next



In the Paint() event of Window1, add this code:

dim x as integer
dim y as integer
dim c as ChessPiece
for x = 0 to 7
for y = 0 to 7
if (x+y) mod 2 = 0 then
g.forecolor = &c000000
else
g.forecolor = &cFFFFFF
end if
g.fillrect x* 32, y* 32,32,32
next
next

x = mypieces.Count
for y = 1 to x
c = MyPieces.item(y)
if c.taken = false then g.DrawPicture c.Sprite,c.row * 32,c.col*32
next


(I have used 32 throughout this code, but it would have been better as a constant)


Top
 Profile  
Reply with quote  
 Post subject: Re: Need Help with (2D) Sprites
PostPosted: Mon May 18, 2009 12:30 pm 
Offline

Joined: Tue Sep 09, 2008 1:43 am
Posts: 39
Nice work on the sample code, but there are a few things I might do differently:

Having an array instead of a collection seems more suitable for this situation because the only elements are all going to be ChessPieces.

MyPieces(31) as ChessPiece //32 chess pieces

or
MyPieces(-1) as ChessPiece //we'll see how many chess pieces


Then when adding them to the array:
dim aPiece as ChessPiece
//this will create the first 16 pieces for the top of the board
for x = 0 to 15
aPiece = new ChessPiece
aPiece.Row = 0
if x >= 7 then aPiece.Row = 1 //second row
aPiece.col=x
aPiece.Sprite = knight //you'll want to create a switch statement or something to get the right piece
if x >= 7 then aPiece.Sprite = pawn //second row is all pawns
mypieces(x) = apiece
//if the array starts at -1 then use
//mypieces.append apiece
next

then the paint event code:
x = Ubound(mypieces)
for y = 1 to x
c = MyPieces(y)
if c.taken = false then g.DrawPicture c.Sprite,c.row * 32,c.col*32
next


Another thing (i'm nitpicking here) I would call the "Sprite" property "Image" instead, I think it might lead to less confusion.

And a question for the OP: are you actually going to make a chess game?


Top
 Profile  
Reply with quote  
 Post subject: Re: Need Help with (2D) Sprites
PostPosted: Mon May 18, 2009 5:41 pm 
Offline

Joined: Sat Nov 24, 2007 3:09 pm
Posts: 159
Location: Denver, CO
And for animation you would simply implement a timer within your game window (by adding a timer class to the IDE)- set the mode to "2-Multiple" and set the period to something like 100 (which is a paltry 10 frames/second, since every second is 1000 ticks)

within the timer.run method you would call your paint event code:
paintBoard()


the key here would be having multiple images or "frames" for the animation, so the ChessPiece class would have to be amended like so:
Row as Integer
Col as Integer
Image(5) as Picture
Taken as Boolean
animIndex as Integer


where the (5) denotes that you have 5 frames of animation- although you obviously could adjust this to whatever you needed. Now when you construct your MyPieces class, you would change the
code like so:
...
aPiece.col = x
aPiece.Image(0) = knight1
aPiece.Image(1) = knight2
etc.
aPiece.animIndex = 0


Or, use a for loop and use some naming convention to load all of your pieces consecutively. The real key here is to somehow use the timer to increment your animation index (animIndex) using the timer, so maybe in your paint event you could have (using the same code):
x = Ubound(mypieces)
for y = 1 to x
c = MyPieces(y)
d = MyPieces(y).animIndex
d = d + 1
if d > 5 then
d = 0
end if
if c.taken = false then g.DrawPicture c.Image(d),c.row * 32,c.col*32
MyPieces(y).animIndex = d
next


viola! Now your pieces will animate 10 times a second- not the most efficient graphics code, but it'll give you the general idea

_________________
Mojiferous Industries, now 300% more Mojifertastic!


Top
 Profile  
Reply with quote  
 Post subject: Re: Need Help with (2D) Sprites
PostPosted: Tue May 19, 2009 3:10 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
First, if you got a private message or e-mail from me, I may have messed up by pressing the wrong button in the topic thread (thus sending it to the wrong person or at least putting the wrong name at the top of the body of the message). If so, my apologies.

I was asked whether I was going to write a chess game. I'm not that smart. I just used chess, chessboard, and chess pieces as a simple example of what I wanted/needed to do with sprites. If I do do anything with chess, it will probably be writing a program to pose chess problems (with the solutions available within the program for those who need them).

The games I really have in mind are the Coney Games I wrote decades ago on the TI-99/4A home computer. Most of them are based on the timeless "Mathematical Recreations" columns by Martin Gardner published in Scientific American from back then. Here's what they share in common: (1) They all can be played "human vs. computer." (2) The rules are very simple. (3) If anything, the rules seem to favor the human player. (4) In spite of that, the computer always wins (or at least 90% of the time in some games).

One Coney Game does NOT use sprites, so I've already ported it over to REALbasic: Tic-Tac-Toe Philadelphia Style, which is a combination game and magic trick. The rules are slightly alterated (supposedly) to make Tic-Tac-Toe more interesting and more fair to the second player. And the "magician" demonstrates how easy it is to beat the computer (and always does so), BUT the "volunteer" from the audience always loses when he plays against the computer (and the two people may switch places after any particular game is completed).

All of the other Coney Games DO use sprites, so once I learn how to bring one over to REALbasic, I can bring them all over. I do intend to make them freely available (including the Project source code) to anyone interested, and I plan on posting a note in the Off-Topic forum letting people know where they can download them. In this way I want to share what I've learned here.

Barry Traver
(email address: opc@traver.org)


Top
 Profile  
Reply with quote  
 Post subject: Re: Need Help with (2D) Sprites
PostPosted: Wed May 27, 2009 7:57 am 
Offline

Joined: Fri Sep 30, 2005 1:53 pm
Posts: 914
Location: Philadelphia, PA
Thanks to everyone who posted (plus Tim Cool who sent an email) for all the comments.

I'm still in the process of digesting them all, but already I am able to have an object with an irregular outline (i.e., have a "sprite") move from one position to another without messing up the background. In addition I can "animate" that "sprite." If someone clicks on a square, I can also tell whether it is empty or what particular game piece it contains. That's a lot that I couldn't do a week or so ago.

Other than come up (or create) the graphics for appropriate game pieces, my only other objective at the moment is to figure out how to do "drag" and "drop" for the "sprites" (assuming it can be done). BUT that's a topic for a new thread, which I will start in a moment....

Barry Traver

P.S. In the "Off-Topic" area I'll post information on what I've done with what I've learned, i.e., where the source code can be downloaded for the game(s) or puzzle program(s) that are the result of this discussion (for those people who may be interested or curious).


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 20 posts ]  Go to page Previous  1, 2

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group