help, it's not working! crashing code random

Advanced game related topics
rory-games
User
User
Posts: 31
Joined: Sun Feb 02, 2020 9:14 am
Contact:

help, it's not working! crashing code random

Post by rory-games »

Hello, please tell me why this code is crashing?
Declare speak(text$)
Declare sound(name$,anchor,other)
Structure sound
name.s
id.i
EndStructure

Global NewList sounds.sound()
Global espawntimer=25000
Global ispawntimer = 40000
Global weapontimer = 0
Global fcd = 20
Structure enemy
health.l
x.l
EndStructure
Global NewList enemies.enemy()
Global enemytimer = 180
Global itemtimer=900
Structure item
x.l
EndStructure
Global NewList items.item()
Procedure enemy()
AddElement(enemies())
enemies()\x = 20
enemies()\health = 50
EndProcedure

Procedure moreitems()
For i = 1 To 3
AddElement(items())
items()\x=Random(40,1)
Next
EndProcedure
Global walktimer = 180
Global pushvar1=0
Global wdamage = 0
Global wrange = 0
Global wtime = 0
Global weapon.s = ""
Global x = 0
Global health = 100
#left$ = Chr(37)
#up$ = Chr(38)
#right$ = Chr(39)
#down$ = Chr(40)
InitKeyboard()
XIncludeFile "tolk.pb"
tolk::load()
tolk::detectscreenreader()
Global x = 0
InitSprite()
InitSound()
UseOGGSoundDecoder()
Procedure gd(anchor,other)
ProcedureReturn other-anchor
EndProcedure
Procedure sound(name$,anchor,other)
did = 0
If did = 0

bla = Random(2000,1)
AddElement(sounds())
sounds()\id = bla
sounds()\name = name$
LoadSound(bla,name$)
SoundVolume(bla,100-Abs(gd(anchor,other))*6)
SoundPan(bla,gd(anchor,other)*6)
PlaySound(bla)
EndIf
EndProcedure

Procedure speak(text$)
tolk::output(text$)
EndProcedure
moreitems()
enemy()
OpenScreen(800,600,32,"test")
mehple = LoadSound(2001,"gamemus.ogg")
PlaySound(2001,#PB_Sound_Loop)
meh = LoadSound(0,"bar1.ogg")
PlaySound(0)
Procedure itemcheckloop()
ForEach items()
sound("ite2.ogg",x,items()\x)
If x = items()\x
speak("health boost!")
health + 10
DeleteElement(items())
Continue
EndIf
Next
EndProcedure
Repeat
FlipBuffers()

ExamineKeyboard()
press$ = KeyboardInkey()
If KeyboardPushed(#PB_Key_LeftAlt) And KeyboardReleased(#PB_Key_Y)
speak(Str(MemoryStatus(#PB_System_FreePhysical)/1000000))

EndIf
If KeyboardPushed(#PB_Key_2) And weapon<> "machinegun"
speak("machinegun.")
sound("machinegundraw.ogg",x,x)
weapon = "machinegun"
weapontime = 90
weapondamage = 5
weaponrange = 10
EndIf
If KeyboardPushed(#PB_Key_3) And weapon <> "eagle"
sound("desert_eagledraw.ogg",x,x)
speak("desert eagle.")
weapon = "eagle"
weaponrange = 8
weapondamage = 8
weapontime = 160
EndIf
If ElapsedMilliseconds() > weapontimer
If KeyboardPushed(#PB_Key_Space) And weapon<> ""
sound(weapon+".ogg",0,0)
weapontimer = ElapsedMilliseconds() + weapontime
ForEach enemies()
If enemies()\x<x+weaponrange And enemies()\x>x-weaponrange
sound("guy1hit"+Random(3,1)+".ogg",x,enemies()\x)
sound("body"+Random(3,1)+".ogg",x,enemies()\x)
enemies()\health - weapondamage
EndIf
If enemies()\health < 1
sound("guy"+Random(5,1)+"death.ogg",x,enemies()\x)
DeleteElement(enemies())
Continue
EndIf
Next
EndIf
EndIf
If press$ = "c"
speak(Str(x))
EndIf
If ElapsedMilliseconds() > walktimer
If KeyboardPushed(#PB_Key_A) And x > 0
itemcheckloop()
walktimer = ElapsedMilliseconds() + 180
Debug1 = ElapsedMilliseconds()
sound("cementstep"+Random(5,1)+".ogg",0,0)
x - 1
debug2 = ElapsedMilliseconds()
debug2 - Debug1

EndIf
EndIf
If ElapsedMilliseconds() > walktimer
If KeyboardPushed(#PB_Key_D) And x < 40
walktimer = ElapsedMilliseconds() + 180

itemcheckloop()
x + 1
sound("cementstep"+Random(5,1)+".ogg",0,0)
EndIf
EndIf
If press$ = "h"
speak(""+health+" health.")
EndIf
If KeyboardPushed(#PB_Key_1) And pushvar1 = 0


pushvar1 = 1
weapon = "sniper_rifle"
speak(weapon)
sound("sniper_rifledraw.ogg",0,0)
weapontime=2000
weapondamage=20
weaponrange=20
EndIf

If KeyboardReleased(#PB_Key_1)
pushvar1 = 0
EndIf
If ElapsedMilliseconds() > itemtimer

itemtimer = ElapsedMilliseconds() + 600
EndIf

If health < 1
sound("death.ogg",x,x)
Delay(1000)
End
EndIf
If ElapsedMilliseconds() > enemytimer
fcd - 1
enemytimer = ElapsedMilliseconds() + 180
ForEach enemies()
If enemies()\x < x
enemies()\x + 1
sound("cementstep"+Random(5,1)+".ogg",x,enemies()\x)
EndIf
If enemies()\x > x
enemies()\x - 1
sound("cementstep"+Random(5,1)+".ogg",x,enemies()\x)
EndIf
If enemies()\x = x And fcd < 1
sound("knifehit.ogg",0,0)
health - 10
fcd = 3
EndIf
Next
EndIf
If ElapsedMilliseconds() >espawntimer
espawntimer = ElapsedMilliseconds() + 15000
enemy()
EndIf
If ElapsedMilliseconds() > ispawntimer
ispawntimer = ElapsedMilliseconds() + 30000
moreitems()
EndIf
ForEach sounds()
If SoundStatus(sounds()\id) = #PB_Sound_Stopped
FreeSound(sounds()\id)
DeleteElement(sounds())
Break
EndIf
Next
Until nevergonnahappen = 1
Also, if there are any stupid variables names, it's because it was started when i just finished raging at videogames, or just because I was in a rush.
Also, if the code is mess up, can you help to clean it a bit?
Thanks in advance.
I know it's not clean at all I know.
just these crashes are confusing me a lot. also this is my first attempt at a pure basic game. let me describe the crashing: it just glitches out, pulls an error and then stops playing anything.
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1243
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: help, it's not working! crashing code random

Post by Paul »

Hello, please tell me why this code is crashing?
How can anyone tell you why your code is crashing when you don't provide code we can actually run?

You have XIncludeFile "tolk.pb" in your code but have not provided that part for us as well as quite a number of .ogg files that we can't run your code without.
And please use CODE tags when posting code so it's easier for everyone to read. :wink:
Image Image
rory-games
User
User
Posts: 31
Joined: Sun Feb 02, 2020 9:14 am
Contact:

Re: help, it's not working! crashing code random

Post by rory-games »

hello, I have fixed the crashes but the items make everything lag. when one is spawned everything slows down. a lot. walking is around 30 ms slower and the macgun drops to around 130ms rather than 90
I can't really provide the ogg sounds because the well, let's just put it this way: are cracked sounds.
So yeah, I just need a better item loop. can you provide one?
Also, what is a code tag? I'd be happy to make use of them so long as I know how to do so :)
User avatar
Demivec
Addict
Addict
Posts: 4086
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: help, it's not working! crashing code random

Post by Demivec »

A code tag is a tag that causes your 'code' to be formatted differently than the rest of the message so that it is easier to read and so it stands out as code.

Different tags are displayed above the editing box where you enter a new message. They can be entered by highlighting the desired text and then clicking on the 'Code' button which is next to the button for a 'Quote'. You can also click the button first and then type or paste code between the pair of tags. You can also type the tags directly before and afted the desired code.

The tags look like this:

Code: Select all

[code]
[/code]

This is what they look like with code between them:

Code: Select all

[code]If useTags = #True
  ; things are more readable
EndIf
[/code]

This is how the preceding text would be shown if the code tags were placed as shown:

Code: Select all

If useTags = #True
  ; things are more readable
EndIf
If you haven't already guessed, I had to put the code tags in code tags themselves to display them in the message. :wink:
rory-games
User
User
Posts: 31
Joined: Sun Feb 02, 2020 9:14 am
Contact:

Re: help, it's not working! crashing code random

Post by rory-games »

Ok, I'll try. Let's try:

Code: Select all

messagerequester("hello","hello, code tags!")
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: help, it's not working! crashing code random

Post by #NULL »

Now you can use the 'edit' button in your first post and put code tags around your code, maybe reformatting it first in the PB-IDE if the indentation is lost.
Post Reply