Question on animation/games

LostProgrammer

Well-known member
Joined
Jan 17, 2003
Messages
123
Well last night I made a tic tac toe game, works like it is supposed to and the AI seems to work like it should. I guess I would like to try a new game and I was thinking of doing tetris. My question is where should I start? I dont have a clue on how to setup a animation on a program. I think if I knew what kind of object .net uses to do animation I might beable to slowly figure this out. Also my problem might be I just dont have a clue on how animations work in programs :D Anyways sorry to bother you all with my silly questions. Hope everyone is having a good monday!

Take care

LP
 
Programs with animation, such as screensavers and games, tend to run in a main loop, using as much CPU as possible to render as many FPS as possible. In many ways theyre easier to program than event-driven programs since your program never goes idle, and never leaves the loop.

Basically you loop until a boolean contition changes, then the program ends. Somewhere in your loop you check for, say, a keypress to set that condition and the loop will finish. Rendering graphics depends whether youre using GDI+ or DirectX, but either way you will usually draw a frame every iteration of the loop.

To do animation without DirectX, you use the GDI+ drawing methods in the System.Drawing namespace. Combined with the Windows Forms double buffering, this can produce a nice result. Every iteration of the loop you call Invalidate() then Application.DoEvents() and your main form gets repainted, and everything is drawn where it should be.
 
Look into FSM (finite state machines) they are basically a coding structure that allows you to manage a games state, that being a piece is falling or new level loading. alternatively, you could try and adopt an OO approach that has events in each object...
 
As far as the graphics of tetris are concerned, you do not NEED to redraw every frame, just use a graphics object to erase where the tetris or line was, and redraw it at its new location, saving alot of cpu power and maintaining that familiar event driven program structure.
 
Saving CPU power for what? Why worry about tracking when the screen needs to be redrawn? Games dont follow the event driven paradigm unless theyre turn-based (and sometimes not even then). An OO approach wouldnt be bad and isnt necessarily the same thing as event driven.
 
Id have thought that it would be easier to write Tetris using an event-driven model rather than a main loop. Could you set up a timer event, say every 1/2 a second and have an event handler that moves the block down the screen. You could also have keyboard event handlers. When a key to move the block left or right is pressed, these would be triggered and it would store the key pressed, and when the timer event is next triggered the block would move left or right as well as down.

As the game progressed and got faster, all you would need to do is to reduce the interval of the timer events.
 
This is exactly the way I have my tetris clone working, see BloX on my site. As my book says using a timer in this case doesnt really matter as we are not after lightening speed, so a timer does the job.

Same goes for trapping user input:)
 
Back
Top