Fight simulation perfected - for now.

Started by Pagan Slim, June 14, 2010, 05:37:44 AM

Previous topic - Next topic

Pagan Slim

#15
I have added a scoring system.  My intent is to have it written to an external file as in the help example.  Problem is it crashes on me so I have left it out until I can solve the problem.  It's the print thing that doesn't work.  It seems so simple and yet I can't get it.


PlayBASIC Code: [Select]
; PROJECT : Fight Simulation
; AUTHOR : Pagan Slim
; CREATED : 6/16/2010
; EDITED : 6/30/2010
; ---------------------------------------------------------------------
; ---------------------------------------------------------------------

LoadFont "Arial",1,16,0

SetFPS 30

Cls RGB(95,0,0)
Print "You are walking the streets of a well known but feared neighborhood."
Print "You usually avoid this part of town but today is different."
Print "You feel braver today. Braver and perhaps a bit more foolish."
Print "You made your boast to your friends and they dared you to come here."
Print "You feel a chill. Maybe a shudder. You hear a garbage pail being knocked over."
Print "The sound jolts you to a higher awareness of your reality. "

Print " "
Print "You see someone ahead of you. It is dark and foggy tonight."
Print "Are you ready to throw down? Well. Are You?"
Print " "
Print "Press the ENTER key to get it on."
Print " "
Sync
WaitKey

Cls


Dim Guys$(6) ; Array Indexes are zero inclusive. That means 0,1,2, etc .. up to the Number of Elements are valid Indexes

For Guys_Name = 0 To 6 ; 0 will be the 'The Mysterious Glitch'
Guys$(Guys_Name) = ReadData$()
Next Guys_Name

;combat routine

;Initialize AND Randomize
;create stats

;Number of NPC's that you will fight.
NUM_GUYS = Rnd (5)+ 1


NPC_HITPOINTS = RndRange (2,10)

HITPOINTS = RndRange (7,18);ADJUSTED TO EQUAL THE ODDS.
ScorePoints = 0 ; Your initial score.

Print "YOUR HIT POINTS " + Str$(HITPOINTS)
Print " "
Print "NPC HIT POINTS " + Str$(NPC_HITPOINTS)
print " "
Print "YOUR SCORE " + Str$(ScorePoints)
Print " "
Sync
Wait 3000

;Game Loop
Do
;Initiative:
;who's first
COINTOSS=Rnd (120)

;PRINT "* INITIATIVE * " + Str$(COINTOSS)
;PRINT " "
;SYNC

Print "Number of NPCs " + Str$(NUM_GUYS)
Print " "
Print "YOUR SCORE " + Str$(ScorePoints)
Print " "

Sync

If COINTOSS>60
Print "The initiative is yours."
Print " "
Gosub MEFIRST:
Else
Print "NPC(s) has initiative."
Print " "
Gosub NPCFIRST:
EndIf


Print "Press a key to go on."
Print " "
Sync
WaitKey
Cls ;clear screen for next round of combat.
Loop


;NPCFIRST - npcfirst
NPCFIRST:
;attack (is it successful)
;N is which of the guys who attack.
randInd = RndRange(1,6)

For N = 1 To NUM_GUYS
Print Guys$(randInd)
Print " "

ATTACK = RndRange (1,6)+1
Print " "
Sync
;damage (HEALTH-ATTACK)

HITPOINTS=HITPOINTS-ATTACK

;adjust health
;report results
Print "YOUR HIT POINTS " + Str$(HITPOINTS)
Print " "
Sync
Wait 2000
;if_dead
If HITPOINTS<1
Print "You're DEAD!"
Print " "
Sync
Wait 3000
Gosub THE_END
EndIf

Next

Return

;ME FIRST - mefirst:
MEFIRST:
;attack (is it successful)
NPC_ATTACK=RndRange (1,8)
;damage (HEALTH-ATTACK)
NPC_HITPOINTS=NPC_HITPOINTS-NPC_ATTACK
;adjust health
;report results
Print "NPC HIT POINTS " + Str$(NPC_HITPOINTS)
Print " "
Sync
Wait 2000
;if_NPC_dead - GET RID OF HIM...
If NPC_HITPOINTS < 1
ScorePoints = ScorePoints + 10 ;Score adjustment for NPC Kills.
NUM_GUYS = NUM_GUYS - 1
Print "ONE NPC DEAD!"
Login required to view complete source code


kevin


Looks like it's coming nicely..

QuoteProblem is it crashes on me so I have left it out until I can solve the problem.

  Can you show us your code ?



Pagan Slim

#17
This is the code I'm working with now.  It prints a zero.  I don't even know if it creates a file.  I used the help example in PB.  I have been studying TDK's guide and I need to know how to print the contents of data files to the screen.  I have a lot of trouble learning this forum thing so please be patient.



PlayBASIC Code: [Select]
; PROJECT : MyScore
; AUTHOR : Pagan Slim
; CREATED : 6/29/2010
; EDITED : 6/30/2010
; ---------------------------------------------------------------------
Filename$="ScoreData.dat"
Scorepoints = 20
If FileExist(Filename$) Then DeleteFile Filename$
MatrixWidth=20000
MatrixHeight=20000
TilesX=70
TilesZ=70
FloatVar#=44.82

WriteFile Filename$,1
WriteString 1,"Here Be the Score Board"
WriteString 1,Str$(MatrixWidth)
WriteString 1,Str$(MatrixHeight)
WriteString 1,Str$(TilesX)
WriteString 1,Str$(TilesZ)
WriteString 1,Str$(FloatVar#)
CloseFile 1

;-------------------------------
Filename$="ScoreData.dat"
If FileExist(Filename$)
ReadFile Filename$,1
Header$ =ReadString$(1)
MatrixWidth =Val(ReadString$(1))
MatrixHeight =Val(ReadString$(1))
MatrixTilesX =Val(ReadString$(1))
MatrixTilesY =Val(ReadString$(1))
FloatVar# =Val#(ReadString$(1))
Print ScorePoints
Sync
WaitKey

CloseFile 1
EndIf










kevin

#18
  The example works, but it wasn't displaying the information back to the user.  

 Here's a version with a few comments in it..  

PlayBASIC Code: [Select]
; PROJECT : MyScore
; AUTHOR : Pagan Slim
; CREATED : 6/29/2010
; EDITED : 6/30/2010
; ---------------------------------------------------------------------
Filename$="ScoreData.dat"

Scorepoints = 20

; Check for an old copy of the file,, If it exists.. delete it
; so we can write a new copy of it to disk
If FileExist(Filename$) Then DeleteFile Filename$


; The variables we'll be saving into our file
MatrixWidth =20000
MatrixHeight=20000
TilesX=70
TilesZ=70
FloatVar#=44.82


; Start WriteFile the file using file channel #1
WriteFile Filename$,1
; write the string data to the open file channel
WriteString 1,"Here Be the Score Board"
WriteString 1,Str$(MatrixWidth)
WriteString 1,Str$(MatrixHeight)
WriteString 1,Str$(TilesX)
WriteString 1,Str$(TilesZ)
WriteString 1,Str$(FloatVar#)

; we're done writing, so close it
CloseFile 1


;-------------------------------
; Read the File.. First we check if the file we're going to
; read actually exists..
If FileExist(Filename$)=true
; Print this message if the file exists
Print "Found File.. Loading"

; Tell PB to start reading to file, using fiole channel #1
ReadFile Filename$,1

; read a line of the string data from the file..
Header$ =ReadString$(1)

; read the next line of string from next. This time we're converting
; the text (characters) returned from ReadtString$(1) into a numeric
; value using the VAL() Function. The result is assign to the variable
;
MatrixWidth =Val(ReadString$(1))
MatrixHeight =Val(ReadString$(1))
MatrixTilesX =Val(ReadString$(1))
MatrixTilesY =Val(ReadString$(1))
FloatVar# =Val#(ReadString$(1))

; we're done reading the file.. so now we close the file channel
CloseFile 1
EndIf


; DRaw the variables to the screen
print Header$
Print MatrixWidth
Print MatrixHeight
Print MatrixTilesX
Print MatrixTilesY
Print FloatVar#

; display the screen
Sync
waitkey

[/code]



and here's a second version to have a look at.


[code]


; Insert folder name in front of the filename so you can view the file
; manually. EG Filename$= "C:\ScoreData.txt" to save the file to
; the C drive

Filename$="ScoreData.txt"


; Check for an old copy of the file,, If it exists.. delete it
; so we can write a new copy of it to disk

If FileExist(Filename$) Then DeleteFile Filename$


;------------------------------------------------------------
; The variables we'll be saving into our file
;------------------------------------------------------------

Name$ ="Bill"
Scorepoints = 20



;------------------------------------------------------------
; Start WriteFile the file using file channel #1
;------------------------------------------------------------
WriteFile Filename$,1

; write the string data to the open file channel
WriteString 1,"My High Score Table"


; write the string data to the open file channel
WriteString 1,Name$


; convert our variable ScorePOints to a text representation
; and write it to the file channel #1
WriteString 1,str$(ScorePoints)

; we're done writing, so close it
CloseFile 1


; Our file now has two rows of TEXT in it.


;------------------------------------------------------------
; Read the File.. First we check if the file we're going to
; read actually exists..
;------------------------------------------------------------
If FileExist(Filename$)=true

; Print this message if the file exists
Print "Found File.. Loading"

; Tell PB to start reading to file, using fiole channel #1
ReadFile Filename$,1

; read a line of the string data from the file..
Loaded_Header$ =ReadString$(1)

; read a line of the string data from the file..
Login required to view complete source code







Pagan Slim

#19
I modified it and it isn't picking up the score.  

PlayBASIC Code: [Select]
; PROJECT : Project5
; AUTHOR : Pagan Slim
; CREATED : 6/30/2010
; ---------------------------------------------------------------------

; PROJECT : MyScore
; AUTHOR : Pagan Slim
; CREATED : 6/29/2010
; EDITED : 6/30/2010
; ---------------------------------------------------------------------
Filename$="ScoreData.dat"

Scorepoints = 20

; Check for an old copy of the file,, If it exists.. delete it
; so we can write a new copy of it to disk
If FileExist(Filename$) Then DeleteFile Filename$


; The variables we'll be saving into our file



; Start WriteFile the file using file channel #1
WriteFile Filename$,1
; write the string data to the open file channel
WriteString 1,"Here Be the Score Board"
WriteString 1,Str$(ScorePoints)


; we're done writing, so close it
CloseFile 1


;-------------------------------
; Read the File.. First we check if the file we're going to
; read actually exists..
If FileExist(Filename$)=true
; Print this message if the file exists
Print "Found File.. Loading"

; Tell PB to start reading to file, using fiole channel #1
ReadFile Filename$,1

; read a line of the string data from the file..
Header$ =ReadString$(1)

; read the next line of string from next. This time we're converting
; the text (characters) returned from ReadtString$(1) into a numeric
; value using the VAL() Function. The result is assign to the variable
;
MatrixWidth =Val(ReadString$(1))


; we're done reading the file.. so now we close the file channel
CloseFile 1
EndIf


; DRaw the variables to the screen
Print Header$


; display the screen
Sync
WaitKey







So... I wrote a little simple scoregame to test my ignorance.

PlayBASIC Code: [Select]
; PROJECT : Project4
; AUTHOR : Pagan Slim
; CREATED : 6/30/2010
; EDITED : 6/30/2010
; ---------------------------------------------------------------------
Filename$="ScoreData.dat"

; Check for an old copy of the file,, If it exists.. delete it
; so we can write a new copy of it to disk
If FileExist(Filename$) Then DeleteFile Filename$


ScorePoints = 0
Print "Silly Scorekeeping Game"
Print " "
Print " Press the enter key to score in the easiest game in the world."
Sync

Do

Print "Top of 'Do Loop'."
WaitKey
ScorePoints = ScorePoints + 1
Print "ScorePoints:" + Str$ (ScorePoints)
Sync

; Start WriteFile the file using file channel #1
WriteFile Filename$,1
; write the string data to the open file channel
WriteString 1,"Here Be the Score Board"
WriteString 1,Str$(ScorePoints)


Loop


ENDGAME:
Cls
Print "FINAL SCORE:"
WriteString 1,Str$(ScorePoints)
; we're done writing, so close it
CloseFile 1
WaitKey
End


READSCORE:
;-------------------------------
; Read the File.. First we check if the file we're going to
; read actually exists..
If FileExist(Filename$)=true
; Print this message if the file exists
Print "Found File.. Loading"

; Tell PB to start reading to file, using fiole channel #1
ReadFile Filename$,1

; read a line of the string data from the file..
Header$ =ReadString$(1)

; read the next line of string from next. This time we're converting
; the text (characters) returned from ReadtString$(1) into a numeric
; value using the VAL() Function. The result is assign to the variable
;
MatrixWidth =Val(ReadString$(1))
MatrixHeight =Val(ReadString$(1))
MatrixTilesX =Val(ReadString$(1))
MatrixTilesY =Val(ReadString$(1))
FloatVar# =Val#(ReadString$(1))

; we're done reading the file.. so now we close the file channel
CloseFile 1
EndIf


; DRaw the variables to the screen
Print Header$
Print MatrixWidth
Print MatrixHeight
Print MatrixTilesX
Print MatrixTilesY
Print FloatVar#

; display the screen
Sync
WaitKey






It doesn't work yet.  I need to study this more, I know.  Any idea of which examples that came with PB that would best demonstrate what I need to know?

Pagan Slim

I forget to put print ScorePoints in there.  My mistake.

Big C.

Slim,

QuoteMy intent is to have it written to an external file

its not so difficult as you thing... Kevin gives you very good examples to study...

For your intent I would put the Checkcode for file at the beginning of your program and at the end you should update the scorefile...

After the line
HITPOINTS = RndRange (7,18);ADJUSTED TO EQUAL THE ODDS.

you would find the starting line which we should modify
ScorePoints = 0 ; Your initial score.


We should start to check if the scorfile exists... You know what to do and will hopfully get the following  :)

If FileExist("ScoreData.dat")

If the file exits you should open the file, read the needed score value and assign the readed value to the scorevar ScorePoints... It should look like this lines
OpenFile "ScoreData.dat",1
ScorePoints = Val(ReadString$(1))

at this point we shoul close the file access with the line
CloseFile 1
so the game can start

Ok but if the scoreData.dat doesn't exists we need do predefine the value of our var ScorePoints
We should expand the if-clause with the else-term like this lines
Else
ScorePoints = 0 ; Your initial score.
EndIf


If the game ends then we need either create the scoredata file or we need to write acess them...

I would change the "The_END" Section as follows..


THE_END:
Print "It's over boys."
Print " "
Print "YOUR SCORE " + Str$(ScorePoints)
Print " "


WriteFile "ScoreData.dat",1
WriteString 1, Str$(ScorePoints)
CloseFile 1

Sync
WaitKey
End
Return


Hope that helps yo a little bit... try to understand by reading the Help on Keyword reference...  ;)