CheatCat's picture

Rayman 2 2D

Table of contest:

The beginning
Drawing images
Player movement

------
The beginning

The background to this project is this YouTube video:
http://www.youtube.com/watch?v=LW-fpwjT9I0&feature=PlayList&p=A047FCF1E8...
I want to do a fan-game to this game because it was canceled before the game was done!

First I have to decide what I should do the game with..
CsGL http://csgl.sourceforge.net : The page hasn't be updated since 2003. I want something newer.
Allegro http://www.talula.demon.co.uk/allegro : Hard to install and you cannot use it for C#. There is wrappers, but they are old.
AgateLib http://www.agatelib.org : It's slow, I want something faster!
SDL.NET http://cs-sdl.sourceforge.net : The forum is dead, I can look in the sky for help..
OpenTK http... Wait, it is this page! xD : It seems to be a great wrapper, let us try! :D

------
Drawing images

First I run the QuickStart example and get a stupid MS-DOS window in front of the program and I ask for help: http://www.opentk.com/node/890

Now I try to make a simple game!
The first program I use was GameMaker. I just need to load a sprite/image, make a object for the sprite, put the object i a room and hit the play button. Then I have a cute window with the image. Simple.

I want do something harder so I switch to C++ with The Game Creators DarkGDK library (using DirectX). To draw a image to the screen I only need write this code:

#include "DarkGDK.h"
 
void DarkGDK()
{
  dbSyncOn();
  dbLoadImage("img.png", 1);
  dbSprite(1, 10, 20, 1);
 
  while(LoopGDK())
  {
     dbSync();
  }
}

And I have a image at the position 10x20 in a window.
I also try XNA and it was little harder to draw a image but it was something similar as DarkGDK. Of course I have tried other programs and libraries and you draw images with them calling simple functions.

Okey, back to OpenGL and OpenTK. To draw a image you first need to load the image. It's hard and I don't understand what the loading function do in the NeHe Examples (google it). However it load a image and it's all I need to know. I get help at http://www.opentk.com/node/899 and learned that I can use Objarni's TexUtil class to load images.

Now I try to draw the image and to do that you must put it on a sort of plain. You can read more at http://www.opentk.com/node/909 and http://www.opentk.com/node/919

At last I want to put a transparent image in front of another image. But the transparent image get a blue border and cut of the red rectangle! I search for help again.

Well, it is not a very big problem. Moving on..

------
Player movement

Okey, let's move a image! I want add some keyboard functions that check what key the user press down. It's time for search!

Get it, Keyboard[] checks the keys! I add a wall sprite and try to check collision between it and the player. A collision function would be great and it can be inverted by myself if I don't find a pre-done function.
I do the collision function by myself since I cannot find any other. Now I should make the player to fall down if there are walls under the player. Done! (I am almost done! xD) The game go very fast, I must find a way to change the framerate or maybe move the sprite with smaller steps.

It's time to make the sprite jump when pressing Ctrl. Then I can make a level!

I get collision error now: When the player collide with a wall it cannot move! I should try to fix this...


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
CheatCat's picture

I manged to do make the sprite jump anyway and then slow down and fall down anyway!

Well, it seems that must read up my knowledge about C# little... I think I know the protection levels (private is things that you can use in only one class and public can be used by all classes and private is... somthing.)

And I don't really understand vectors since it's hard to use them in C++..

the Fiddler's picture

You've got private and public correct. There's also protected, which means the property, field or method is only accessible inside the declaring class or its inheritors; and internal which works exactly like public, but limits visibility to the declaring assembly (dll).

Don't confuse mathematical vectors with C++ vectors, they have nothing to do with each other. A mathematical vector is a simple construct that has length and direction. Acceleration, velocity and position can all be represented by mathematical vectors, like the ones found OpenTK.Math.Vector[234]. Vectors form the basis of linear algebra (hence all 2d and 3d graphics) - it's a good idea to read a few tutorials on the subject.

C++ std::vector is a programmatic construct that represents an expandable array of items (unlike normal arrays, which have fixed size). In C#, the equivalent construct is called System.Collections.Generic.List<> and is very easy to use:

List<int> items = new List<int>();
items.Add(5);
items.Add(-100);
Console.WriteLine(items[1]); // Prints "-100"

Edit: just wanted to add that programming a simple game is a good way to become familiar with a new programming language. The more familiar you become with it, the better your code will become: don't hesitate to go back and rewrite stuff using newfound knowledge. Follow the once, twice, rewrite rule: if you write code to do something, it's ok; if you write the same code a second time, go back and abstract this code into a new method or class. The third time you'll need this code, just use this new method/class - your design just got better. :)

Oh, and there will come a time when you'll look at some code and say, "what was I thinking when I wrote this thing?" This is normal: it is the equivalent of gaining a level in programming skills. I've lost count how many times I've been through this in OpenTK.

CheatCat's picture

Thank you! If I want to store x, y positions and height and width for a sprite should I use a Vector4? xD Or maybe just a plain array?

I switch the game between 2 computers. At the first computer the player move very fast and on the other it move like a snail.. How can I change the speed? I tried to change the FPS but the sprite vibrate then..

objarni's picture

CheatCat: In GameWindow.UpdateFrame(Event event) you receive event data. If you look into that, you can find Time. You should use time in you physics calculations:

x = x + vx*event.Time;
y = y + vy*event.Time;

That way the animation should be the same regardless of computer system.

[Disclaimer: I don't have OpenTK beside me, so names might be wrong]

CheatCat's picture

Cool! :D Now I have manged to make the player collide! But it can jump at the sides of the wall..

Entropy's picture
the Fiddler wrote:

Oh, and there will come a time when you'll look at some code and say, "what was I thinking when I wrote this thing?" This is normal: it is the equivalent of gaining a level in programming skills. I've lost count how many times I've been through this in OpenTK.

Haha! I can really identify with that. I'm absolutely mystified at some of my earlier attempts...

Part of my difficulty was that before C#, I'd only ever used BASIC in various forms and FORTRAN 90 for my university work. My earliest attempts involved storing arrays of array-indices to reference information in static classes. Completely unreadable nonsense, when you're using an OO language...

CheatCat's picture

The big walljump problem:

I have a function that checks if the player are 1 pixel over a wall and allow it to jump if so. But if I put a wall above another then the player can jump from the under wall. You maybe don't know what I mean, I will try to draw it:


(You can jump in both cases)

How I fix so I only can jump when player really is on the wall and not beside it?

My "oversprite" function:

private bool rectcollide(float xmin1, float xmax1, float ymin1, float ymax1,
 
          float xmin2, float xmax2, float ymin2, float ymax2)
 
        {
 
          if (xmax1 >= xmin2 && xmin1 <= xmax2 && ymax1 >= ymin2 && ymin1 <= ymax2)
 
          {
 
            return true;
 
          }
 
          return false;
 
        }
private bool oversprite(int s1, int s2)
 
        {
 
          if(rectcollide(sprite[s1].x, sprite[s1].x + sprite[s1].w, sprite[s1].y, sprite[s1].y + sprite[s1].h,
 
            sprite[s2].x, sprite[s2].x + sprite[s2].w, sprite[s2].y - 1.0f, sprite[s2].y + sprite[s2].h))
 
          {
 
            return true;
 
          }
 
          return false;
 
        }
AttachmentSize
walljump.png7.19 KB
objarni's picture

You have to check if there is a wall just beneath the player.

Think of the player as only one point, beneath the center of his feet. At location (x+width)/2, y+height.

Move one pixel down. This point is an interesting point, call it the "support point" of the player.

Now if you think of the "top line" of each wall, it will be extending from wallX to wallX+wallWidth.

If the support point lies in any such "top line", the player can jump. If I understand your problem correctly.

So you need a function:

bool OnWall(int supportX, supportY, toplineMinX, toplineMaxX, toplineY)

CheatCat's picture

It works, thank you! :D But I need to change toplineY to toplineMinY and add a toplineMaxY argument.. But something have turn me down and I don't feel to do anything.. :(