Jump to content
Xtreme .Net Talk

Recommended Posts

Posted (edited)

Update:

 

- Down arrow makes falling shape fall faster (thanks for suggestion hog)

- A score + bonus is now displayed (thanks for suggestion hog)

- If you win, your time and score is now displayed.

- FPS bottleneck fixed, so game should run at approx. 50 fps (thanks pjv for help)

- Game text is now formatted.

- Fixed minor bug with fading blocks.

- Updated help file.

 

Some notes:

 

- This is not a Tetris clone. It's a Tetris style game. To learn how to play go to the Help menu. To start a game, go to File/New Game.

- The game is written in pure C# and GDI+. Nothing fancy was used (including no API).

- The game is complete.

- There are no known bugs (unless you want to include a minor FPS problem which is described below).

- I know the game isn't all that fun, but it's my first game and I needed to complete it so I could learn.

- You can run the game by either compiling it or running the .exe under Bin/Release.

- If you find any bugs or have any improvement suggests, please let me know.

- Feel free to tell me what you think. You don't have to be nice, I have a pretty thick backbone. So if you think it sucks, say so.

 

Okay now that that's out of the way... I could use some help if anyone would like to help me out.

 

I have the game set to run at 50 fps, but it doesn't run at that speed on my 800mhz system (I get a solid 21-25 fps). I can set the game speed to just loop with no control and get 50 fps, but I don't want to do that since it's important that everyone gets the same speed. If anyone would like to look at the code and possibly find out why (other then GDI+ overhead) it'd be appreciated. Also, if anyone would like to tell me what FPS they get and their system specs it'd help (to show FPS look under the debug menu).

 

There's also the problem of my overall design. While designing the game on paper (class hierarchy and all that), it looked good. Unfortunately it started to fall apart near the end (as you can probably see just by looking at it). I could definitely use some help with my designing techniques.. maybe some dos and don'ts? Feel free to bash my current design and tell me what's wrong with it, I only learn by example.

 

Thanks.

blockingelements.zip

Edited by wyrd
Gamer extraordinaire. Programmer wannabe.
Posted

Smooth.....very smooth :) I like it, only comments at first are; would be nice if the blocks move down faster when you press the down arrow, and to see a rolling score total. Apart from that....very smooth well done:)

 

I'm currently doing the same learning thingy with a tetris clone type game. I'll post it when I'm done.

My website
Posted (edited)

Nice game (and code).

 

FPS problem is a tricky one.. tried changing it to:

 

		while (_mode != GameModes.End) {
			// Check if game needs update.
			if (Environment.TickCount > nextTime) {
				// ** BEGIN PJV ADDED **
				int beforeTime = Environment.TickCount;
				// ** END PJV ADDED **

				// Update game.
				_update();

				// Get next update time.
				//nextTime = (1000 / GameSpeed) + Environment.TickCount;

				// Check second update.
				if (Environment.TickCount > nextSecond) {
					// Increase elapsed time.
					_elapsedTime++;

					// Display fps.
					if (_fps) {
						_blockSurface.DisplayText = "FPS: " + fpsCount;
					}

					// Reset fps.
					fpsCount = 0;

					// Get next second.
					nextSecond = 1000 + Environment.TickCount;
				}

				// Update fps.
				fpsCount++;

				// Process game updates.
				Application.DoEvents();

				// ** BEGIN PJV ADDED **
				int frameTime = Environment.TickCount - beforeTime;
				nextTime = Environment.TickCount +
						   ( ( 500 / GameSpeed ) - frameTime );
				// ** END PJV ADDED **
			}
		}

 

..but for some reason I still need to do the "500/GameSpeed" instead of 1000 to get 50 fps. Weird.

 

The only thing I can think of to improve the design would be to make use of delegates in the update function (instead of the switch statement).

 

Pete

Edited by pjv
Posted

Smooth.....very smooth I like it, only comments at first are; would be nice if the blocks move down faster when you press the down arrow, and to see a rolling score total. Apart from that....very smooth well done

 

I'm currently doing the same learning thingy with a tetris clone type game. I'll post it when I'm done.

 

Hmm.. I was going to add the down arrow but didn't for some reason. *ponders* Maybe I forgot. I'll add it.

 

.. can't wait to see your Tetris game. :)

 

I get 33 FPS on a P4 2.4 processor and 512 RAM.

 

Hmm.. seems like there isn't to much difference in speed.

 

..but for some reason I still need to do the "500/GameSpeed" instead of 1000 to get 50 fps. Weird.

 

The only thing I can think of to improve the design would be to make use of delegates in the update function (instead of the switch statement).

 

If you look up in the constants you'll see a GameSpeed constant which is set to 50. Set it to 1000 and it'll go as fast as possible. But that leads to uncontrolled speeds which I don't like.

 

There are lots of places for delegates.. but I read their performance was slow. I suppose I could give 'em a shot anyway to see just how much slow down they cause.

Gamer extraordinaire. Programmer wannabe.
Posted

If you look up in the constants you'll see a GameSpeed constant which is set to 50. Set it to 1000 and it'll go as fast as possible. But that leads to uncontrolled speeds which I don't like.

 

Yes but when I use the slight alteration I posted above (and change to 500 / GameSpeed -- not changing GameSpeed itself) then I get a steady 50fps (which I assumed was what you wanted). The only question is why doesn't it work with 1000 instead of 500.

 

Pete

Posted (edited)

Ah, I see. I'll take a look. I would imagine changing the GameSpeed to 100 would have the same effect. *ponders* My math is screwy somewhere.

 

EDIT:

Okay I found a bottleneck (there are probably others). Looks like you were on the right track with this one.

 

All the calls to Environment.TickCount were majorly slowing the loop down. I went ahead and added a variable which is updated once a loop, then I just use that variable as the tick count for the loop. It increased the FPS on my machine by about 20. The FPS still dips by about 10 when there are a lot of blocks on the screen.. anyone have suggestions to improve that?

 

I'll post the updated code when I'm done making other changes.

Edited by wyrd
Gamer extraordinaire. Programmer wannabe.
Posted

There is something very unintuitive about the game play that I'm not sure I can fully place. I do have a suggestion for improvement though.

 

Once a block is in the corner, it's done for. There's no way to get rid of it. And, of course, accidental maneuverings will sometimes cause more blocks to get next to it, in turn becoming indestructible. There should be some kind of mechanism to get rid of these blocks if they are unwanted. Perhaps a special block that destroys all surrounding blocks or something.

 

P4 2.6GHz Hyperthreaded with 512 MB DDR runs at very steady 33 FPS. Also takes up 50% of the processor. I'm sorry I didn't bother looking at the code, but there probably needs to be some System.Threading.Thread.CurrentThread.Sleep() commands placed in a few places. I have a feeling the program eats up the processor just waiting for its turn to do something.

 

Can I do something? No... Ok... How about now? No... Ok... How about now? No... Ok... How about now? No... Ok... How about now? No... Ok... How about now? No... Ok... How about now? No... Ok...

Posted

Game updated with changes that were suggested (view original post for details + updated d/l). If anyone finds a bug in the new version please let me know. :)

 

reanjr:

There is something very unintuitive about the game play that I'm not sure I can fully place. I do have a suggestion for improvement though.

 

Hmm.. yeah. Maybe now that the game keeps track of a score I'll allow the user to use up points to do special things (like click on a block to destroy it). Special blocks are good, but then that adds more random elements of luck into the game (another random element of luck wouldn't hurt at this point I suppose). I'll keep it under consideration, although I'm not exactly sure how I'd implement special blocks.

 

P4 2.6GHz Hyperthreaded with 512 MB DDR runs at very steady 33 FPS. Also takes up 50% of the processor. I'm sorry I didn't bother looking at the code, but there probably needs to be some System.Threading.Thread.CurrentThread.Sleep() commands placed in a few places. I have a feeling the program eats up the processor just waiting for its turn to do something.

 

I responded to your previous post about my game loop.. but I'll respond here as well. System.Threading.Thread.CurrentThread.Sleep() does not exit. System.Threading.Thread.Sleep() however does, but it doesn't process messages from the form.. which makes it so the form freezes.

 

If you have any other suggestions I'm all ears. In the mean time I'll continue to look over the game and see if I notice anything.

Gamer extraordinaire. Programmer wannabe.
Posted

Huh... that's odd. In VB, CurrentThread.Sleep exists, but in C# it doesn't seem to.

 

Oh... if the app isn't already multithreaded, you should try to do that. Two or three threads (maybe one for user input/form, one for main game loop, and one for drawing) in conjunction with some ThreadPriority tweaking could increase performance greatly.

 

If you're not familiar with doing multithreaded development it can be a bit of a bear to grasp, but it is well worth it.

 

For instance, the actual game loop I imagine is mostly waiting on its frame incrementer (whatever tells it it's time to do a new frame). If that had its own thread, it could be told to sleep for 10 milliseconds or before checking to see if it's ready to do something (which I imagine takes far less than 10ms). And/or it could be set to a low thread priority, giving the Drawing loop more time to do its thing when it needs to.

 

I've never done multithreaded development in C#, so I'm not quite sure how to go about doing it, but judging from VB, it's very clean, it just introduces some new complications. Oh, and System.Threading.Mutex is your friend (it took me a while to figure out that existed, meanwhile I was writing my own)

Posted
I've only done threading for a school project (which was in Java). I'll look into it if I have some time.. especially since I'm curious as to what kind of performance it'll give.
Gamer extraordinaire. Programmer wannabe.
Posted

hog:

 

It's at the top of the page?

 

Wyrd:

 

Cool.. all you need now is a funky background pic and a better help dialog -- something like several screens with pics/animation rather than just a textbox. With all that then it would look quite professional.

 

Pete

Posted

hog:

I replaced the original link at the top of the page with the updated copy.

 

pjv:

Any suggestions for the background?

Gamer extraordinaire. Programmer wannabe.
Posted

Hmm.. now do you mean a background for the background or for the windows (the squares that display info) or for both?

 

I still have no idea what type of background to make, heh.

Gamer extraordinaire. Programmer wannabe.
Posted

I got the game running at a steady 50 fps, regardless of how many blocks are on the screen (thanks to Hamburger1984 for the help). In general, using a TextureBrush w/ graphics.FillRectangle() instead of a Bitmap w/ graphics.DrawImage() increased the FPS by about 10. Amazing :eek:

 

Since I can no longer edit my post, I attached the updated game below (no real reason to d/l it unless you were having FPS difficulties with the last version).

 

Bad news about the background. Thanks to my absolutely cruddy design I can't add a background without taking a significant FPS hit or redesigning the whole game engine. I made some bad mistakes, but that's okay, it's all a learning experience for the next game I make.

 

Thanks all for the help and ideas to improve the game. I think this is about all the work I'm going to put into it unless someone finds a bug worth fixing. The game is complete and has a steady ~50 FPS. It's time to move on to the next game.

blockingelements.zip

Gamer extraordinaire. Programmer wannabe.
Posted

Wryd, I'm going for making a non rectangular window for my game. As my website is about pigs......obviuosly I have decided on a pig:)

 

I have attached the .exe file which shows it in full pig glory. Perhaps rather than put a bacground image on your form, why not funk the form itself up:):)

 

DOH......how do ya attach a zip file:confused:

My website
Posted
Don't use the quick reply box if you want to attach something, click the "post reply" button in the lower right hand corner of a post. You'll see an attachment option on there. ;)
Gamer extraordinaire. Programmer wannabe.
Posted

Heh sounds interesting. :) I'll take a look when I get some time. I don't have .NET on this computer.

 

I'll get back to you tomorrow. ;)

Gamer extraordinaire. Programmer wannabe.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...