Full source of game for developers to check why it crashes

Linux specific forum
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Full source of game for developers to check why it crashes

Post by marcoagpinto »

Code: Select all

; HANGMAN V1.0
; @ 2013 Marco A.G.Pinto (code)
;        and
;         Robert Bergström (graphics)
;


  ; Init stuff
  InitSprite()
  InitSound()
  InitKeyboard()
  InitMovie()
  
  
  ; Declare Procedures
  Declare place_words_score_left(words, score)
  Declare death_frame_player(death_frame)
  Declare place_word_hidden(word_during_game_hidden$)
  Declare place_letters_failed(letters_failed$)
  Declare place_word_failed(word_during_game$)
  Declare congratulations()
  
  
  ; Load the font
  LoadFont(0, "Courier", 10) ;Load Courrier Font, Size 10
  LoadFont(1, "Courier", 16) ;Load Courrier Font, Size 16
  

  ; Reserve the words
  Global word_during_game$=""
  Global word_during_game_hidden$=""
  Global letters_failed$=""
  Global Dim current_word$(100)
  Global Dim word_selected(100) ;0=not played, 1=played
  Global number_of_words=0 ; total number of words in the data section
  

  ; Read the words
  Restore words
  Repeat
    Read.s a$
    If a$<>"*"
      number_of_words=number_of_words+1
      current_word$(number_of_words)=a$
    EndIf
  Until a$="*"
  
  
  ; Create the main menu
  OpenScreen(1024,768,32,"Hangman V1.0 - build 4")
  
  ; Load frames of guy dieing
  For f=0 To 11
    UsePNGImageDecoder()
    LoadImage(f+2,GetCurrentDirectory()+"media\graphics\hangman_frame_"+Right("0"+Str(f),2)+".png")
  Next f
  
main_menu_lp:
  StartDrawing(ScreenOutput())
  UsePNGImageDecoder()
  LoadImage(1,GetCurrentDirectory()+"media\graphics\mainmenu.png")
  DrawingMode(#PB_2DDrawing_Default)
  DrawImage(ImageID(1),0,0)
  DrawingFont(FontID(0)) ;Use the 'Courier' font
  BackColor(RGB(0,0,0))
  FrontColor(RGB(255,255,255))
  DrawingMode(1) ;Transparent TextBackground
  DrawText(1024-50-50-50-20-20,768-50,"V1.0 alpha - Build 4")  
  StopDrawing()
  FlipBuffers()
  
  LoadMovie(1,GetCurrentDirectory()+"media\music\title.mp3")
  PlayMovie(1,ScreenID())
  
    
  Repeat
    ExamineKeyboard()
    i$=KeyboardInkey()
  Until i$<>""
  If i$=Chr(27)
    End
  EndIf
  
  StopMovie(1)
  
  
  Delay(400) ;a small pause
    
  words=0
  score=0
  
  ; Load audio to play during game
  LoadMovie(2,GetCurrentDirectory()+"media\music\beep.mp3")  
  LoadMovie(3,GetCurrentDirectory()+"media\music\dead.mp3")   
  
  
  ; Clear the words already used during last play
  For f=1 To number_of_words
    word_selected(f)=0
  Next f

  
game_lp:
  ClearScreen(RGB(0,0,0))
  StartDrawing(ScreenOutput())
  UsePNGImageDecoder()
  LoadImage(1,GetCurrentDirectory()+"media\graphics\gamebackground.png")
  DrawingMode(#PB_2DDrawing_Default)
  StopDrawing()
  
  letters_failed$=""
  death_frame=0
  word_during_game_hidden$=""
  
  
  ; SELECT THE RANDOM WORD
  t=0
  For f=1 To number_of_words
    If word_selected(f)=1
       t=t+1
     EndIf
  Next f
  If t=number_of_words
    congratulations()
    ClearScreen(RGB(0,0,0))
    Goto main_menu_lp
  EndIf
  
  RandomSeed(ElapsedMilliseconds())
  Repeat
    t=Random(number_of_words,1)
  Until word_selected(t)=0
  word_selected(t)=1
  word_during_game$=current_word$(t)
  For f=1 To Len(word_during_game$)
    word_during_game_hidden$=word_during_game_hidden$+"-"
  Next f
  ; END SELECT THE RANDOM WORD
  
  
game_lp1:

; The main functions to avoid flicker
  StartDrawing(ScreenOutput())
;  DrawText(20,20,word_during_game$+"xxx",RGB(255,255,255),RGB(0,0,0))
  DrawImage(ImageID(1),0,0)
  StopDrawing()
  place_words_score_left(words, score)
  place_letters_failed(letters_failed$)
  death_frame_player(death_frame)
  If Len(letters_failed$)=11
    place_word_failed(word_during_game$)
    words=words+1
    Goto game_lp
  EndIf
  place_word_hidden(word_during_game_hidden$)
  FlipBuffers()
  Delay(400)

  
  Repeat
    
    ExamineKeyboard()
    i$=UCase(KeyboardInkey())
    
    ; Validate the letters pressed which can only be between A and Z and <ESC>
    If (Asc(i$)<Asc("A") Or Asc(i$)>Asc("Z")) And i$<>Chr(27)
      i$=""
    EndIf
    
    If i$<>Chr(27) And i$<>""
      PlayMovie(2,ScreenID())
      t=0 ; if t=1 it means the letter is already failed and in the list of failed letters
      If Len(letters_failed$)>0
        For f=1 To Len(letters_failed$)
          If Mid(letters_failed$,f,1)=i$
            t=1
          EndIf
        Next f
      EndIf
      If t=0
        tt=0
        For f=1 To Len(word_during_game$)
          If Mid(word_during_game$,f,1)=i$
            tt=1   
          EndIf
        Next f
        If tt=0
          letters_failed$=letters_failed$+i$
          death_frame=death_frame+1
          Goto game_lp1
        EndIf
        
        tt$=""
        For f=1 To Len(word_during_game$)
          t$=Mid(word_during_game$,f,1)
          If t$=i$
            tt$=tt$+i$
          Else
            tt$=tt$+Mid(word_during_game_hidden$,f,1)
          EndIf
        Next f
        word_during_game_hidden$=tt$
        Goto game_lp1
      EndIf
    EndIf
    
    ; Win the current word
    If word_during_game$=word_during_game_hidden$
      words=words+1
      score=score+1
      Goto game_lp
    EndIf
  Until i$=Chr(27)
  

  
  ClearScreen(RGB(0,0,0))
  FlipBuffers()
  Delay(400) ;a small pause
  Goto main_menu_lp
  

 
; Places words and score on the screen
; V1.0 - 21/APR/2013
; V1.1 - 23/APR/2013
;        a) Now shows on screen the number of words left
Procedure place_words_score_left(words, score)
  StartDrawing(ScreenOutput())
  DrawingFont(FontID(1)) ;Use the 'Courier' font  
  DrawText(100,10, "WORDS:"+Str(words), RGB(255,0,0))
  DrawText(500-50,10, "SCORE:"+Str(score), RGB(255,0,0))
  DrawText(500-50+400-50,10, "LEFT:"+Str(number_of_words-words), RGB(255,0,0))  
  StopDrawing()
EndProcedure


; Places the frames of the hanging man
; V1.0 - 21/APR/2013
Procedure death_frame_player(death_frame)
      StartDrawing(ScreenOutput())
      DrawImage(ImageID(death_frame+2),100+200+100,100+100)
      StopDrawing()
      If death_frame=11
        FlipBuffers()
        Delay(400)
        StartDrawing(ScreenOutput())
        DrawImage(ImageID(10+2),100+200+100,100+100)
        StopDrawing()
        PlayMovie(3,ScreenID())
        Delay(6000)
      EndIf
EndProcedure


; Places the hidden word on the screen with "-"s and letters
; 1.0 - 21/APR/2013
Procedure place_word_hidden(word_during_game_hidden$)
  StartDrawing(ScreenOutput())
  DrawingFont(FontID(1)) ;Use the 'Courier' font 
  DrawText((1024/2)-(Len(word_during_game_hidden$)*8/2),100+100+200+100,word_during_game_hidden$,RGB(255,255,255),RGB(0,0,0))
  StopDrawing()
EndProcedure



; Places the 11 letters failed
; 1.0 - 21/APR/2013
Procedure place_letters_failed(letters_failed$)
  StartDrawing(ScreenOutput())
  DrawingFont(FontID(1)) ;Use the 'Courier' font 
  DrawText(50,700,letters_failed$,RGB(255,255,255),RGB(0,255,0))
  StopDrawing()
EndProcedure



; Places the failed word centered on the screen and pauses
; 1.0 - 21/APR/2013
Procedure place_word_failed(word_during_game$)
  StartDrawing(ScreenOutput())
  DrawingFont(FontID(1)) ;Use the 'Courier' font 
  DrawText((1024/2)-(Len("The word was: "+word_during_game$)*8/2),100+100+200+100,"The word was: "+word_during_game$,RGB(255,255,255),RGB(255,0,0))
  StopDrawing()
  FlipBuffers()
  Delay(400)
  Repeat
    ExamineKeyboard()
    i$=UCase(KeyboardInkey())
  Until i$<>""
EndProcedure



; Places the congratulation image for solving all words
; 1.0 - 21/APR/2013
Procedure congratulations()
  StartDrawing(ScreenOutput())
  UsePNGImageDecoder()
  LoadImage(14,GetCurrentDirectory()+"media\graphics\congratulations.png")
  DrawingMode(#PB_2DDrawing_Default)
  DrawImage(ImageID(14),0,0)
  StopDrawing()
  FlipBuffers()
  Delay(4000)
EndProcedure


DataSection
words: 
    Data.s "SCHOOL", "TOMORROW","ADDRESS","LENGTH","EXPERT"
    Data.s "*"
  EndDataSection
  
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Full source of game for developers to check why it crash

Post by marcoagpinto »

Ahhh...

Rings was saying on IRC that without the media, it wouldn't be possible to test the source code.

So, I have shared in Dropbox the full game: https://dl.dropboxusercontent.com/u/306 ... cecode.zip

Please notice that the graphics are from Microsoft Word Clipart and also from the ZX Spectrum game (this is a remake). They are just there until I replace them with the final release graphics still to be developed.

In paths, please notice that in Linux you have to change "\" with "/".

Thanks!

Kind regards,
>Marco A.G.Pinto
---------------
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Full source of game for developers to check why it crash

Post by luis »

marcoagpinto wrote: In paths, please notice that in Linux you have to change "\" with "/".
You can just use "/" and it will work on both linux and windows.

I tried to guess a word right and one wrong, in both cases it didn't crash (pb511 x86 / win7 x64).

Unless you are saying it crashes only on linux, because it's not clear.
"Have you tried turning it off and on again ?"
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Full source of game for developers to check why it crash

Post by marcoagpinto »

luis wrote: Unless you are saying it crashes only on linux, because it's not clear.
Yes, it crashes only on Linux.

On Windows 8 it works without any problems.
User avatar
Shardik
Addict
Addict
Posts: 2067
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Full source of game for developers to check why it crash

Post by Shardik »

I tested your source code on Kubuntu 12.04 LTS x86. After changing all "\" to "/" I discovered the problem with your code: You should never use LoadMovie() without checking whether the file was successfully loaded. Otherwise you might produce a crash! On Linux LoadMovie() always returns an error when trying to load an mp3 file because mp3 files are only supported on Windows:
http://www.purebasic.fr/english/viewtop ... 37&t=53683
Fred wrote:No, the movie lib was never intended to play mp3. It does work on Windows because of the DirectShow plugin management but it's not the rule. The help should be changed.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Full source of game for developers to check why it crash

Post by luis »

Well, we can say no return value is actually ever tested in that code, not the init*(), not the loadimage(), not the loadmovie(). And this is a tiny program. Ignoring return values usually is not a good idea.
"Have you tried turning it off and on again ?"
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Full source of game for developers to check why it crash

Post by marcoagpinto »

Buaaaaaaaaaaaa

So, I can't use MP3s on Linux?

but, if I place audio in WAV the files will become huge.

Is there any other way of solving the issue?

Thanks!

Kind regards from Marco A.G.Pinto
---------------
Fred
Administrator
Administrator
Posts: 18350
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Full source of game for developers to check why it crash

Post by Fred »

There is an OGG lib for that, see: http://www.purebasic.com/documentation/ ... coder.html. Use it with PlaySound() and the streaming flag and should do the work. BTW, you don't have the right to play mp3 unless you paid a licence for the decoder.
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Full source of game for developers to check why it crash

Post by marcoagpinto »

Fred wrote:There is an OGG lib for that, see: http://www.purebasic.com/documentation/ ... coder.html. Use it with PlaySound() and the streaming flag and should do the work. BTW, you don't have the right to play mp3 unless you paid a licence for the decoder.
Fred, I have changed all audio to .ogg format and it still crashes on Ubuntu 12.10...

What am I doing wrong?

Thanks!

Kind regards,
>Marco A.G.Pinto
---------------
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Full source of game for developers to check why it crash

Post by luis »

marcoagpinto wrote: What am I doing wrong?
Updating your code in the first post or better yet on dropbox (new media included) with the changes you made could help.

Did you add the checks for the values returned by the commands or are you still ignoring them ?

With "changed all audio to .ogg format" do you mean you also stopped to use LoadMovie and switched to LoadSound, PlaySound ? Because it's not clear.

At least with these answers maybe someone else on linux can try to help you.
"Have you tried turning it off and on again ?"
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Full source of game for developers to check why it crash

Post by marcoagpinto »

ahhhh...

here is the new source code: https://dl.dropboxusercontent.com/u/306 ... cecode.zip

I placed the load commands (fonts, audio and images) inside a procedure to make the code more modular.

Thanks for your help!

Kind regards,
>Marco A.G.Pinto
---------------
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Full source of game for developers to check why it crash

Post by luis »

I tried to run it under a VM with Linux mint x64 and PB 5.11 x64 but for some reason all the keyboard commands seems to have problems, I type and the chars go to the ide instead of the main program window, etc.
All seems to go upside down as soon graphical/keyboard commands are involved.
I'm sorry but you'll have to wait for someone with a real linux installation to check it, I cannot help.
"Have you tried turning it off and on again ?"
LuCiFeR[SD]
666
666
Posts: 1033
Joined: Mon Sep 01, 2003 2:33 pm

Re: Full source of game for developers to check why it crash

Post by LuCiFeR[SD] »

I suspect it maybe related to this
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Full source of game for developers to check why it crash

Post by luis »

LuCiFeR[SD] wrote:I suspect it maybe related to this
Could be, thank you. :?
"Have you tried turning it off and on again ?"
User avatar
marcoagpinto
Addict
Addict
Posts: 1076
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Full source of game for developers to check why it crash

Post by marcoagpinto »

Hello my dear ones,

I told Rings on IRC that my game still crashes even though I placed the hints to check if screens and files are opened.

This happens on my VM with Ubuntu 12.10 x86 with PureBasic 5.11 x86.

The source code is here: https://dl.dropboxusercontent.com/u/306 ... gman_5.zip

Could you take a look please?

Thanks!

Kind regards,
>Marco A.G.Pinto
---------------
Post Reply