May have some Help please problem solved thanks

Started by 659_minifly, June 11, 2010, 08:15:52 AM

Previous topic - Next topic

659_minifly

I made a small Maze program first of all it read a file call level.01 for initializing the maze.
My problem is when i move the sprite with the keyleft, keyright,keyup or keydown
the sprite is redraw at the previous place and at the new one so all the window trace the sprite.
I am sure that is normal and this is the way i made the program.
If some one could take a look at it and tell me what and where is the mistake . i am new in programming with Playbasic.
Thanks

kevin

#1
 You haven't actually included the PlayBasic code in your example (the .PBP file is the project info file, not the code), so I'm not too sure how anyone can really help.  

 However, my best guess as to what your codes logic looks like,  would be something like the following.

PlayBASIC Code: [Select]
;----------------------------------------------
; >> Set Up IMAGE <<
;----------------------------------------------

; this bit of code makes an image (with a circle on it)
Image=NewFXImage(64,64)
RenderToImage image
Circle 32,32,32,true
rendertoscreen


;----------------------------------------------
; >> Set Up SPRITE <<
;----------------------------------------------

; make our sprite, using the image we just created
MySpr=newSprite(400,300,Image)
spritedrawmode MySpr,2


; limit the speed of the program to less to 75 frames per second
SetFPS 75

;----------------------------------------------
; >> Main Loop Of Program <<
;----------------------------------------------
do

; handle LEFT/RIGHT movement
if LeftKey()=true then MoveSpriteX MYSpr,-2
if RightKey()=true then MoveSpriteX MySpr,2

; handle UP/DOWN movement
if upKey()=true then MoveSpriteY MySpr,-2
if downKey()=true then MoveSpriteY MySpr,2

; change the sprites tint colour each update
Colour=$ff000000 | rndrgb()
SpriteTint MySpr,Colour

; draw all the sprites to the current surface(Screen or image)
drawallsprites

; show the newly drawn screen to the user
Sync

; loop back to the DO statement to keep this program running
loop




 The above version will produce trails as the sprite moves around the screen.  

PlayBASIC Code: [Select]
;----------------------------------------------
; >> Set Up IMAGE <<
;----------------------------------------------

; this bit of code makes an image (with a circle on it)
Image=NewFXImage(64,64)
RenderToImage image
Circle 32,32,32,true
rendertoscreen


;----------------------------------------------
; >> Set Up SPRITE <<
;----------------------------------------------

; make our sprite, using the image we just created
MySpr=newSprite(400,300,Image)
spritedrawmode MySpr,2


; limit the speed of the program to less to 75 frames per second
SetFPS 75

;----------------------------------------------
; >> Main Loop Of Program <<
;----------------------------------------------
do

; Clear the screen of whatever was on it last time we run
; this loop. IN this case i'm using the CLS command to clear
; the screen to BLACK ( RGB (0,0,0)='s BLACK). This will
; remove everything from the screen

Cls Rgb(0,0,0)


; handle LEFT/RIGHT movement
if LeftKey()=true then MoveSpriteX MYSpr,-2
if RightKey()=true then MoveSpriteX MySpr,2

; handle UP/DOWN movement
if upKey()=true then MoveSpriteY MySpr,-2
if downKey()=true then MoveSpriteY MySpr,2

; change the sprites tint colour each update
Colour=$ff000000 | rndrgb()
SpriteTint MySpr,Colour

; draw all the sprites to the current surface(Screen or image)
drawallsprites

; show the newly drawn screen to the user
Sync

; loop back to the DO statement to keep this program running
loop




  While this version doesn't.

   When we draw a sprite in PlayBASIC,  the graphics that represent this sprite are merged (pixel by pixel) with whatever we're drawing onto, which could be the screen or another image.    So the drawing process modifies the surface we're drawing onto.  






659_minifly

#2
Yes my problem was the first one you write.
In fact i went to read differents post and i found one with the same problem,but i did not know the solution for it.
Thank for your answer and your sample who is working for good.
But in this case how to kept the background image made with a lot off tiles?
PlayBASIC Code: [Select]
; PROJECT : lab02
; AUTHOR : Microsoft
; CREATED : 11/06/2010
; EDITED : 12/06/2010
; ---------------------------------------------------------------------

; ---------------------------------------------------------------------

ForceDeclaration

Declaration

f$,a$,I,J,Dude$,Mur$,Porte01$,Porte02$,Sol$,Monst01$,Monst02$,Monst03$
Monst04$,Monst05$,Parametre$,Long_Chain_Param,Mot$,Pos_Caractere
CR$,LF$,I_p,J_p,I_Indice_tableauX,J_Indice_tableauX,I_Sprite_X
I_Tableau_Max,j_Tableau_Max,dummy$

EndDeclaration

dim Spr_Tableau$(50,50)


Mot$="x"

CR$=chr$(13)

Lf$=chr$(10)

rem ******************* Lecture fichier Level *****************

REM Ouverture du fichier level
readfile "c:\exemples\projets\labyrinthe\level.01",1

REM Lecture du fichier Dude
Dude$=readString$ (1)

REM Lecture du fichier Mur
Mur$=readString$ (1)

REM Lecture du fichier Porte01
Porte01$=readString$ (1)

REM Lecture du fichier Porte02
Porte02$=readString$ (1)

REM Lecture du fichier Sol
Sol$=readString$ (1)

REM Lecture du fichier Monst01$
Monst01$=readString$ (1)

REM Lecture du fichier Monst02$
Monst02$=readString$ (1)

REM Lecture du fichier Monst03$
Monst03$=readString$ (1)

REM Lecture du fichier Monst03$
Monst04$=readString$ (1)

REM Lecture du fichier Monst04$
Monst05$=readString$ (1)

REM Lecture paramètres de la fenêtre
Parametre$=readString$ (1)

REM On retire les blancs de devant et de l'arrière de la chaine
parametre$=trim$(parametre$)

REM Longueur de la chaine parramètres

Long_Chain_Param=len(parametre$)

Pos_Caractere=Instring(parametre$,Mot$)

I_Tableau_Max=Val(Left$(parametre$,Pos_Caractere-1))
J_Tableau_Max=Val(Right$(parametre$,Long_Chain_Param-Pos_Caractere))

while EndofFile(1)=0

for I=1 to I_Tableau_Max
for J=1 to J_Tableau_Max

Spr_Tableau$(I,J)=ReadChr$( 1,1)

Next J
dummy$= ReadChr$( 1,2)

Next I
ENDWHILE

closeFile 1
REM ******************* Fin lecture fichier Level *******************************
; Creation fenetre graphique

openscreen J_Tableau_Max*32+92,I_Tableau_Max*32+64,24,1

REM **************** Fill up the Maze **********************

for I=1 to I_Tableau_Max step 1

for J=1 to J_Tableau_Max step 1
if Spr_Tableau$(I,J)="M"

LoadFxImage "c:\Exemples\Projets\Labyrinthe\mur.bmp",J+ J_Tableau_Max*i
DrawImage J+ J_Tableau_Max*i,j*32,i*32,1

else

if Spr_Tableau$(I,J)="P"
LoadFxImage "c:\Exemples\Projets\Labyrinthe\door2.bmp",J+ J_Tableau_Max*i
DrawImage J+ J_Tableau_Max*i,j*32,i*32,0

else

if Spr_Tableau$(I,J)="X"

LoadFxImage "c:\Exemples\Projets\Labyrinthe\r_r.bmp",J+ J_Tableau_Max*i

I_sprite_X=J+ J_Tableau_Max*i

CreateSprite I_sprite_X+1
SpriteImage I_sprite_X+1, I_sprite_X

; Position the sprite to the start
PositionSprite I_sprite_X+1,j*32,i*32
DrawSprite I_sprite_X+1

I_Indice_tableauX=I
J_Indice_TableauX=J

endif
endif
endif
Next j
Next i

; *********************** End filling the Map ************************************
;
; ******************* Moving the sprite ***********************
setfps 10
do

; REM code 37 de la touche flêche à gauche du clavier.
if leftkey()=1

; Check if we can move to the left
cls 0
if Spr_Tableau$(I_Indice_tableauX,J_Indice_TableauX-1)=chr$(32)
Login required to view complete source code



Big C.

#3
hi minifly,

QuoteBut in this case how to kept the background image made with a lot off tiles?

here is a way based on a tutorial I have written time ago (here you will find the tutorial)

I hope the following code will help and anwser some question from you.

PlayBASIC Code: [Select]
; PROJECT : Maze_Solution
; AUTHOR : Big C.
; CREATED : 13.06.2010
; EDITED : 13.06.2010
; ---------------------------------------------------------------------

ForceDeclaration

Declaration
Mur, Dude, Back, Background, Port, Hole, Image
Map$, Map_Height, Map_Width, Map_Row, Map_Col
Row$, ReturnString$
MyDude, MyDude_X, MyDude_Y, Dude_X, Dude_Y
Screen_OffSet_X, Screen_OffSet_Y
EndDeclaration

;Open the GameWindow
OpenScreen 800, 600, 16, 1
TitleScreen (PlayBasic$ + " - MAZE EXAMPLE")

;Load needed GFX. This is not the best way but for the moment we will start
;with them
Mur = LoadNewImage("bmp\mur.bmp")
Port = LoadNewImage("bmp\door2.bmp")
Hole = LoadNewImage("bmp\hole.bmp")
Back = LoadNewImage("bmp\background_2.bmp")
Dude = LoadNewImage("bmp\r_r.bmp")

;We need the Map Dimensions. So we read the values from Data Section
Map_Width = ReadData()
Map_Height = ReadData()

;The Map will declared as an 2D-Array with the Dimensons above
;But we will start at 0,0 to 14,14
;The Map Array will represent our Map in Chars to check later
;if we can move or not or do something collision work
Dim Map$(Map_Width-1, Map_Height-1)

;Load the Map-Data in to the Map-Array (this can do from external File too)
;and build the Background in one big Image
Background = NewImage(Map_Width*32, Map_Height*32)

RenderToImage Background

For Map_Row = 0 To Map_Height-1
Row$ = ReadData$()
;Split the Row in his Chars and assign the values to the Map
For Map_Col = 0 To Map_Width-1
Map$(Map_Col, Map_Row) = Mid$(Row$,Map_Col+1,1)
Select Map$(Map_Col, Map_Row)
Case "M"
Image = Mur
Case "P"
Image = Port
Case "H"
Image = Hole
Case "."
Image = Back
Case "X"
;"X" satnds for the Player and will be a sprite
;so we have to substitute the position with the background tile
Map$(Map_Col, Map_Row) = "."
Image = Back
;we save the payers starting position
Dude_X = Map_Col
Dude_Y = Map_Row
EndSelect
;draw the Tiles from the Map on our Background Image
DrawImage Image, (Map_Col)*32, (Map_Row)*32, 1
RemStart Testcode
ReturnString$ = ReturnString$ + Map$(Map_Col, Map_Row)
RemEnd

Next Map_Col
RemStart Testcode
#Print ReturnString$
ReturnString$ =""
RemEnd

Next Map_Row

RenderToScreen

;To Center the Playfield we define Screen-Offsets
Screen_Offset_X = (GetScreenWidth()-GetImageWidth(Background))/2
Screen_OffSet_Y = (GetScreenHeight()-GetImageHeight(Background))/2

;Define Players X-Pos
MyDude_X = Screen_Offset_X + (Dude_X*32)

;Define Players Y-Pos
MyDude_Y = Screen_Offset_Y + (Dude_Y*32)

;Create the Player as Sprite
MyDude = NewSprite(MyDude_X, MyDude_Y, Dude)

;Main Loop
Do
;Clear the Screen
Cls 0

;Draw the created Background every loop
DrawImage Background, Screen_Offset_X, Screen_OffSet_Y, 1

Text 10, 10, "Dude_X : " + Str$(Dude_X)
Text 10, 20, "Dude_Y : " + Str$(Dude_Y)

;Get Key_Input
If RightKey()
; Check if next space can be moved into
If Map$(Dude_X+1, Dude_Y) = "."
Dude_X = Dude_X + 1
MoveSprite MyDude, 32, 0
EndIf
FlushKeys
EndIf

If LeftKey()
; Check if next space can be moved into
If Map$(Dude_X-1, Dude_Y) = "."
Dude_X = Dude_X - 1
MoveSprite MyDude, -32, 0
EndIf
FlushKeys
EndIf

If UpKey()
; Check if next space can be moved into
If Map$(Dude_X, Dude_Y-1) = "."
Dude_Y = Dude_Y - 1
MoveSprite MyDude, 0, -32
EndIf
FlushKeys
EndIf

If DownKey()
; Check if next space can be moved into
If Map$(Dude_X, Dude_Y+1) = "."
Dude_Y = Dude_Y + 1
MoveSprite MyDude, 0, 32
EndIf
FlushKeys
EndIf

;Draw the Player
DrawAllSprites

Sync

Loop

Login required to view complete source code


Question to Kevin:

Why will this code taken from the last Code from minifly
Quoteparametre$=trim$(parametre$)

not produce a error because there is left the second parameter or is this set by default?

Big C.

659_minifly

Hi Big C.

Thank for your programme It is very interesting and i learnt a lot with it.

But in another hand i prefer to have the level into a file .
I explain me :
Into data the maze is into the *.exe and nobody can touch it . But i would like that player can make their own maze and use their own sprite. For the game . I just start with this game. On the end the gamer will have the possibility to add their traps and monsters to in following simple rules . Perhaps i will writte a front programme that the user will give is own rules.

I am amazing how fast you gave me the solution to my problem. excuse me for that question : Is there a long time you program with PB ?

Big C.

#5
Glad to read that I could help you a little bit   :)

QuoteBut in another hand i prefer to have the level into a file .

This is not a great problem if you read the very good helpfiles (F1)  ;)

In the attachment i will show you a way by using the library Ini Files

To use a ini file I have rewritten your level.01 file and renamed it to map_level.01

Have fun to study it (and read the Help on Keyword)

to Kevin:
I think I have found a bug in the IniFiles.pba. Didn't know if there are more bugs.

PlayBASIC Code: [Select]
Function GetIniSectionName(FileName$, SectionIndex)
Local Result$ = ""
If FileExist(FileName$) = 0 Then Exitfunction 0
Local FChannel = GetFreeFile()
ReadFile FileName$, FChannel
Local Counter = 0
While EndOfFile(FChannel) = 0
Local Line$ = ReadString$(FChannel)
If (InString(Line$, "[", 1, 0) = 1) And (InString(Line$, "]", 1, 0) = Len(Line$))
Inc Counter
If Counter = Section ;<------ This line have to change to: If Counter = SectionIndex (See Parameter in Function Name)
Result$ = Line$
ExitWhile
EndIf
EndIf
EndWhile
CloseFile FChannel
EndFunction Result$




Big C.

kevin

#6
Quotenot produce a error because there is left the second parameter or is this set by default?

 See Here


QuoteI think I have found a bug in the IniFiles.pba. Didn't know if there are more bugs.

 Well, why post it here ?


Edit: Updated InFiles. 

659_minifly

@Big C.
I got your program look very impressiv.But i have an error for running it Warning Fatal run time error
Fatal error Image #0  out of legal range ( o to 1000).Error Number (300) at line (473) (That is wrong number of line)
Drawimage backround,screen_offset_x,screnne_offset_y,1.Error caused by : Drawimage. i try to find the error but i coulnd'not.Thanks

Big C.

minifly,

this is the reason I post the bug in the Inifiles.pba in this threat because I didn't know if the bug exists in later retail versions too. Kevin has confirm the bug and correct it. I think a new library file will be in one of the next retail or beta updates. If you do not want to wait, then you must correct the bug and the code will do his job. Here the Guidance:

1. change to the slib folder in your PB Install folder
2. looking for file IniFiles.pba
3. make a backup file from your original so we can modify the original file
4. open inifiles.pba with a texteditor
5. looking for row If Counter = Section (will be around line No. 59) and change the line with If Counter = SectionIndex
6. save your change and run the code

Big C.

659_minifly

Big C. I modified the file and i try that is ok WORKS GREAT.
Thank a lot