May 05, 2024, 01:02:57 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


variable values gone mad in larger program

Started by yujinwunz, December 13, 2008, 12:47:05 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

yujinwunz

December 13, 2008, 12:47:05 AM Last Edit: December 13, 2008, 12:55:59 AM by yujinwunz
For Whoever might this interest, i have this very perculiar problem where an integer variable array named l(5) becomes in sync with another variable, lr(5). this caused bugs. it might just be my compiler. if anyone could compile the following code for me:

IF CREATESCREEN(800,600,32)<0
MESSAGEBOX 0, "error creating screen","error"
END
ENDIF

CONST DIK_ESCAPE = 0x01
CONST DIK_1 = 0x02
CONST DIK_2 = 0x03
CONST DIK_3 = 0x04
CONST DIK_4 = 0x05
CONST DIK_5 = 0x06
CONST DIK_6 = 0x07
CONST DIK_7 = 0x08
CONST DIK_8 = 0x09
CONST DIK_9 = 0x0A
CONST DIK_0 = 0x0B

CONST DIK_RETURN = 0x1C
CONST DIK_SPACE = 0x39 'shoot!

CONST DIK_UP = 0xC8 /* UpArrow on arrow keypad */
CONST DIK_LEFT = 0xCB /* LeftArrow on arrow keypad */
CONST DIK_RIGHT = 0xCD /* RightArrow on arrow keypad */
CONST DIK_DOWN = 0xD0 /* DownArrow on arrow keypad */

CONST DIK_W = 0x11 'second player
CONST DIK_A = 0x1E
CONST DIK_S = 0x1F
CONST DIK_D = 0x20


DEF ret as INT
DEF request as INT

DEF dx[5] as float
DEF dy[5] as float
DEF da[5] as float
DEF dv[5] as float
DEF dh[5] as INT
DEF dt[5] as float 'turning speed
DEF ds[5] as INT   'shooting status

DEF lx[5,16] as float
DEF ly[5,16] as float
DEF la[5,16] as float
DEF le[5,16] as INT
DEF lr[5] as INT
DEF l[5] as INT

DEF elx  as float
DEF ely  as float
DEF ela  as float
DEF ta as int
DEF ele  as INT
DEF elr  as INT
DEF eh as INT
DEF eTarget as INT 'enimy proposed target

DEF crt as INT
DEF exitGame as INT
crt = 57.2957795

DEF i as INT
DEF c AS INT
DEF j as INT
DEF exit as INT

FOR i = 1 to 5 'set initial variables----------------------

dv[i] = 1
da[i] = RAND(360)
dx[i] = RAND(800)
dy[i] = RAND(600)
dh[i] = 10
ds[i] = 1
le[i] = 1
lr[i] = 30
l[i] = 1


NEXT i

eh = 10

DEF tx as INT
DEF ty as INT
DEF dis as float

tx = 400
ty = 300
ta = 1




'menu===================================================================================

DO
FILLSCREEN RGB(10,10,15)

WRITETEXT 350, 310, "Play"

WRITETEXT 350, 350, "Instructions"

WRITETEXT 350, 390, "Credits"

DRAWFILLEDRECT 340, request * 40 + 310, 10, 10, RGB(255,0,0)
FLIP

WAITKEY

IF KEYDOWN(DIK_UP) & request <> 0 THEN request --

IF KEYDOWN(DIK_DOWN) & request <> 2 THEN request ++

IF KEYDOWN(DIK_RETURN) | KEYDOWN(DIK_ESCAPE) THEN ret = 1

UNTIL ret = 1
'game--------------------------------------------------------------------------------------



IF request = 1 THEN

DO

FILLSCREEN RGB(10,10,15)

WRITETEXT 100, 300, "Shoot the green circle 100 times b4 it shoots you"
WRITETEXT 100, 350, "use left and right. up and down for direction. space to shoot"
FLIP

WAITKEY

UNTIL KEYDOWN(DIK_RETURN)

ENDIF

IF request = 2 THEN 'credits------------------------------

DO
FILLSCREEN RGB(10,10,15)

WRITETEXT 100, 300, "Space Battles Va0.0.2 10-12-08 by Yujin Wu"
WRITETEXT 100, 340, "All Rights Reserved 2008 Yujin Wu"
FLIP

WAITKEY

UNTIL KEYDOWN(DIK_RETURN)

ENDIF


'game===============================================================


DO
FILLSCREEN RGB(10,10,15)

'keycontrols!----------------------------------------------------------------------

IF KEYDOWN(DIK_ESCAPE) THEN exitGame = 1

'aiming!-------------------------------------------------------------------







IF KEYDOWN(DIK_LEFT) & dt[1] > -1.5 THEN dt[1] = dt[1] - 0.2                   'turn left
IF KEYDOWN(DIK_RIGHT)& dt[1] < 1.5 THEN dt[1] = dt[1] + 0.2 'turn right

'angle adjustments--------------------------------

da[1] = da[1] + dt[1]

IF ABS(dt[1]) <> dt[1] THEN
dt[1] = dt[1] + 0.1
ENDIF

IF ABS(dt[1]) = dt[1] & dt[1] <> 0 THEN dt[1] = dt[1] - 0.1



IF KEYDOWN(DIK_UP) THEN
IF dv[1] < 1 THEN dv[1] = dv[1] + 0.01 'speed up
ENDIF

IF KEYDOWN(DIK_DOWN) THEN
IF dv[1] > 0.35 THEN dv[1] = dv[1] - 0.01 'slow down
ENDIF

'mk laser!----------------------------------------------

IF KEYDOWN(DIK_SPACE) & lr[1] < 0 THEN 'make laser

lx[1,l[1]] = dx[1]
ly[1,l[1]] = dy[1]
la[1,l[1]] = da[1]
le[1,l[1]] = 1
l[1] = l[1] + 1
lr[1] = 15
IF l[1] = 16 THEN l[1] = 1
ENDIF

'alliance laser shooting



FOR i = 2 to 5




IF ds[i] = 1 & lr[i] < 0 THEN
IF l[i] < 1 THEN l[i] = 1
lx[i,l[i]] = dx[i]

ly[i,l[i]] = dy[i]
la[i,l[i]] = da[i] 
le[i,l[i]] = 1
l[i] = l[i] + 1
lr[i] = 15
IF l[i] > 15 THEN l[i] = 1


ENDIF

NEXT i




'end of alliance----------------------

IF elr < 0 THEN                   'make enemy laser

FOR i = 1 to 5 'aim---------------------------

IF dis > SQRT(ABS(dx[i]-tx) * ABS(dx[i]-tx)  + ABS(dy[i] - ty ) * ABS(dy[i] - ty ) ) THEN
eTarget = i
dis = SQRT(ABS(dx[i]-tx) * ABS(dx[i]-tx) + ABS(dy[i] - ty ) * ABS(dy[i] - ty ))
ENDIF


NEXT i

dis = 1000

ta = ATAN((ty-dy[eTarget])/(tx-dx[eTarget])) * crt -90 'locking target to your ship
IF ABS(tx-dx[eTarget]) <> tx - dx[eTarget] THEN ta = ta + 180

elx = tx
ely = ty
ela = ta
ele = 1
elr = 5
elr = 120
ENDIF


'CALCULATE POSITIONS AND UPda[i]TE!---------------------------------

';ship new position----------
FOR i = 1 to 5

dx[i] = dx[i] + dv[i] * SIN(da[i] / crt)
dy[i] = dy[i] - dv[i] * COS(da[i] / crt)

NEXT i
'enimy laser new position

elx = elx + 4 * SIN(ela / crt)
ely = ely - 4 * COS(ela / crt)

'move lasers

FOR i = 1 to 5              'i = ship

FOR j = 1 to 16

lx[i,j] = lx[i,j] + 4 * SIN(la[i,j] / crt)
ly[i,j] = ly[i,j] - 4 * COS(la[i,j] / crt)

NEXT j

NEXT i

'laser collisions!----------------------------------------------------------------

FOR i = 1 to 5

FOR j = 1 to 16        'j is laser

IF le[i,j] = 1 THEN          'make sure the laser exists

IF lx[i,j] - tx < 20 & ly[i,j] - ty < 20 & lx[i,j] - tx > 0 & ly[i,j] - ty > 0 THEN 'collide with target?
eh --
DRAWRECT lx[i,j] - 3, ly[i,j] - 3, 6,6, RGB(100,100,0)
le[i,j] = 0
ENDIF 'end of collide with target?

ENDIF  'end of laser calculations

NEXT j 

NEXT i

FOR i = 1 to 5

IF ABS(elx - dx[i]) < 2 & ABS(ely - dy[i]) < 2 THEN
FILLSCREEN RGB(150, 0, 0)
dh[i] --
ele = 0
ENDIF

NEXT i
'==================

'angle corrections------------------------------

FOR i = 1 to 5

IF da[i] < 2 THEN da[i] = da[i] + 360                  'angle corrections
IF da[i] > 362 THEN da[i] = da[i] - 360

NEXT i


'health checks!---------------------------------------------------------------
FOR i = 1 to 5

IF dh[i] < 1 THEN
exit ++
ENDIF

NEXT i

IF exit = 5 THEN
exitGame = 1
ELSE
exit = 0
ENDIF

IF eh < 1 THEN
exitGame =1
ENDIF

'drawing!--------------------------------------------------------------------------
DRAWRECT tx, ty, 20, 20, RGB(0,100,0)          'draw target


FOR i = 1 to 5 'draw lasers

FOR j = 1 to 16

IF  le[i,j] = 1 THEN WRITEPIXEL lx[i,j], ly[i,j], RGB(255,100,100)

NEXT j

NEXT i

IF ele = 1 THEN WRITEPIXEL elx, ely, RGB(0,255,0)    'enimy lasers


FOR i = 1 to 5

DRAWLINE dx[i], dy[i], dx[i] + 5 * SIN(da[i] / crt), dy[i] - 5 * COS(da[i] / crt), RGB(255,0,0) 'draw fighter

NEXT i

DRAWFILLEDRECT 350, 550, 100, 10, RGB(50,10,15) 'health box
DRAWFILLEDRECT 350, 550, dh[1] * 10, 10, RGB(200,0,0)

DRAWFILLEDRECT 350, 570, 100, 10, RGB(10,10,50) 'speed box
DRAWFILLEDRECT 350, 570, dv[1] * 100, 10, RGB(0,0,200)

DRAWFILLEDRECT 350, 530, 100, 10, RGB(10,50,15) 'enimy health box
DRAWFILLEDRECT 350, 530, eh , 10, RGB(0,200,0)

'flash- -       -       -     

IF dh[1] < 2 & c > 30 THEN
DRAWFILLEDRECT 350, 550, 100, 10, RGB(50,10,15)   'flash box warning

ENDIF

c ++

IF c > 60 THEN c = 0
'end of flash -   -    -   -   -

'miscellanious!-------------------------------------------------------------------
elr--

FOR i = 1 to 5

lr[i]--

NEXT i

FLIP



'ending====================================================================
UNTIL exitGame = 1

FILLSCREEN RGB (10,10,15)

IF eh = 0 THEN WRITETEXT 350, 300, "YOU HAVE WON! WITH " + STR$(dh[1]) + " HEALTH LEFT"

IF dh[1] = 0 THEN WRITETEXT 350, 300, "GAME OVER!"

IF eh <> 0 & dh[1] <> 0 THEN WRITETEXT 0, 0, "press esc key to continue"

FLIP

WAITKEY(DIK_ESCAPE)



CLOSESCREEN
END



as i was saying, if anyone compiled that please tell me if it had worked on your computer.

on my computer after running debug build, i found out that immediately after variable assignment l[5] was normal, equaling 5. some point in the program l[5] became 30 (weird) and reduced by 1 every frame (which was what lr[5] should have done) and caused problems when it reached -1 because it was used as an array pointer thing. lr[5] was normal.

if anyone could figure out what happened i will think of an appropriate reward.

if anyone wants more details i am happy to provide it.

i have a previous 1 player version that worked well so with enough public nagging i might post that on as well.

Ionic Wind Support Team

Indexes are zero based.  Described in the users guide on arrays.

int l[5] has indices 0 to 4 so if you try and do 1 to 5 you will be overwriting memory of an adjacent variable.

So the correct FOR statement would be

FOR i = 0 to 4

NOT

FOR i = 1 to 5

If you prefer 1's based indices just make your arrays larger by 1.  You would of course be wasting the 0th element.

Paul.
Ionic Wind Support Team

pistol350

I'm pretty sure that this game is based on a game written in a graphical calculator.
I remember the old days (not that old when i think about it  ::) ) when i used to write small games using my Casio graphical calculator
which had only 3 colors RED\GREEN\BLUE , and 64Kb of memory. :)
Regards,

Peter B.

yujinwunz

December 13, 2008, 02:58:04 PM #3 Last Edit: December 13, 2008, 03:00:12 PM by yujinwunz
i think that was too easy for a reward.


my game is original, i have never used 64x calculators or whatever it is. do you still have them?

Copex

Quote from: yujinwunz on December 13, 2008, 02:58:04 PM
i think that was too easy for a reward.


my game is original, i have never used 64x calculators or whatever it is. do you still have them?

a reward was offered so you should honor your first post and head over here https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=ionicwin%40ionicwind%2ecom&item_name=Donate%20to%20Ionic%20Wind%20Software&item_number=ionic_wind_donation&no_shipping=0&no_note=1&tax=0&currency_code=USD&bn=PP%2dDonationsBF&charset=UTF%2d8
-
I really should learn how to use a spell checker! though im not sure how it will help someone who can not spell?
-
Except where otherwise noted, content Posted By Copex is
licensed under a Creative Commons Attribution 3.0 License

http://creativecommons.org/licenses/by/3.0/

pistol350

Quote from: yujinwunz on December 13, 2008, 02:58:04 PM
i think that was too easy for a reward.


my game is original, i have never used 64x calculators or whatever it is. do you still have them?

I am being VERY offtopic now.
Sorry i dit not meant that your game was  not original.
I just meant that from the look of it, it makes me think of games written in basic for 64kb calculators.

I still have my graphical calculator.
But unfortunately, i can no longer use it as it's damaged now.
A few years ago I lent it to a friend of mine who needed it to sit for an exam.
He gave it back to me a few month later with a couple of buttons missing from it.  >:(
Not mentionning the few other buttons that didn't work.
Only god knows how disappointed i was  :'(
It's since then that i started to play around computer programming and then i met Ibasic standard... and HERE i am :)
Regards,

Peter B.

yujinwunz

im posting a working version of the game next post here on general questions and roundtable.

im not old enough for paypal, im only 14