need help with health code
Author |
Message |
ut2004
|
Posted: Thu Jun 12, 2008 2:15 pm Post subject: need help with health code |
|
|
When health = 0
I want it so that the program doesn't end but it disallows the user to move the character after hes dead. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
jeffgreco13
![](http://compsci.ca/v3/uploads/user_avatars/21290830414888dd996a022.jpg)
|
Posted: Thu Jun 12, 2008 2:29 pm Post subject: Re: need help with health code |
|
|
it is really impossible to help when all were given is "health = 0"...
and what exactly do u mean? you want the guy or w/e to not be able to move but other things are still going on? the user can still interact with the progrm? |
|
|
|
|
![](images/spacer.gif) |
ut2004
|
Posted: Thu Jun 12, 2008 3:06 pm Post subject: Re: need help with health code |
|
|
when my health intetger becomes 0 i want the guy to not be able to move but things are still going on |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Thu Jun 12, 2008 3:39 pm Post subject: RE:need help with health code |
|
|
You're really going to have to explain more, and probably post a lot more of your code. We have no idea how you've implemented input handling / character movement, so we can't offer advice without knowing more.
Post your main game loop or similar structure here so we can understand what it is you're trying to do. Make sure you use the syntax tags when you do so:
[ syntax="turing ]
code goes here...
[ / syntax ]
without spaces. |
|
|
|
|
![](images/spacer.gif) |
Tony
![](http://wiki.compsci.ca/images/f/f4/OniTony.gif)
|
Posted: Thu Jun 12, 2008 3:46 pm Post subject: RE:need help with health code |
|
|
exit the move procedure unless health > 0. |
Tony's programming blog. DWITE - a programming contest. |
|
|
|
![](images/spacer.gif) |
ut2004
|
Posted: Thu Jun 12, 2008 4:04 pm Post subject: Re: need help with health code |
|
|
Turing: |
loop
Input.KeyDown (chars )
%-------------------MONK MOVEMENTS------------------------------------------------%
%monk jump left
if y > GROUND_HEIGHT and chars (KEY_LEFT_ARROW) then
Pic.Draw (monkjumpleft, x, y, picMerge)
end if
%Go through platform
if chars (KEY_DOWN_ARROW) then
GROUND_HEIGHT := 20
end if
% to make the player move left
if chars (KEY_LEFT_ARROW) then
velx := -RUN_SPEED
dir := 10
monklll := monkleft
monkjumpup := monkjumpleft
if chars (KEY_LEFT_ARROW) and y = GROUND_HEIGHT then
Pic.Draw (monkleft, x, y, picMerge)
end if
% to make the player move right
elsif chars (KEY_RIGHT_ARROW) then
velx := RUN_SPEED
dir := - 10
monklll := monkright
monkjumpup := monkjumpright
if chars (KEY_RIGHT_ARROW) and y = GROUND_HEIGHT then
Pic.Draw (monkright, x, y, picMerge)
end if
%monk jump right
if y > GROUND_HEIGHT and chars (KEY_RIGHT_ARROW) then
Pic.Draw (monkjumpright, x, y, picMerge)
end if
else
velx := 0
end if
if y > GROUND_HEIGHT then
Pic.Draw (monkjumpup, x, y, picMerge)
end if
if y = GROUND_HEIGHT then
Pic.Draw (monklll, x, y, picMerge)
end if
% ---------------------------Jumping Monk-----------------
if chars (KEY_UP_ARROW) and y = GROUND_HEIGHT then
if Time.Elapsed - last3 > 720 then % delay after ever jump
vely := JUMP_SPEED
last3 := Time.Elapsed
end if
end if
%-------MONK PROJECTILES-------
if chars ('/') then
%Pic.Draw (monkatkleft, x, y, picMerge)
if Time.Elapsed - last > 800 then
new bullet, upper (bullet ) + 1
bullet (upper (bullet )).x := x - dir
bullet (upper (bullet )).y := y + 75
bullet (upper (bullet )).dir := dir / . 5
last := Time.Elapsed
end if
end if
for i : 1 .. upper (bullet )
if i <= upper (bullet ) then
bullet (i ).x - = bullet (i ).dir
%----BULLECT COLLSION on wiz DETECTION---------
if bullet (i ).x + 10 >= x2 and bullet (i ).y >= y2 and bullet (i ).y <= y2 + 100 and bullet (i ).x - 10 <= x2 + 40 then
health2 := health2 - 5
for t : i .. upper (bullet ) - 1
bullet (t ) := bullet (t + 1)
end for
new bullet, upper (bullet ) - 1
end if
end if
end for
for i : 1 .. upper (bullet )
drawfilloval (round (bullet (i ).x ), round (bullet (i ).y ), 10, 10, 80)
end for
%----------GRAV-----
% subtract your "gravity" constant from the velocity EACH frame
vely - = GRAVITY
x + = round (velx )
y + = round (vely )
% simple "collision" check. just makes sure the player stays above ground
if y < GROUND_HEIGHT then
y := GROUND_HEIGHT
vely := 0
end if
% simple "collision" check. just makes sure the player doesnt go past sides of screen
if x < WALLLEFT then
x := WALLLEFT
vely2 := 0
end if
if x > WALLRIGHT then
x := WALLRIGHT
vely2 := 0
end if
%-------------------WIZARD MOVEMENTS------------------------------------------------%
%wizard jump left
if y2 > GROUND_HEIGHT2 and chars ('a') then
Pic.Draw (wizjumpleft, x2, y2, picMerge)
end if
%Go through platform
if chars ('s') then
GROUND_HEIGHT2 := 20
end if
% to make the player move left
if chars ('a') then
velx2 := -RUN_SPEED2
dir2 := 10
wizrrr := wizleft
wizstand := wizjumpleft
if chars ('a') and y2 = GROUND_HEIGHT2 then
Pic.Draw (wizleft, x2, y2, picMerge)
end if
% to make the player move right
elsif chars ('d') then
velx2 := RUN_SPEED2
dir2 := - 10
wizrrr := wizright
wizstand := wizjumpright
if chars ('d') and y2 = GROUND_HEIGHT2 then
Pic.Draw (wizright, x2, y2, picMerge)
end if
%monk jump right
if y2 > GROUND_HEIGHT2 and chars ('d') then
Pic.Draw (wizjumpright, x2, y2, picMerge)
end if
else
velx2 := 0
end if
if y2 > GROUND_HEIGHT2 then
Pic.Draw (wizstand, x2, y2, picMerge)
end if
if y2 = GROUND_HEIGHT2 then
Pic.Draw (wizrrr, x2, y2, picMerge)
end if
% ----------------------------------------Wizard jump-----------
if chars ('w') and y2 = GROUND_HEIGHT2 then
if Time.Elapsed - last4 > 720 then % delay after ever jump
vely2 := JUMP_SPEED2
last4 := Time.Elapsed
end if
end if
%-------WIZ PROJECTILES-------
if chars ('c') then
if Time.Elapsed - last2 > 800 then
new bullet2, upper (bullet2 ) + 1
bullet2 (upper (bullet2 )).x := x2 - dir2
bullet2 (upper (bullet2 )).y := y2 + 75
bullet2 (upper (bullet2 )).dir := dir2 / . 5
last2 := Time.Elapsed
end if
end if
for i : 1 .. upper (bullet2 )
if i <= upper (bullet2 ) then
bullet2 (i ).x - = bullet2 (i ).dir
%----BULLECT COLLSION on Monk DETECTION---------
if bullet2 (i ).x - 10 <= x + 10 and bullet2 (i ).y >= y and bullet2 (i ).y <= y + 100 and bullet2 (i ).x + 10 >= x then
health := health - 5
for t : i .. upper (bullet2 ) - 1
bullet2 (t ) := bullet2 (t + 1)
end for
new bullet2, upper (bullet2 ) - 1
end if
end if
end for
for i : 1 .. upper (bullet2 )
drawfilloval (round (bullet2 (i ).x ), round (bullet2 (i ).y ), 10, 10, 20)
end for
%----------GRAV-----
% subtract your "gravity" constant from the velocity EACH frame
vely2 - = GRAVITY2
x2 + = round (velx2 )
y2 + = round (vely2 )
% simple "collision" check. just makes sure the player stays above ground
if y2 < GROUND_HEIGHT2 then
y2 := GROUND_HEIGHT2
vely2 := 0
end if
% simple "collision" check. just makes sure the player doesnt go past sides of screen
if x2 < WALLLEFT then
x2 := WALLLEFT
vely := 0
end if
if x2 > WALLRIGHT then
x2 := WALLRIGHT
vely := 0
end if
%---------------------------------Platforms-----------------------------------------%
%platform left
if x > 65 and x < 265 and y >= 325 then
GROUND_HEIGHT := 325
%------platform middle------
elsif x > 250 and x < 680 and y >= 170 then
GROUND_HEIGHT := 170
%------platform right------
elsif x > 660 and x < 905 and y >= 325 then
GROUND_HEIGHT := 325
else
GROUND_HEIGHT := 20
end if
%drawfillbox (posx - 10, posy, posx + 10, posy + 20, 12)
%if posxa >= maxx then
% posxa := 1
%end if
%if posxa <= 0 then
% posxa := maxx - 1
% end if
%------------platform left---------
if x2 > 65 and x2 < 265 and y2 >= 325 then
GROUND_HEIGHT2 := 325
%------------platform middle---------
elsif x2 > 250 and x2 < 680 and y2 >= 170 then
GROUND_HEIGHT2 := 170
%------------platform right---------
elsif x2 > 660 and x2 < 905 and y2 >= 325 then
GROUND_HEIGHT2 := 325
else
GROUND_HEIGHT2 := 20
end if
if health2 <= 0 then
health2 := 0
Pic.Draw (wizdead, x2, y2, picMerge)
wizright := wizdead
wizleft := wizdead
wizjumpright := wizdead
wizjumpleft := wizdead
Font.Draw ("Monk Wins", 700, 500, font3, brightblue)
end if
%-------------when monk dies---------
if health <= 0 then
health := 0
Pic.Draw (monkdead, x, y, picMerge)
monkright := monkdead
monkleft := monkdead
monkjumpleft := monkdead
monkjumpright := monkdead
Font.Draw ("Wizard Wins", 70, 500, font3, brightblue)
end if
end loop
|
|
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Thu Jun 12, 2008 8:05 pm Post subject: RE:need help with health code |
|
|
Assuming "monk" is the player, then what you want to do is in the block at the bottom.
You already have the code to detect "if health <= 0 then". All you need to do is exit the loop after you've done the picture-drawing and the "Wizard Wins" font-writing. You can modify the code to be as follows:
Turing: |
if health <= 0 then
health := 0
Pic.Draw (monkdead, x, y, picMerge)
monkright := monkdead
monkleft := monkdead
monkjumpleft := monkdead
monkjumpright := monkdead
Font.Draw ("Wizard Wins", 70, 500, font3, brightblue)
exit
end if
|
The "exit" line tells the program to leave your loop-end loop structure when it gets to that part. After your "end loop", you should have the cleanup / game-over code. |
|
|
|
|
![](images/spacer.gif) |
riveryu
![](http://compsci.ca/v3/uploads/user_avatars/196991102647ae2debe0bf0.jpg)
|
Posted: Thu Jun 12, 2008 8:41 pm Post subject: RE:need help with health code |
|
|
I dont think theres too much point reseting variables if you are going to exit right after, but then again I dont know what your planning after it. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Hendo
![](http://compsci.ca/v3/uploads/user_avatars/1049240745485a9912c2ba0.jpg)
|
Posted: Sun Jun 15, 2008 5:17 pm Post subject: RE:need help with health code |
|
|
or simply create a boolean variable and only move him if its true and when he dies set it to false |
|
|
|
|
![](images/spacer.gif) |
TheGuardian001
|
Posted: Sun Jun 15, 2008 7:57 pm Post subject: Re: need help with health code |
|
|
or you could just do
code: |
if chars(direction key) and health > 0 then
movement code
end if
|
that way it only does the move condition if they have at least 1 health left...
game keeps going but player cant move. |
|
|
|
|
![](images/spacer.gif) |
riveryu
![](http://compsci.ca/v3/uploads/user_avatars/196991102647ae2debe0bf0.jpg)
|
Posted: Sun Jun 15, 2008 8:52 pm Post subject: Re: need help with health code |
|
|
@TheGuardian001, I dont know but wouldnt the computer do less calculation if it just checks if health > 0 once, stores it in a boolean var and other ifs will just compare with that var.
e.g
code: |
loop
if health > 0 then
alive := true
end if
if alive and chars (key) then
movement
end if
end loop |
|
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Mon Jun 16, 2008 8:10 am Post subject: RE:need help with health code |
|
|
Not necessarily, riveryu. It really depends on the execution environment, compiler / interpreter, and so forth.
Depending on compiler, comparing (health > 0) may do something like:
LOAD health
JUMP-LESS-EQUAL health, 0, END-IF
... if-code ...
While something like assigning alive := true, then comparing to it may do the following:
JUMP-GREATER health, 0, END-IF
STORE health, 0
... intervening code ...
LOAD alive
JUMP-EQUAL alive, 0, END-IF
So if we assume some fairly optimal code, then memorizing a single comparison isn't necessarily a good plan. You'd be correct if it was calling any methods (method-call overhead), or was doing any actual arithmetic, but a simple compare probably doesn't need it, speed-wise.
That said, using "alive" instead of "health > 0" may be more readable, and is probably easier to change later, if the conditions on "alive" ever change (maybe the player has some equipment that lets them stay alive to -10 HP?).
Oh, and it's in Turing, so it's fairly unclear what's *actually* faster. Truth is, if you want it to execute quickly, you aren't writing Turing in the first place. The only language I've ever heard of being slower than Turing is Javascript, and then only when the implementation is bad . |
|
|
|
|
![](images/spacer.gif) |
|
|