making a easy move sprite/brush routine

Find quick help here to get you started with Hollywood
Post Reply
User avatar
Juan Carlos
Posts: 932
Joined: Mon Sep 06, 2010 1:02 pm

making a easy move sprite/brush routine

Post by Juan Carlos »

I'm working in a shot game and I have several doubts of How make a routine to move sprites/brushes in the screen, and the use of If collision and the BeginDoubleBuffer if it is need, with the examples I don't understand how make iot, I only make the shot to targets not move, with If Collission although I could see that the x coords I haven add 10 extra pixels to mouse pointer and the target are join.
I need some idea how make the easy routine to move a sprite/brush and collison it with the mouse pointer or other background object, tables, etc.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: making a easy move sprite/brush routine

Post by Bugala »

Not entirely sure what you were after, but here are couple of codes from head, which means that i might remember some commands bit wrong.

First brush that moves with mouse pointer:

Code: Select all

loadbrush 1, "filename.png"
begindoublebuffer

repeat
x = mousex()
y = mousey()

displaybrush(1, x, y)
flip
CLS  #BLACK

forever
This code will display a brush always at position where mouse pointer is.

What happens in code. First loads brush number 1, then starts double buffer (notice that you have to start double buffer before loop, or it wil lstart it again and again, and hence crashing. So first start doublebuffer, then go to loop).

at start of loop it takes mousepoitners x and y cordinates, then uses those cordinatres to display brush number 1 to same place.

However, at this point it doesnt really draw anything yet, since double buffer is activated.

But command "Flip" means that it will draw all the stuff so far drawn, and at that point you will see what is actually happening.

Right after flip comes CLS #BLACK, which clears screen to black color. Once again, you wont see this, since it wont show this until next flip comes.

To see difference without double buffer, you can remove both double buffer and flip command from code, and notice how brush keeps blinking. that is because everytime something is drawn, it will show it. In this case it will first clear screen black and show it, then draw that brush to its place, and then again clear the screen and show it...



next code:

Code: Select all

player_x = 100
player_y = 200

loadbrush 1, "filename.png"
begindoublebuffer

repeat
down = iskeydown("DOWN")
up = iskeydown("UP")

if up = 1 then player_y = player_y - 1
if down = 1 then player_y = player_y + 1

displaybrush(1, player_x, player_y)
flip
CLS  #BLACK

forever
Very similar to previous code, except instead of drawing brush to where ever the mouse is, it will now be moved by arrows.

At start we first tell where player starts from, which are x-coord 100 and y-coord 200.

When we reach the loop (repeat - forever) it wil lat first check if UP or DOWN arrows are pushed down. If they are, they will report 1 (true), if not, then they will report 0 (not true). Hnece we can use this in following way.

variable "up", will get value of werher UP-arrow key is pushed down or not, same happens to variable "down" with DOWN-arrow.

After that we can simply check that if "up" is 1 (true) then y = y - 1. Notice that when you move up, y value gets smaller, and when down, y value gets bigger.

after this we simply display brush to current player_x and player_y coords.

In practice you now have player character that keeps moving up and down, but stays still on left-right movement.



then simple bullet code added to previous code: (new lines marked with arrows)

Code: Select all

player_x = 100
player_y = 200

loadbrush 1, "filename.png"
---> loadbrush 2, "bullet.png" <---
begindoublebuffer

repeat
down = iskeydown("DOWN")
up = iskeydown("UP")
--->fire = iskeydown("SPACE")<---

if up = 1 then player_y = player_y - 1
if down = 1 then player_y = player_y + 1

--->if fire = 1 and bullet=0
   bullet=1<---
   bullet_x = player_x
   bullet_y = player_y
endif

if bullet=1
   bullet_x = bullet_x + 10
   if bullet_x > 1000 then bullet=0
endif<---

displaybrush(1, player_x, player_y)
--->if bullet=1 then displaybrush(2, bullet_x, bullet_y)<---
flip
CLS  #BLACK

forever
What happens here now is that first of all it is checking if SPACE-button is down and record this to variable "fire"

Next it will check if "fire" is 1 (true) and in additio to this, it will also check that bullet is 0. this is our own variable, 0 meaning bullet can be fired, 1 that bullet is already on its way. With this system, we can make sure there is only one bullet flying on screen at once.

If bullet was 0 (ready to be fired) and space was pressed down, then we will move to actual laucnhing of bullet.

At this point Bullet_x and Bullet_y will be given to be same as players, making it that way seem like player is shooting that bullet. At that same we are also going to change bullet to 1, meaning that next bullet cant be shot until this bullet have ran its course.

After this, it will check if bullet is 1 (currently on its way), and if it is, then it will execute these two lines.

First adds 10 to its current x (making it move towards right) and after that checking if x is already higher than 1000, which could mean that depending upon your resolution, it have already flew out of screen. In situation like this, bullet variable is changed back to 0. Which means that next bullet can be shot again.



As last, piece of code about collision detection:

Code: Select all

col = collision(#BRUSH, 2, bullet_x, bullet_y, 3, enemy_x, enemy_y)
if col = 1
    bullet = 0
    enemystatus = 0
endif
This how it at its simplest goes.

Idea is that variable "col" gets the result of collision. In this case it is collision between brush number 2 (bullet) and brush number 3 (supposedly enemy brush)
if col is then 1, then that means that collision happened and following things happen:
bullet is changed to 0, so new bullet can be shot
and variable "enemystatus" is changed to 0, meaning that it is dead (this is once again similar to our "bullet" variable, that we have ourselves decided that 0 represents dead and other numbers something else, like 1 could be that it is alive)

Notice one thing here. When you do collision check, Hollywood doesnt actually look at screen wether these two collide or not. But it is doing virtual check on collision. That if brush 2 would be diplayed to this x and y, and brush 3 would be placed to this x and y, then would they collide.

That is also the reason why you need to tell the coordinates of those bruhses, instead of just the names/numbers of brushes to be checked, since Hollywood never checks collisions based upon how they are on screen, but does virtual check so to say.



Hope this helps you at least something.
User avatar
Juan Carlos
Posts: 932
Joined: Mon Sep 06, 2010 1:02 pm

Re: making a easy move sprite/brush routine

Post by Juan Carlos »

Thanks Bugala for your examples and explanations, I'll text it because I want make a mallet&moles game where the moles will be the spanish corrupt politicians, to avoid get the spanish and european money.
Again thanks for your help.
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: making a easy move sprite/brush routine

Post by Bugala »

Okay, sounds fun.

And seems then you just take mousex() and mouse(y) and display mallet brush on those coordinates as i explained there. But to add one thing you might want to do is to turn mouse pointer off by using: hidepointer(), and naturally ShowPointer() to show it again.

Anotehr possiblity is that you change the mouse pointer to that mallet itself, which is posible too with Setpointer(id) and CreatePointer() (check manual for more instructions for this command), but this has some draw backs too, main one being that Tablets dont use mouse pointers, and hence it wont work on any tablet computer, while using HidePointer() and moving brush to mouses x and y cordinates works fine with Tablet computers as well.


What comes to moles themselves, by quick i would imagine that I would use 4 different states for them, in way of mole.status = number.

0 would mean that mole is not existing at all at that time. 1 Would mean it is peeking from hole, but not hittable yet, 2 is that it is hittable, and 3 that it have ben hit and is dieing right now.


Reason for using these different states is because when you do that collision check, it will just check wether mallet and mole collide together, but if mole is only peeking yet, or is already dieing, then you probably dont want that mole to actually get hit, hence the collision code would go something like this:

Code: Select all

mole = {}
mole[1] = { x=100, y = 200 }
mole[2] = { x=200, y = 300 }


function p_collisioncheck()
for n = 1 to numberofmoles
   col = collision(#BRUSH, "mallet", mallet_x, mallet_y, "mole"..n, mole[n].x, mole[n].y)
      if col = 1 and mole[n].status = 3
          then do that hitting stuff
      endif
next
endfunction
User avatar
Juan Carlos
Posts: 932
Joined: Mon Sep 06, 2010 1:02 pm

Re: making a easy move sprite/brush routine

Post by Juan Carlos »

Again thanks Bugala for your help, I have get free time to test your examples, my current problem now, and yes the change the mouse pointer with setpointer, I have done with the mallet and two images it works, and with If Collision give the blow to the head of spanish corrupt politician.
Post Reply