Jump to content
IGNORED

"objects"


meshounah

Recommended Posts

hello i am producing a paintball game and i have my playfield designed with little hay-bales but i would like to make it that those hay-bales restrict movement and block missiles. how would i do this?

 

 

  playfield:
................................
.....X..................X.......
.............X..............X...
.....X..........................
.........X............X.........
....X.........X.............X...
................................
..............X.................
...X...................X........
.....X..........X...............
..........................X.....
................................
end

player0:
011100
011000
011000
100000
%01011010
%01111100
100100
010000
011000
111100
011000
end

x=50
y=50

main
COLUP0=28
COLUPF=90
COLUBK=160

player0x=x
player0y=y

drawscreen

if joy0right then x=x+1
if joy0left then x=x-1
if joy0up then y=y-1
if joy0down then y=y+1

goto main

Link to comment
Share on other sites

i would like to make it that those hay-bales restrict movement and block missiles. how would i do this?

 

There are a few ways you can better optimize your current code, and then you will also need to add some things to accomplish what you want to do. First, the optimizations... (You should get in the habit of learning and then using different ways to optimize your code while your program is still small, since it's more of a chore to modify larger programs than smaller programs.)

 

Original code:

 

   x=50
  y=50

 

   player0x=x
  player0y=y

 

   if joy0right then x=x+1
  if joy0left then x=x-1
  if joy0up then y=y-1
  if joy0down then y=y+1

 

In your current code, you're using x and y as temporary values to hold player0's x and y coordinates, and increasing or decreasing x and y depending on the joystick input. This is actually a waste of two bB user variables (x and y), because it's redundant (player0x and player0y are bB "system variables," or two bytes of RAM that bB uses for a predetermined purpose). Instead, you can simply set and then modify the player0x and player0y variables directly, as follows:

 

   player0x=50
  player0y=50

 

   if joy0right then player0x=player0x+1
  if joy0left then player0x=player0x-1
  if joy0up then player0y=player0y-1
  if joy0down then player0y=player0y+1

 

This has two benefits-- (1) it leaves the two user variables x and y free for something else, and (2) it lets you omit the unnecessary steps of setting player0x to x, and setting player0y to y (which saves a few bytes of ROM).

 

Furthermore, you can save another few bytes of ROM by putting the following statements on the same line of code (separated by a colon), because bB will recognize that both variables are being set to the same value, and it will compile them in a manner that's more compact:

 

   player0x=50:player0y=50

 

Another optimization can be achieved by rearranging the following lines of code:

 

main
  COLUP0=28
  COLUPF=90
  COLUBK=160

 

The statements to set the playfield color and background color don't need to be inside the loop, since those colors will keep their values once you set them. However, you *do* need to set the player0 color inside the loop, since bB overwrites the player0 and player1 colors when it displays the score at the bottom of the screen using the scorecolor (because the score is drawn using player0 and player1, so bB will change the player0 and player1 colors to the scorecolor just before it displays the score, and it will *not* set them back to their original colors afterward).

 

The new order of the statements would be as follows:

 

   COLUPF=90
  COLUBK=160

main

  COLUP0=28

 

This might not seem like much of a change, but it will save some precious machine cycles in the loop, since you don't need to waste machine cycles reperforming any statements that can be performed just once. Right now your loop has plenty of machine cycles to spare, but as your code starts to grow, you might run out of machine cycles, and then you would need to find ways to eliminate any unnecessary steps so you can reclaim machine cycles for doing other things. It's better to keep things tight from the beginning, to get a head start on freeing up as many machine cycles as possible.

 

Thus, the newly optimized version of your code would be as follows:

 

   playfield:
  ................................
  .....X..................X.......
  .............X..............X...
  .....X..........................
  .........X............X.........
  ....X.........X.............X...
  ................................
  ..............X.................
  ...X...................X........
  .....X..........X...............
  ..........................X.....
  ................................
end

  player0:
  011100
  011000
  011000
  100000
  %01011010
  %01111100
  100100
  010000
  011000
  111100
  011000
end

  player0x=50:player0y=50

  COLUPF=90
  COLUBK=160

main

  COLUP0=28

  drawscreen

  if joy0right then player0x=player0x+1
  if joy0left then player0x=player0x-1
  if joy0up then player0y=player0y-1
  if joy0down then player0y=player0y+1

  goto main

 

Now, on to answering your question... :)

 

To make the playfield blocks act as objects that get in the way of the sprites' movements-- as opposed to simply being blobs of color on the screen that the sprites can pass through-- you need to check for collisions, and then take appropriate action whenever specific types of collisions are detected.

 

To stop a player when it hits a bale of hay (or playfield pixel), the easiest approach is to save the player's previous x and y coordinates, and then if the player collides with a playfield pixel, set the player's x and y coordinates back to what they were before you moved the player. And the simplest way to do that is to use two user variables to hold the direction that the player is moving in-- one user variable for the change in the x direction, and another user variable for the change in the y direction. You will need to start by setting them both to 0, and then set them to either 1 for an increase, or 255 (which is equivalent to -1 in one-byte numbers) for a decrease, as follows:

 

   x=0:y=0
  if joy0right then x=1
  if joy0left then x=255
  if joy0up then y=255
  if joy0down then y=1

 

You'll need to do this *before* you draw the screen, update player0x and player0y by adding these x and y "direction values" to them, and *then* draw the screen, as follows:

 

   x=0:y=0
  if joy0right then x=1
  if joy0left then x=255
  if joy0up then y=255
  if joy0down then y=1

  player0x=player0x+x
  player0y=player0y+y

  drawscreen

 

This way, if the joystick isn't being pushed in any direction, then x and y will be 0, and player0x and player0y will stay the same. Otherwise, x and y will be set depending on which way the joystick is being pushed, and player0x and player0y will be updated accordingly, then the screen will be drawn using the new player0x and player0y values.

 

Next, you'll need to check for a collision between player0 and the playfield. This needs to be done *after* drawing the screen with the new player0x and player0y vcalues, because in order to detect a collision, you must first allow the collision to occur (makes sense, right?), and collisions occur when two things overlap each other as the screen is being drawn.

 

Furthermore, when a collision *does* occur between player0 and the playfield, you'll need to reset player0x and player0y to their previous values, by "undoing" the addition of the x and y direction changes (i.e., subtracting the changes to reverse the effects of having added them). You'll need to do this right away, *before* looping back to where x and y get set to new values based on the joystick:

 

   drawscreen

  if collision(player0,playfield) then player0x=player0x-x:player0y=player0y-y

  goto main

 

So the updated program will look like this:

 

   playfield:
  ................................
  .....X..................X.......
  .............X..............X...
  .....X..........................
  .........X............X.........
  ....X.........X.............X...
  ................................
  ..............X.................
  ...X...................X........
  .....X..........X...............
  ..........................X.....
  ................................
end

  player0:
  011100
  011000
  011000
  100000
  %01011010
  %01111100
  100100
  010000
  011000
  111100
  011000
end

  player0x=50:player0y=50

  COLUPF=90
  COLUBK=160

main

  COLUP0=28

  x=0:y=0
  if joy0right then x=1
  if joy0left then x=255
  if joy0up then y=255
  if joy0down then y=1

  player0x=player0x+x
  player0y=player0y+y

  drawscreen

  if collision(player0,playfield) then player0x=player0x-x:player0y=player0y-y

  goto main

 

There are other changes you'll probably want to make, such as preventing player0 from moving too far left, right, up, or down, but I won't get into that yet, since I don't know how far you want to let player0 move in those directions. You can also set player0 to a reflected image if it's moving left; but again, I won't get into that just yet.

 

Michael

Edited by SeaGtGruff
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

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.

Loading...
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...