J'essaie de modifier un programme trop génial du Soldat Inconnu ('pong') nommé LSI Versus AntiVirus / Publication : Juillet 2011
En fait j'aurais voulu pouvoir choisir 1 ou 2 joueurs , et bouger les raquettes suivant l'appuie de touches clavier (bref utiliser le code pour jouer à pong!)
J'ai dû utiliser OpenScreen (qui me fait malheureusement disparaître la souris quand je survol un objet)
Je ne comprend pas pourquoi
Code : Tout sélectionner
test = ExamineKeyboard(): Debug test
car si j'utilise un programme tout simple sur le même ordi.... il me retourne bien 1 et teste les appuies des touches
ps: je suis débutant merci de votre comprhéension, merci pour l'aide
code modifié:
Code : Tout sélectionner
; *** Jeu PONG *********************
; PureBasic 5.22 LTS (Windows - x86)
; **********************************
; Auteur : Le Soldat Inconnu
; Nom original : LSI Versus AntiVirus
; Publication : Juillet 2011
;
;
;nom des joueurs
#PlayerL ="LBP COBA" ;Gauche
#PlayerR = "LBP Manager" ;Droite
;couleur des raquettes
#RColorL = 44 : #GColorL = 131 : #BColorL = 226 ;Gauche
#RColorR = 44 : #GColorR = 131 : #BColorR = 226 ;Froite
;précision des joueurs
#QualityL = 0.5 ;plus petit = plus précis ;Gauche
#QualityR = 1 ;Droite
;vitesse des raquettes
#VitesseL = 12 ;Gauche
#VitesseR = 12 ;Droite
;vitesse balle
#VitesseB = 18
Procedure Jouer_CasseBriques()
Protected Police, Texte.s, x, y, Couleur
Protected Dim Pivot(24 - 1, 128 - 1)
Protected Image_Raquette_D, Image_Raquette_G, Image_Balle, Image_Score, Image
Protected Fenetre_Raquette_D, Fenetre_Raquette_G, Fenetre_Balle, Fenetre_Score
Protected Raquette_D_Y.f, Raquette_G_Y.f, Balle_X.f, Balle_Y.f, Balle_Dx.f, Balle_Dy.f, Balle_Vitesse.f, Balle_X_Estimation.f, Balle_Y_Estimation.f, Balle_Dy_Estimation.f
Protected Raquette_G_Retour, Raquette_D_Retour, Raquette_G_Y_Estimation.f, Raquette_D_Y_Estimation.f
Protected Score_G, Score_D, Score_Affiche, Score_Gadget, Depart
Protected Ecran_X, Ecran_Y, Ecran_
Police = LoadFont(#PB_Any, "Verdana", 10, #PB_Font_HighQuality)
If Police
Image = CreateImage(#PB_Any, 128, 24, 24)
Image_Score = CreateImage(#PB_Any, 128, 24, 24)
Image_Raquette_D = CreateImage(#PB_Any, 24, 128, 24)
Image_Raquette_G = CreateImage(#PB_Any, 24, 128, 24)
Image_Balle = CreateImage(#PB_Any, 8, 8, 24)
If Image And Image_Raquette_D And Image_Raquette_G And Image_Balle
If ExamineDesktops()
; Analyse de l'écran
Ecran_X = DesktopX(0)
Ecran_Y = DesktopY(0)
Ecran_L = DesktopWidth(0)
Ecran_H = DesktopHeight(0)
If InitSprite() And InitKeyboard() And OpenScreen(Ecran_L, Ecran_H, 32, "FunctionsKeyboard")
; Position par défaut des éléments
Raquette_G_Y = Ecran_Y + (Ecran_H - 128) / 2
Raquette_D_Y = Ecran_Y + (Ecran_H - 128) / 2
Balle_X = Ecran_X + 24 + 1
Balle_Y = Ecran_Y + (Ecran_H - 8) / 2
Balle_Dx = -1
Balle_Dy = 0
Balle_Vitesse = 1
Depart = 1 * 1000 / 20
; Fenêtres
Fenetre_Score = OpenWindow(#PB_Any, Ecran_X + (Ecran_L - 128) / 2, Ecran_Y, 128, 24, #PlayerL+" VS "+#PlayerR, #PB_Window_BorderLess)
If Fenetre_Score
Fenetre_Raquette_G = OpenWindow(#PB_Any, Ecran_X, Raquette_G_Y, 24, 128, #PlayerL, #PB_Window_BorderLess, WindowID(Fenetre_Score))
Fenetre_Raquette_D = OpenWindow(#PB_Any, Ecran_X + Ecran_L - 24, Raquette_D_Y, 24, 128, #PlayerR, #PB_Window_BorderLess, WindowID(Fenetre_Score))
Fenetre_Balle = OpenWindow(#PB_Any, Balle_X, Balle_Y, 8, 8, "balle", #PB_Window_BorderLess, WindowID(Fenetre_Score))
If Fenetre_Raquette_G And Fenetre_Raquette_D And Fenetre_Balle
StickyWindow(Fenetre_Score, 1)
StickyWindow(Fenetre_Balle, 1)
StickyWindow(Fenetre_Raquette_D, 1)
StickyWindow(Fenetre_Raquette_G, 1)
; Dessin de la raquette gauche
StartDrawing(ImageOutput(Image))
Box(0, 0, 128, 24, RGB(#RColorL, #GColorL, #BColorL))
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
Box(0, 0, 128, 24, 0)
DrawingFont(FontID(Police))
Texte.s = #PlayerL
DrawText((128 - TextWidth(Texte)) / 2,(24 - TextHeight(Texte)) / 2, Texte, 0)
; Enregistre l'image pivotée
For x = 0 To 128 - 1
For y = 0 To 24 - 1
Couleur = Point(x, y)
Pivot(y, x) = Couleur
Next
Next
StopDrawing()
StartDrawing(ImageOutput(Image_Raquette_G))
For x = 0 To 24 - 1
For y = 0 To 128 - 1
Couleur = Pivot(x, y)
Plot(x, 128 - 1 - y, Couleur)
Next
Next
StopDrawing()
UseGadgetList(WindowID(Fenetre_Raquette_G))
ImageGadget(#PB_Any, 0, 0, 24, 128, ImageID(Image_Raquette_G))
; Dessin de la raquette droite
StartDrawing(ImageOutput(Image))
Box(0, 0, 128, 24, RGB(#RColorR, #GColorR, #BColorR))
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
Box(0, 0, 128, 24, 0)
DrawingFont(FontID(Police))
Texte = #PlayerR
DrawText((128 - TextWidth(Texte)) / 2,(24 - TextHeight(Texte)) / 2, Texte, 0)
; Enregistre l'image pivotée
For x = 0 To 128 - 1
For y = 0 To 24 - 1
Couleur = Point(x, y)
Pivot(24 - 1 - y, x) = Couleur
Next
Next
StopDrawing()
StartDrawing(ImageOutput(Image_Raquette_D))
For x = 0 To 24 - 1
For y = 0 To 128 - 1
Couleur = Pivot(x, y)
Plot(x, y, Couleur)
Next
Next
StopDrawing()
UseGadgetList(WindowID(Fenetre_Raquette_D))
ImageGadget(#PB_Any, 0, 0, 24, 128, ImageID(Image_Raquette_D))
; Dessin de la balle
StartDrawing(ImageOutput(Image_Balle))
Box(0, 0, 8, 8, $00FFFF)
DrawingMode(#PB_2DDrawing_Outlined)
Box(0, 0, 8, 8, 0)
StopDrawing()
UseGadgetList(WindowID(Fenetre_Balle))
ImageGadget(#PB_Any, 0, 0, 8, 8, ImageID(Image_Balle))
UseGadgetList(WindowID(Fenetre_Score))
Score_Gadget = ImageGadget(#PB_Any, 0, 0, 8, 8, ImageID(Image_Score))
Score_Affiche = 1
AddWindowTimer(Fenetre_Score, 999, 20)
Repeat
Event = WaitWindowEvent()
If EventWindow() = Fenetre_Score
Select Event
Case #PB_Event_Gadget
Select EventGadget()
Case Score_Gadget
Select EventType()
Case #PB_EventType_LeftClick
If MessageRequester("LSI VS AntiVirus", "Souhaitez-vous quitter cette partie passionnante ?", #PB_MessageRequester_YesNo) = #PB_MessageRequester_Yes
Quitter = 1
EndIf
EndSelect
EndSelect
Case #PB_Event_Timer
Select EventTimer()
Case 999
; Animation
If Depart = 0
; Position de la balle
Balle_X + Balle_Dx * Balle_Vitesse
Balle_Y + Balle_Dy * Balle_Vitesse
; Position de la raquette gauche
If Balle_Dx < 0
Balle_X_Estimation = Balle_X
Balle_Y_Estimation = Balle_Y
Balle_Dy_Estimation = Balle_Dy
x = Balle_X_Estimation
y = Balle_Y_Estimation
Repeat
Balle_X_Estimation + Balle_Dx * Balle_Vitesse
Balle_Y_Estimation + Balle_Dy_Estimation * Balle_Vitesse
If (Balle_Y_Estimation <= Ecran_Y And Balle_Dy_Estimation < 0) Or (Balle_Y_Estimation >= Ecran_Y + Ecran_H - 8 And Balle_Dy_Estimation > 0)
x = Abs(Balle_Y_Estimation - y) + Abs(Balle_X_Estimation - x)
Balle_Dy_Estimation * -1
Balle_X_Estimation + (Random(x) - x / 2) * #QualityL
x = Balle_X_Estimation
y = Balle_Y_Estimation
EndIf
Until Balle_X_Estimation <= Ecran_X
x = Abs(Balle_Y_Estimation - y) + Abs(Balle_X_Estimation - x)
Balle_Y_Estimation + (Random(x) - x / 2) * #QualityL
Raquette_G_Y_Estimation * 0.96 + Balle_Y_Estimation * 0.04
Raquette_G_Retour = 0
Else
If Raquette_G_Retour <> 1
Raquette_G_Retour = 1
Raquette_G_Y_Estimation = Random(1000) / 500
Raquette_G_Y_Estimation = 2 * Raquette_G_Y_Estimation - Raquette_G_Y_Estimation * Raquette_G_Y_Estimation - 1
Raquette_G_Y_Estimation = Ecran_Y + Ecran_H / 2 + Ecran_H * Raquette_G_Y_Estimation / 2
EndIf
EndIf
test = ExamineKeyboard()
If KeyboardPushed(#PB_Key_Up)
Debug "up"
EndIf
If KeyboardPushed(#PB_Key_Down)
Debug "down"
EndIf
Debug test
; If Raquette_G_Y_Estimation + 4 < Raquette_G_Y + 48
; Raquette_G_Y - #VitesseL
; ElseIf Raquette_G_Y_Estimation + 4 > Raquette_G_Y + 128 - 48
; Raquette_G_Y + #VitesseL
;EndIf
; Position de la raquette droite
If Balle_Dx > 0
Balle_X_Estimation = Balle_X
Balle_Y_Estimation = Balle_Y
Balle_Dy_Estimation = Balle_Dy
x = Balle_X_Estimation
y = Balle_Y_Estimation
Repeat
Balle_X_Estimation + Balle_Dx * Balle_Vitesse
Balle_Y_Estimation + Balle_Dy_Estimation * Balle_Vitesse
If (Balle_Y_Estimation <= Ecran_Y And Balle_Dy_Estimation < 0) Or (Balle_Y_Estimation >= Ecran_Y + Ecran_H - 8 And Balle_Dy_Estimation > 0)
x = Abs(Balle_Y_Estimation - y) + Abs(Balle_X_Estimation - x)
Balle_Dy_Estimation * -1
Balle_X_Estimation + (Random(x) - x / 2) * #QualityR
x = Balle_X_Estimation
y = Balle_Y_Estimation
EndIf
Until Balle_X_Estimation >= Ecran_X + Ecran_L
x = Abs(Balle_Y_Estimation - y) + Abs(Balle_X_Estimation - x)
Balle_Y_Estimation + (Random(x) - x / 2) * #QualityR
Raquette_D_Y_Estimation * 0.96 + Balle_Y_Estimation * 0.04
Raquette_D_Retour = 0
Else
If Raquette_D_Retour <> 1
Raquette_D_Retour = 1
Raquette_D_Y_Estimation = 1000 / 500
Raquette_D_Y_Estimation = 2 * Raquette_D_Y_Estimation - Raquette_D_Y_Estimation * Raquette_D_Y_Estimation - 1
Raquette_D_Y_Estimation = Ecran_Y + Ecran_H / 2 + Ecran_H * Raquette_D_Y_Estimation / 2
EndIf
EndIf
; If Raquette_D_Y_Estimation + 4 < Raquette_D_Y + 48
; Raquette_D_Y - #VitesseR
; ElseIf Raquette_D_Y_Estimation + 4 > Raquette_D_Y + 128 - 48
; Raquette_D_Y + #VitesseR
; EndIf
; Rebond en haut et en bas
If (Balle_Y <= Ecran_Y And Balle_Dy < 0) Or (Balle_Y >= Ecran_Y + Ecran_H - 8 And Balle_Dy > 0)
Balle_Dy * -1
EndIf
; Rebond sur les raquettes
If (Balle_X <= Ecran_X + 24 And Balle_Y - 8 > Raquette_G_Y And Balle_Y + 8 < Raquette_G_Y + 128 - 1) Or (Balle_X >= Ecran_X + Ecran_L - 24 - 8 And Balle_Y - 8 > Raquette_D_Y And Balle_Y + 8 < Raquette_D_Y + 128 - 1)
; La balle tape sur une raquette
Balle_Vitesse = Random(1000) / 1000
Balle_Vitesse * Balle_Vitesse * #VitesseB + #VitesseB
Balle_Dx = 0.25 + Random(1000) / 1000 * 0.75
If Balle_X > Ecran_X + 24
Balle_Dx * -1
EndIf
Balle_Dy = Sqr(1 - Balle_Dx * Balle_Dx)
If Random(1) = 1
Balle_Dy * -1
EndIf
EndIf
; Raté la balle coté gauche
If Balle_X < Ecran_X
Score_Affiche + 1
Score_D + 1
Raquette_G_Y = Ecran_Y + (Ecran_H - 128) / 2
Raquette_D_Y = Ecran_Y + (Ecran_H - 128) / 2
Balle_X = Ecran_X + Ecran_L - 24 - 8 - 1
Balle_Y = Ecran_Y + (Ecran_H - 8) / 2
Balle_Dx = 1
Balle_Dy = 0
Balle_Vitesse = 1
Depart = 3 * 1000 / 20
EndIf
; Raté la balle coté droit
If Balle_X >= Ecran_X + Ecran_L
Score_Affiche + 1
Score_G + 1
Raquette_G_Y = Ecran_Y + (Ecran_H - 128) / 2
Raquette_D_Y = Ecran_Y + (Ecran_H - 128) / 2
Balle_X = Ecran_X + 24 + 1
Balle_Y = Ecran_Y + (Ecran_H - 8) / 2
Balle_Dx = -1
Balle_Dy = 0
Balle_Vitesse = 1
Depart = 3 * 1000 / 20
EndIf
Else
Depart - 1
;Debug Depart
EndIf
; Affichage du score
If Score_Affiche
StartDrawing(ImageOutput(Image_Score))
Box(0, 0, 128, 24, $FFFFFF)
DrawingMode(#PB_2DDrawing_Outlined | #PB_2DDrawing_Transparent)
Box(0, 0, 128, 24, 0)
DrawingFont(FontID(Police))
Texte = Str(Score_G) + " / " + Str(Score_D)
DrawText((128 - TextWidth(Texte)) / 2,(24 - TextHeight(Texte)) / 2, Texte, $D33DDA)
StopDrawing()
SetGadgetState(Score_Gadget, ImageID(Image_Score))
Score_Affiche = 0
EndIf
; Affichage
ResizeWindow(Fenetre_Raquette_G, Ecran_X, Raquette_G_Y, #PB_Ignore, #PB_Ignore)
ResizeWindow(Fenetre_Raquette_D, Ecran_X + Ecran_L - 24, Raquette_D_Y, #PB_Ignore, #PB_Ignore)
ResizeWindow(Fenetre_Balle, Balle_X, Balle_Y, #PB_Ignore, #PB_Ignore)
EndSelect
Case #PB_Event_CloseWindow
Quitter = 1
EndSelect
EndIf
Until Quitter
CloseWindow(Fenetre_Raquette_G)
CloseWindow(Fenetre_Raquette_D)
CloseWindow(Fenetre_Balle)
Else
End
EndIf
CloseWindow(Fenetre_Score)
Else
End
EndIf
Else
End
EndIf
FreeImage(Image)
FreeImage(Image_Raquette_D)
FreeImage(Image_Raquette_G)
FreeImage(Image_Balle)
Else
End
EndIf
FreeFont(Police)
Else
End
EndIf
EndIf
EndProcedure
Jouer_CasseBriques()