Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / How to make sprite wall collisions

Author
Message
Flemar
13
Years of Service
User Offline
Joined: 20th Apr 2011
Location:
Posted: 20th Apr 2011 14:39
Hey Guys

I am new to Dark Basic and I am currently making a 2D Beer catching game.

My biggest issue is that I am unable to make the sprites function as walls. And because of that, the player is able to walk right through them. I am trying to make sprite 7 stop when it hits Sprite 1 to 6.
this is the code:



Please Help.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 21st Apr 2011 01:45
Hi Flemar, welcome to the forums! Ok, first thing I found was that you have a couple of goto commands in there. So far they don't seem to be causing a problem but gotos are considered to be poor programming practise as it jumps from here to there and everywhere, which makes it very hard to follow as projects get larger and makes finding bugs a lot harder. So now that you have had a little expierence with the goto command I recommend that you should use it when there is no alternative. With your code there are heaps of alternatives such as using a do-loop and subroutines. A great alternative to the goto is the gosub command.

Here is your code rearranged using a do-loop and the gosub command. I also added a sync rate in there, most media programs benefit from one.


So try this and tell me if there is any difference to your original project.

Now for the wall problem. As I don't know what your images look like and their dimensions I can't help you too much but what I can do is demonstrate the concept of stopping at a wall. Here is a small code snippet. Basically, it checks for when the object is out of its bounds and moves it back in. Code is fairly simple.



Try to implement the concept of this into your game. Other than that good luck

A clever person solves a problem, a wise person avoids it - Albert Einstein
Flemar
13
Years of Service
User Offline
Joined: 20th Apr 2011
Location:
Posted: 25th Apr 2011 11:38
Thanks for the help. I am currently testing the code, and it is working like a charm. I only have one question. Could you tell me exactly where you define the size of the images, and how to do more than one?

But anyway. thanks for the help so far.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 26th Apr 2011 04:25 Edited at: 26th Apr 2011 04:43
Defining the size of your images is easy. Simply use the "image width", "image height" commands. You can also use the sprite width and sprite height commands as well.

Sorry, misread your post, thought you were asking How you get the image width and height. Where I define the width and height of images is before the loop as they only need to be defined once unless you're scaling, stretching your sprites, then it will need to be in a loop.

Quote: "how to do more than one?
"

When creating a game like this I would use User-defined types (UDTs) and I think you would benefit from using them as well.
I'll give an example of one.

This would be a type for a wall.
type wall
x as integer
y as integer
width as integer
height as integer
sprite_num as integer
endtype

Now let's say you have 4 walls you could make an array
dim bounds(4) as wall

now load your image
load image "yourimage.whatever", 1

Now, if all walls use the same image you can do this
for n = 1 to 4
bounds(n).width = image width(1)
next n

Using UDTs will also help with collision checking, sprite drawing etc and it also means you don't need to create a huge variable list for each object you want.

A clever person solves a problem, a wise person avoids it - Albert Einstein

Login to post a reply

Server time is: 2024-11-22 23:23:22
Your offset time is: 2024-11-22 23:23:22