- Restructuration de ton code.
Code : Tout sélectionner
;***********************************************************************
; GALACTIC SHOOTER 2020
;***********************************************************************
EnableExplicit
Enumeration Fenetres
#window_0
EndEnumeration
Enumeration Sprites
#spr_Menu
#Player
#Bullet ; Modele d'un bullet
#Target ; Modele d'une cible
EndEnumeration
Global Event_Window
Global Stage ; Indicateur de scene
Global Life ; Indicateur de vie
Global n ; Pour boucle de création des cibles et bullets
Global FontGlobal ; Police de jeu
Global FontStrong ; Police affichage gros titre
Global x, y ; Position joueur
Structure NewSprite
idSprite.i
x.i
y.i
EndStructure
Global NewList Bullets.NewSprite() ; Memorisation des tirs
Global NewList Targets.NewSprite() ; Mémorisation des cibles
; Sommaire
Declare Start() ; Init et boucle rendering
Declare ecran_main() ; Scene Accueil
Declare ecran_jeu() ; Scene Combat
Declare Exit() ; Sortie de l'application
Start()
; Initialisation du jeu
Procedure Start()
UseJPEGImageDecoder()
UsePNGImageDecoder()
UseOGGSoundDecoder()
FontGlobal = LoadFont(#PB_Any, "Arial", 30)
FontStrong = LoadFont(#PB_Any, "Arial", 100)
If InitSprite()=0 Or InitKeyboard()=0 Or InitMouse()=0 Or InitSound()=0
MessageRequester("Erreur", "Impossible d'initialiser le jeu ")
Exit()
EndIf
OpenWindow(#window_0,0,0,1024,768,"Galactic Shooter",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(#window_0),0,0,1024,768)
; Chargement du sprite d'accueil
LoadSprite(#spr_Menu,"images/menu.png")
; Création du player (Ou chargement d'un sprite)
CreateSprite(#Player, 32, 32)
If StartDrawing(SpriteOutput(#player))
Box(0, 0, 32, 32, RGB(255, 0, 0))
StopDrawing()
EndIf
; Création du modele de bullet (Ou chargement d'un sprite)
CreateSprite(#Bullet, 32, 5)
If StartDrawing(SpriteOutput(#Bullet))
Box(0, 0, 32, 5, RGB(255, 0, 0))
StopDrawing()
EndIf
; Création du modele de cible (Ou chargement d'un sprite)
CreateSprite(#Target, 32, 32)
If StartDrawing(SpriteOutput(#Target))
Box(0, 0, 32, 32, RGB(0, 0, 255))
StopDrawing()
EndIf
; Boucle rendering
Repeat
Repeat
Event_Window = WindowEvent()
Select Event_Window
Case #PB_Event_CloseWindow
Exit()
EndSelect
Until Event_Window = 0
ExamineKeyboard()
ClearScreen(RGB(0, 0, 0))
Select Stage
Case 0 : ecran_main() ; Accueil
Case 1 : ecran_jeu() ; Combat
EndSelect
FlipBuffers()
ForEver
EndProcedure
;-
;- Scenes
; Scene d'accueil
Procedure ecran_main()
DisplaySprite(#spr_Menu, 0, 0)
If KeyboardReleased(#PB_Key_Space)
; Création des cibles
; Les cibles sont disposés à droite en dehors de l'ecran
x = 100
y = ScreenHeight()/2
ClearList(Targets())
ClearList(Bullets())
For n = 1 To 20
AddElement(Targets())
Targets()\idSprite = CopySprite(#Target, #PB_Any)
Targets()\x = ScreenWidth() + Random(2000)
Targets()\y = Random(ScreenHeight()-128, 128)
Next
Life = 1 ; Attribution du nombre de vie
Stage = 1 ; Passage à la premiere scene
EndIf
If KeyboardReleased(#PB_Key_Escape)
Exit()
EndIf
EndProcedure
; Scene de combat
Procedure ecran_jeu()
Static LastFire
Protected Speed = 5, Collision
; Evenement clavier
If life
If KeyboardPushed(#PB_Key_Left)
x-Speed
EndIf
If KeyboardPushed(#PB_Key_Right)
x+Speed
EndIf
If KeyboardPushed(#PB_Key_Up)
y-Speed
EndIf
If KeyboardPushed(#PB_Key_Down)
y+Speed
EndIf
EndIf
; Un tir est actionné
If KeyboardPushed(#PB_Key_Space) And Life
; Pas plus d'un tir par demie seconde
If ElapsedMilliseconds() - LastFire > 500 Or LastFire = 0
LastFire = ElapsedMilliseconds() + 1 ; +1 Pour éviter un Lasfire = 0
AddElement(Bullets())
Bullets()\idSprite = CopySprite(#Bullet, #PB_Any)
Bullets()\x = x + SpriteWidth(#Player) + 5
Bullets()\y = y + SpriteHeight(#Player)/2
EndIf
EndIf
If KeyboardReleased(#PB_Key_Escape)
Stage = 0
EndIf
; Affichage des sprites
; Affichage du joueur
DisplaySprite(#Player, x, y)
; Affichage des bullet
If Life
ForEach Bullets()
Bullets()\x + 10
If Bullets()\x < ScreenWidth()
DisplaySprite(Bullets()\idSprite, Bullets()\x, Bullets()\y)
Else
DeleteElement(Bullets())
EndIf
Next
EndIf
; Affichages des cibles
ForEach Targets()
If life
Targets()\x - 5
EndIf
If Targets()\x < SpriteWidth(Targets()\idSprite)
Targets()\x = ScreenWidth() + Random(1000)
EndIf
DisplaySprite(Targets()\idSprite, Targets()\x, Targets()\y)
Next
; Gestion des collisions
;- Collision Bullet -> Cible
ForEach Bullets()
ForEach Targets()
If SpriteCollision(Bullets()\idSprite, Bullets()\x, Bullets()\y,
Targets()\idSprite, Targets()\x, Targets()\y)
Collision = #True
DeleteElement(Targets())
EndIf
Next
If Collision
DeleteElement(Bullets())
EndIf
Next
;- Collision Player -> Cible
ForEach Targets()
If SpriteCollision(#Player, x, y, Targets()\idSprite, Targets()\x, Targets()\y) And Life
Life - 1
EndIf
Next
;- Affichage informations
If StartDrawing(ScreenOutput())
DrawingMode(#PB_2DDrawing_Transparent)
; Nombres de cibles restantes
DrawingFont(FontID(FontGlobal))
DrawText(20, 20, "Cibles : " + Str(ListSize(Targets())), RGB(255, 0, 0))
; C'est gagné :)
If ListSize(Targets()) = 0
DrawingFont(FontID(FontStrong))
DrawText(20, 300, "Yeah! Gagné :)", RGB(0, 255, 0))
DrawingFont(FontID(FontGlobal))
DrawText(20, ScreenHeight() - 50, "[Esc] - Retour à l'écran d'accueil", RGB(255, 0, 0))
EndIf
; C'est perdu (:
If Life = 0
DrawingFont(FontID(FontStrong))
DrawText(400, 300, "Perdu", RGB(255, 0, 0))
DrawingFont(FontID(FontGlobal))
DrawText(20, ScreenHeight() - 50, "[Esc] - Retour à l'écran d'accueil", RGB(255, 0, 0))
EndIf
StopDrawing()
EndIf
EndProcedure
;-
Procedure Exit()
End
EndProcedure
Barre d'espacement pour tirer.