moving and shooting
Author |
Message |
msimard8
|
Posted: Wed Jun 29, 2005 10:32 am Post subject: moving and shooting |
|
|
Hello
How do you move a object and shoot something out of it ...and have the bullet moving while your moving aswell.
ya i know that question may be hard to understand. The code is part of what I have done, and you will see what i mean. I freeze until the bullet stops moving.
code: |
setscreen ("graphics:649;479")
var gx, gy : int := 20
var key : array char of boolean
var ly : int := 20
var lx : int := 30
var weaponex : int := 370
proc background
drawfillbox (0, 0, 500, maxy, black)
end background
proc scoreboard
drawfillbox (501, 0, 649, 479, 12)
end scoreboard
%checks input keys
proc press
if key (KEY_UP_ARROW)
then
gy := gy + 1
ly := ly + 1
end if
if key (KEY_LEFT_ARROW)
then
gx := gx - 1
lx := lx - 1
weaponex := weaponex + 1
end if
if key (KEY_DOWN_ARROW)
then
gy := gy - 1
ly := ly - 1
end if
if key (KEY_RIGHT_ARROW)
then
gx := gx + 1
weaponex := weaponex - 1
lx := lx + 1
end if
if gx < -80 then
weaponex := 480
elsif gx > 389 then
weaponex := 0
end if
end press
%.........fireing bullet animation
proc gun
setscreen ("nooffscreenonly")
for c : 1 .. weaponex
scoreboard
background
drawfilloval (100 + gx, 100 + gy, 10, 10, red)
drawfilloval (100 + lx + c, 100 + ly, 2, 2, yellow)
setscreen ("offscreenonly")
View.Update
end for
end gun
%.......allows guy to move around and activates weapon
proc good
loop
background
scoreboard
delay (5)
Input.KeyDown (key)
drawfilloval (100 + gx, 100 + gy, 10, 10, red)
setscreen ("offscreenonly")
press
if key ('a') or key ('s') or key ('d') or key ('w')
then
gun
end if
View.Update
cls
end loop
end good
good
|
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Cervantes
|
Posted: Wed Jun 29, 2005 11:26 am Post subject: (No subject) |
|
|
This will require you to learn some new concepts.
Turing Walkthrough -> Arrays -> Flexible Arrays
In the flexible array tutorial, there is an example of this very thing.
It would also be good to know how to use records to keep things organized and to same you some code when you have to remove a bullet. |
|
|
|
|
|
msimard8
|
Posted: Thu Jun 30, 2005 2:43 pm Post subject: ? |
|
|
I dont understand how to incorperate flexible arrays and records to solve this problem
thanks |
|
|
|
|
|
Cervantes
|
Posted: Fri Jul 01, 2005 7:08 am Post subject: (No subject) |
|
|
Have you read my tutorial and looked at the source code?
Make a flexible array of bullets:
code: |
var bullet : flexible array 1 .. 0 of
record
x, y, vx, vy : real
end record
|
When you hit the fire button (via Input.KeyDown), incriment the flexible array.
code: |
new bullet, upper (bullet) + 1
bullet (upper (bullet)).x := player.x
bullet (upper (bullet)).y := player.y
%3 + player.velocity represents the velocity of the bullet
bullet (upper (bullet)).vx := cosd (player.angle) * (3 + player.velocity) bullet (upper (bullet)).vy := sind (player.angle) * (3 + player.velocity)
|
Notice how I added that 3 + player.velocity. Remember we're not shooting beams of light here.
Movement of the bullet is like such:
code: |
bullet.x += bullet.vx
bullet.y += bullet.vy
|
When you want to delete the bullet (it has collided with something or gone passed its range):
code: |
bullet (bulletToDelete) := bullet (upper (bullet))
new bullet, upper (bullet) - 1
|
See here we are copying the upper bullet to the position of the one we want to delete and then deleting the original upper bullet. If you want to keep the bullets in "order", you'll need a for loop:
code: |
for i : bulletToRemove + 1 .. upper (bullet)
bullet (i - 1) := bullet (i)
end for
new bullet, upper (bullet) - 1
|
Hope that helps |
|
|
|
|
|
|
|