Création d'un jeu 2D (Gestion lumière, ...)

Vous débutez et vous avez besoin d'aide ? N'hésitez pas à poser vos questions
Avatar de l’utilisateur
Dumli
Messages : 75
Inscription : dim. 14/févr./2010 22:28
Localisation : Sud de la France

Création d'un jeu 2D (Gestion lumière, ...)

Message par Dumli »

Salut tout le monde :D
Je voudrais me lancer dans la conception d'un mini-jeu où le personnage qu'on incarne se déplace dans un monde en 2D. Je pense que la principale difficulté va surtout être dans la gestion de la lumière ... Je voudrais au final obtenir un résultat semblable à celui la : http://www.mojang.com/notch/j4k/l4kd/ Est-ce réalisable ? Le langage Basic est il adapté pour faire ceci ?
Voici une image explicative :
Image
Merci d'avance :)

Pour le moment, j'ai "réussi" à faire ça, après une journée de travail ( oui, je sais c'est peu :cry: ). La direction à la souris lag énormément chez moi, je ne comprends pas :? De plus, la collision est buggée puisque un simple contact avec un mur fige le joueur. Pour la collision, j'ai essayais d'utiliser un "calque" en noir et blanc pour détecter la collision simplement quand il y a du blanc :

Code : Tout sélectionner

InitSprite()
InitSprite3D()
InitKeyboard()
InitMouse()

#player=0
#ply = 1
#map = 2
#col = 3
ox = 800/2
oy= 600/2
x=ox
y=oy
v = 3

OpenWindow(0,0,0,800,600,"Zombie Land v0.1", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)

CreateSprite(#Ply,30,10,#PB_Sprite_Texture)
StartDrawing(SpriteOutput(#Ply))
Box(0,0,30,10,#Blue)
StopDrawing()

CreateSprite3D(#player,#ply)

UsePNGImageDecoder()

If LoadImage(#map,"map.png")=0 : Debug "Error load : map.png" : EndIf
If LoadImage(#col,"col.png")=0 : Debug "Error load : col.png" : EndIf

CreateSprite(5,800*2,600*2)
StartDrawing(SpriteOutput(5))
DrawImage(ImageID(#col),0,0,800*2,600*2)
StopDrawing()

Repeat
  
  ExamineMouse()
  ExamineKeyboard()
  
  mx = MouseX()
  my = MouseY()
  
  Repeat
    
    event = WindowEvent()
    
    If event = #PB_Event_CloseWindow
      End
    EndIf
    
  Until event=0
  
  If SpritePixelCollision(#ply,ox,oy,5,x,y)
    col=1
  Else
    col=0
  EndIf
  
  If KeyboardPushed(#PB_Key_Up) And col<>1
    y+v
  EndIf
  
  If KeyboardPushed(#PB_Key_Down) And col<>1
    y-v
  EndIf
  
  If KeyboardPushed(#PB_Key_Right) And col<>1
    x-v
  EndIf
  
  If KeyboardPushed(#PB_Key_Left) And col<>1
    x+v
  EndIf
  
  If mx-ox <>0 : calc = (my-oy)/(mx-ox) : EndIf
  ang = Degree(ATan(calc)) + 90
  
  If (my < oy And mx < ox) Or (my > oy And mx < ox)
    ang + 180
  Else
    ang + 360
  EndIf
  
  FlipBuffers()
  ClearScreen(#Black)
  
  StartDrawing(ScreenOutput())
  DisplayTransparentSprite(5,x,y)
  DrawImage(ImageID(#map),x,y,800*2,600*2)
  Circle(MouseX(),MouseY(),2,#Red)
  StopDrawing()
  
  Start3D()
  RotateSprite3D(#Player,ang,#PB_Absolute)
  DisplaySprite3D(#player,800/2,600/2)
  Stop3D()
  
ForEver
Les images :
Image
Image
by Typhoon :mrgreen:
Windows 7 Ultimate 64-Bit | Intel Core i5 CPU 750 @ 2.67GHz | 4,0 GB RAM | Sapphire Vapor-X ATI Radeon HD 4890 | 2,5 TeraOctet 7200 RPM
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par blendman »

pour la lumière, j'avais posté ce code trouvé sur un site russe, qui après adaptation avec les sprites3D donne d'excellents résultats :

http://www.purebasic.fr/french/viewtopi ... =1&t=12376
Avatar de l’utilisateur
Dumli
Messages : 75
Inscription : dim. 14/févr./2010 22:28
Localisation : Sud de la France

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par Dumli »

Merci ! :) Mais le problème c'est qu'il y a un "label manquant" quand je compile. Je n'arrive pas à corriger cette erreur :?
Windows 7 Ultimate 64-Bit | Intel Core i5 CPU 750 @ 2.67GHz | 4,0 GB RAM | Sapphire Vapor-X ATI Radeon HD 4890 | 2,5 TeraOctet 7200 RPM
G-Rom
Messages : 3641
Inscription : dim. 10/janv./2010 5:29

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par G-Rom »

Salut , ca fait un bail que je ne suis pas intervenu , si j'ai un conseil à te donner , utilise la géométrie pour résoudre ton problème de lumière , je m'explique :

en admettant que ta lumière est omnidirectionnel ( un cercle donc ) avec le centre C de rayon R
tu créer des triangles (ABC) qui ont pour sommet le centre C avec une longueur de R

de cette manière par exemple :

Code : Tout sélectionner

OpenWindow(0,0,0,800,600,"")


#LIGHT_STEP = 36


StartDrawing(WindowOutput(0))

  CtrX.f = 400
  CtrY.f = 300
  R = 200
  
  For Angle = 0 To 360 - #LIGHT_STEP  Step #LIGHT_STEP
  
    Ax.f = CtrX
    Ay.f = CtrY
    
    Bx.f = Ax + R * Cos(Angle * #PI / 180 )
    By.f = Ay + R * Sin(Angle * #PI / 180 )
    
    Cx.f = Ax + R * Cos((Angle+#LIGHT_STEP) * #PI / 180 )
    Cy.f = Ay + R * Sin((Angle+#LIGHT_STEP) * #PI / 180 )
  
    LineXY(Ax,Ay,Bx,By,$0000FF)
    LineXY(Bx,By,Cx,Cy,$0000FF)
  
  Next 

StopDrawing()


Repeat
  event = WindowEvent()
Until event = #PB_Event_CloseWindow
Evidemment tu sauvegardes les coordonnées dans un endroit structuré , par dessus ces coordonnées tu appliques pour chaque triangle un Sprite 3D que tu déformes via TransformSprite3D() ( pour lui donner la forme du triangle ) se sprite3D devra avoir une texture en forme de dégradé du blanc ( le centre du cercle) au noir ( la base de chaque triangle )
Au final du devrais avoir un jolie cercle blanc au centre qui font vers le noir.

ensuite je te laisse lire ceci :

http://gregouar.developpez.com/tutoriel ... miques-2d/

tout à fait applicable PB , il te faut quelques notions de math quand même, mais le résultat est bluffant ;)
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par blendman »

salut

Si tu arrives à obtenir un résultat, surtout n'hésites pas à poster, car ça m'intéresse énormément cette technique ! :D

Pour ton problème de label, il faut juste une image bmp (un carré de 64*64 par exemple) que tu notes "1.bmp" et que tu places dans le meme dossier que celui de ton fichier pb.

Cela a rapport avec ceci :

Code : Tout sélectionner

DataSection
b1:
IncludeBinary "1.bmp"
EndDataSection
Avatar de l’utilisateur
Dumli
Messages : 75
Inscription : dim. 14/févr./2010 22:28
Localisation : Sud de la France

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par Dumli »

Merci à vous deux :D
J'ai bossé vos codes et j'ai réussi à faire les Sprite3D en forme de triangle. Cependant, je ne parviens pas à faire une chose toute simple : un dégradé (Affichage 3, voir code).
J'obtiens un cercle sans dégradé alors que je voudrais obtenir ceci :
Image

Voici le code :

Code : Tout sélectionner

InitSprite()
InitSprite3D()
InitKeyboard()

OpenWindow(0,0,0,800,600,"", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)


#LIGHT_STEP = 18
#Tri = 1

CtrX.f = 400
CtrY.f = 300
R = 200
aff = 1 ; Variable à changer pour avoir les différents affichages : 1,2 ou 3

For Angle = 0 To 360 - #LIGHT_STEP  Step #LIGHT_STEP
  
  n+1
  
  Ax.f = CtrX
  Ay.f = CtrY
  
  Bx.f = Ax + R * Cos(Angle * #PI / 180 )
  By.f = Ay + R * Sin(Angle * #PI / 180 )
  
  Cx.f = Ax + R * Cos((Angle+#LIGHT_STEP) * #PI / 180 )
  Cy.f = Ay + R * Sin((Angle+#LIGHT_STEP) * #PI / 180 )
  
  If aff = 1 : StartDrawing(ScreenOutput()) : LineXY(Ax,Ay,Bx,By) : LineXY(Bx,By,Cx,Cy) : StopDrawing() : EndIf ; Affichage 1 : Lignes de construction
  
  CreateSprite(n,R,R,#PB_Sprite_Texture)
  StartDrawing(SpriteOutput(n))
  Box(0,0,R,R,#Green)
  StopDrawing()
  CreateSprite3D(n,n)
  
  TransformSprite3D(n,0,0,Bx-Ax,By-Ay,Cx-Ax,Cy-Ay,0,0)
  
Next angle

If aff = 2 ; Affichage 2 : Sprite3D Triangulaire
  Start3D()
  For n = 1 To 20
    DisplaySprite3D(n,Ax,Ay)
  Next n
  Stop3D() 
EndIf

If aff = 3 ; Affichage 3 : Dégradé
  StartDrawing(ScreenOutput())
  DrawingMode(#PB_2DDrawing_Gradient)
  BackColor(#Black)
  FrontColor(#Green)
  CircularGradient(Ax,Ay,200)
  Circle(Ax,Ay,200)
  StopDrawing()
EndIf

Repeat
  
  event = WindowEvent()
  
Until event = #PB_Event_CloseWindow
:roll:
Windows 7 Ultimate 64-Bit | Intel Core i5 CPU 750 @ 2.67GHz | 4,0 GB RAM | Sapphire Vapor-X ATI Radeon HD 4890 | 2,5 TeraOctet 7200 RPM
G-Rom
Messages : 3641
Inscription : dim. 10/janv./2010 5:29

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par G-Rom »

utilise un dégradé linéaire comme ceci : http://www.tutomaker.com/tutoriaux/imag ... -blanc.jpg que tu applique a ton sprite 3D ?
Backup
Messages : 14526
Inscription : lun. 26/avr./2004 0:40

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par Backup »

en Pb les degradés ne marchent qu'avec les images ;)

Code : Tout sélectionner

InitSprite()
InitSprite3D()
InitKeyboard()

OpenWindow(0,0,0,800,600,"", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)

aX.f = 400
aY.f = 300

If CreateImage(0, 800, 600)
	StartDrawing(ImageOutput(0))
		
		DrawingMode(#PB_2DDrawing_Gradient)     
		BackColor(#Green)
		FrontColor(#black)
		
		CircularGradient(Ax, Ay, 200)     
		Circle(Ax, Ay, 200)
		
		
	StopDrawing()
	ImageGadget(0, 0, 0, 400, 200, ImageID(0))
EndIf
StartDrawing(ScreenOutput())
	DrawAlphaImage(ImageID(0),0,0)
StopDrawing()


Repeat
	
	event = WindowEvent()
	
Until event = #PB_Event_CloseWindow
ensuite , faut te servir de cette image pour mapper ton sprite
Dernière modification par Backup le mer. 21/déc./2011 11:44, modifié 1 fois.
Avatar de l’utilisateur
blendman
Messages : 2017
Inscription : sam. 19/févr./2011 12:46

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par blendman »

OU alors, tu utilises une image comme dégradé, ça n'irait pas ?

En tout cas, surtout, continue dans ta lancée, car si tu parviens à ajouter ce système de projection d'ombre dans purebasic, ça va être fabuleux ! :D.
Avatar de l’utilisateur
Dumli
Messages : 75
Inscription : dim. 14/févr./2010 22:28
Localisation : Sud de la France

Re: Création d'un jeu 2D (Gestion lumière, ...)

Message par Dumli »

Merci ! :mrgreen:

Le problème c'est la transparence du noir :x Je suis en train de bosser dessus, je vous tiens au courant :wink:

Edit : Victoire ! J'ai réussi :!: Cependant, je ne sais pas comment rentrer le dégradé dans le sprite3D triangulaire :( J'ai pas était clair. Non ? 8O

Code : Tout sélectionner

InitSprite()
InitSprite3D()
InitKeyboard()

OpenWindow(0,0,0,800,600,"Moteur de lumière 2D", #PB_Window_ScreenCentered | #PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,800,600,0,0,0)


#LIGHT_STEP = 18
#Tri = 0

x=100
y=0
CtrX.f = 400
CtrY.f = 300
R = 200
aff = 6 ; Variable à changer pour avoir les différents affichages : 1,2 ou 3

CreateSprite(#Tri,800,600)

For angle = 0 To 360 - #LIGHT_STEP  Step #LIGHT_STEP
  
  n+1
  
  Ax.f = CtrX
  Ay.f = CtrY
  
  Bx.f = Ax + R * Cos(Angle * #PI / 180 )
  By.f = Ay + R * Sin(Angle * #PI / 180 )
  
  Cx.f = Ax + R * Cos((Angle+#LIGHT_STEP) * #PI / 180 )
  Cy.f = Ay + R * Sin((Angle+#LIGHT_STEP) * #PI / 180 )
  
  StartDrawing(SpriteOutput(#Tri)) : LineXY(Ax,Ay,Bx,By) : LineXY(Bx,By,Cx,Cy) : StopDrawing()
  
  If CreateImage(1, R, R,32)
    
    StartDrawing(ImageOutput(1))
    DrawingMode(#PB_2DDrawing_Gradient)     
    BackColor(#Green)
    FrontColor(#Black)
    CircularGradient(0, 0, R)     
    Circle(0, 0, R)
    StopDrawing()
    
  EndIf
  
  CreateSprite(n,R,R,#PB_Sprite_Texture)
  StartDrawing(SpriteOutput(n))
  DrawImage(ImageID(1),0,0)
  StopDrawing()
  CreateSprite3D(n,n)
  
  TransformSprite3D(n,0,0,Bx-Ax,By-Ay,0,0,Cx-Ax,Cy-Ay)
  
Next angle

If aff<>6
  Debug "error"
  For angle = 0 To 360 - #LIGHT_STEP  Step #LIGHT_STEP
    
    n+1
    
    Ax.f = CtrX
    Ay.f = CtrY
    
    Bx.f = Ax + R * Cos(Angle * #PI / 180 )
    By.f = Ay + R * Sin(Angle * #PI / 180 )
    
    Cx.f = Ax + R * Cos((Angle+#LIGHT_STEP) * #PI / 180 )
    Cy.f = Ay + R * Sin((Angle+#LIGHT_STEP) * #PI / 180 )
    
    StartDrawing(SpriteOutput(#Tri)) : LineXY(Ax,Ay,Bx,By) : LineXY(Bx,By,Cx,Cy) : StopDrawing()
    
    CreateSprite(n,R,R,#PB_Sprite_Texture)
    StartDrawing(SpriteOutput(n))
    Box(0,0,R,R,#Green)
    StopDrawing()
    CreateSprite3D(n,n)
    
    TransformSprite3D(n,0,0,Bx-Ax,By-Ay,Cx-Ax,Cy-Ay,0,0)
    
  Next angle
EndIf

For n = 21 To 23
  
  If n=21
    r=255
    g=0
    b=0
  ElseIf n=22
    r=0
    g=255
    b=0
  ElseIf n=23
    r=0
    g=0
    b=255
  EndIf
  
  If CreateImage(n, 800, 600,32)
    
    StartDrawing(ImageOutput(n))
    DrawingMode(#PB_2DDrawing_Gradient)     
    BackColor(RGB(r,g,b))
    FrontColor(#Black)
    CircularGradient(Ax, Ay, 200)     
    Circle(Ax, Ay, 200)
    StopDrawing()
    
    ImageGadget(n, 0, 0, 400, 200, ImageID(n))
    
  EndIf
  
  CreateSprite(n,800,600,#PB_Sprite_AlphaBlending|#PB_Sprite_Texture)
  StartDrawing(SpriteOutput(n))
  DrawImage(ImageID(n),0,0)
  StopDrawing()
  CreateSprite3D(n,n)
  
Next n

Repeat
  
  Repeat
    Event = WindowEvent()
    If event=#PB_Event_CloseWindow
      End 
    EndIf
  Until Event = 0 
  
  ExamineKeyboard()
  
  If aff = 1
    DisplaySprite(#Tri,0,0)
  EndIf
  
  If aff = 2 ; Affichage 2 : Sprite3D Triangulaire
    Start3D()
    For n = 1 To 20
      DisplaySprite3D(n,Ax,Ay)
    Next n
    Stop3D() 
  EndIf
  
  If Aff=0 ;Buggé
    StartDrawing(ScreenOutput())
    DrawingMode(#PB_2DDrawing_AlphaBlend)
    DrawAlphaImage(ImageID(0),-100,0)
    DrawAlphaImage(ImageID(1),100,0)
    StopDrawing()  
  EndIf
  
  If aff = 4
    DisplayTranslucentSprite(21,-100,0,100)
    DisplayTranslucentSprite(22,100,0,100)
  EndIf
  
  If aff = 5
    Start3D()
    Sprite3DBlendingMode(7,2)
    DisplaySprite3D(21,-100,0)
    DisplaySprite3D(22,0,100)
    DisplaySprite3D(23,x,y)
    Sprite3DBlendingMode(5,6)
    Stop3D()
  EndIf
  
  If aff = 6
    Start3D()
    For n = 1 To 20
      DisplaySprite3D(n,Ax,Ay)
    Next n
    Stop3D()
  EndIf
  
  If aff=7
    StartDrawing(ScreenOutput())
    DrawImage(ImageID(1),Ax-R/2,Ay-R/2)
    StopDrawing()
  EndIf
  
  If KeyboardPushed(#PB_Key_F1)
    aff=1
  EndIf
  If KeyboardPushed(#PB_Key_F2)
    aff=2
  EndIf
  If KeyboardPushed(#PB_Key_F3)
    aff=3
  EndIf  
  If KeyboardPushed(#PB_Key_F4)
    aff=4
  EndIf
  If KeyboardPushed(#PB_Key_F5)
    aff=5
  EndIf
  If KeyboardPushed(#PB_Key_F6)
    aff=6
  EndIf
  If KeyboardPushed(#PB_Key_F7)
    aff=7
  EndIf
  If KeyboardPushed(#PB_Key_Up)
    y-5
  EndIf
  If KeyboardPushed(#PB_Key_Down)
    y+5
  EndIf
  If KeyboardPushed(#PB_Key_Right)
    x+5
  EndIf
  If KeyboardPushed(#PB_Key_Left)
    x-5
  EndIf
  
  FlipBuffers()
  ClearScreen($000000)
  
  Delay(1)
  
Until event = #PB_Event_CloseWindow
Windows 7 Ultimate 64-Bit | Intel Core i5 CPU 750 @ 2.67GHz | 4,0 GB RAM | Sapphire Vapor-X ATI Radeon HD 4890 | 2,5 TeraOctet 7200 RPM
Répondre