Small Basic: Exemple - Course de Voiture Graphique (fr-FR)
L'exemple qui suit créé un jeu graphique de course de voiture:
Code
'sous-routinr de dessin de la route
'sous-routine de dessin de la voiture
'début de la boucle
'dessine la route
'dessine la voiture
'accepte les touches pour conduire la voiture: Gauche / Droite
'redessine la voiture
'pause
'boucle vers le début
crash = 0
score = 0
Roadmove = 5
RL1x = 100
RL1y = 0
RL1xd = 100
RL1yd = 100
RR1x = 300
RR1y = 0
RR1xd = 300
RR1yd = 100
RL2x = 100
RL2y = 100
RL2xd = 100
RL2yd = 200
RR2x = 300
RR2y = 100
RR2xd = 300
RR2yd = 200
RL3x = 100
RL3y = 200
RL3xd = 100
RL3yd = 300
RR3x = 300
RR3y = 200
RR3xd = 300
RR3yd = 300
RL4x = 100
RL4y = 300
RL4xd = 100
RL4yd = 400
RR4x = 300
RR4y = 300
RR4xd = 300
RR4yd = 400
Sub directionchange
'direction aléatoire
D = Math.GetRandomNumber(2)
If D = 1 Then
'déplace la route vers la gauche
RL4x = RL3xd
RL4y = RL3yd
RL4xd = RL4x - Roadmove
RL4yd = 400
RR4x = RR3xd
RR4y = RR3yd
RR4xd = RR4x - Roadmove
RR4yd = 400
If RL4xd < 0 Then
RL4xd = RL4x + Roadmove
RR4xd = RR4x + Roadmove
EndIf
EndIf
If D = 2 Then
'déplace la route vers la droite
RL4x = RL3xd
RL4y = RL3yd
RL4xd = RL4x + Roadmove
RL4yd = 400
RR4x = RR3xd
RR4y = RR3yd
RR4xd = RR4x + Roadmove
RR4yd = 400
EndIf
EndSub
counter = 0
Sub roaddraw
GraphicsWindow.PenWidth = "10"
GraphicsWindow.DrawLine(RL1x,RL1y,RL1xd,RL1yd)
GraphicsWindow.DrawLine(RR1x,RR1y,RR1xd,RR1yd)
GraphicsWindow.DrawLine(RL2x,RL2y,RL2xd,RL2yd)
GraphicsWindow.DrawLine(RR2x,RR2y,RR2xd,RR2yd)
GraphicsWindow.DrawLine(RL3x,RL3y,RL3xd,RL3yd)
GraphicsWindow.DrawLine(RR3x,RR3y,RR3xd,RR3yd)
GraphicsWindow.DrawLine(RL4x,RL4y,RL4xd,RL4yd)
GraphicsWindow.DrawLine(RR4x,RR4y,RR4xd,RR4yd)
counter=counter+1
GraphicsWindow.DrawEllipse(50,400-counter*20,10,10)
GraphicsWindow.DrawEllipse(500,400-counter*20,10,10)
If 400-counter*20 < 0 Then
counter = 0
EndIf
'If XC >= 120 and XC <= 80 and YC >= (320-counter*20) and YC <=(480-counter*20)then
'crash = 1
'EndIf
EndSub
Sub roadmovement
RL1x = RL2x
RL1xd = RL2xd
RR1x = RR2x
RR1xd = RR2xd
RL2x = RL3x
RL2xd = RL3xd
RR2x = RR3x
RR2xd = RR3xd
RL3x = RL4x
RL3xd = RL4xd
RR3x = RR4x
RR3xd = RR4xd
EndSub
XC=150
Sub drawcar
GraphicsWindow.PenColor = "red"
GraphicsWindow.DrawRectangle(XC,YC,10,40)
GraphicsWindow.PenColor = "black"
GraphicsWindow.DrawRectangle(XC-8,YC,2,2)
GraphicsWindow.DrawRectangle(XC-8,YC+20,2,2)
GraphicsWindow.DrawRectangle(XC+18,YC,2,2)
GraphicsWindow.DrawRectangle(XC+18,YC+20,2,2)
EndSub
Sub movecar
XC = GraphicsWindow.mousex
YC = GraphicsWindow.mousey
EndSub
Sub drawsmash
GraphicsWindow.PenColor = "red"
GraphicsWindow.DrawBoundText(RL4x+20,200,60,"CRASH")
GraphicsWindow.PenColor = "black"
Sound.Play("C:\Users\Rob\Music\crash.mp3")
EndSub
Sub nextlevel
RR4x = RR4x-30
RR4xd = RR4xd - 30
RR3x = RR4x
RR3xd = RR4x
EndSub
Sub level
For i = 1 To 100
score=score + i
GraphicsWindow.DrawBoundText(400,50,100,"SCORE = " + score)
roaddraw()
directionchange()
graphicsWindow.Clear()
GraphicsWindow.DrawBoundText(400,50,100,"SCORE = " + score)
roaddraw()
roadmovement()
directionchange()
Sound.PlayClick()
graphicsWindow.Clear()
GraphicsWindow.DrawBoundText(400,50,100,"SCORE = " + score)
roaddraw()
drawcar()
movecar()
Program.Delay(100)
If XC = RR1x Then ' tentatively =
crash = 1
EndIf
If crash = 1 Then
drawsmash()
Goto finish
EndIf
EndFor
EndSub
For game = 1 To 5
GraphicsWindow.DrawBoundText(100,30,150,"SOYEZ PRÊT....NIVEAU "+game)
If game = 1 Then
Sound.Play("C:\Users\Rob\Music\level1.wma")
Sound.play("C:\Users\Rob\Music\123.wma")
EndIf
If game = 2 Then
Sound.Play("C:\Users\Rob\Music\level2.wma")
EndIf
If game = 3 Then
Sound.Play("C:\Users\Rob\Music\level3.wma")
EndIf
If game = 4 Then
Sound.Play("C:\Users\Rob\Music\level4.wma")
EndIf
If game = 5 Then
Sound.Play("C:\Users\Rob\Music\level5.wma")
EndIf
Program.Delay(1000)
level()
nextlevel()
Sound.PlayBellRing()
finish:
If crash = 1 Then
GraphicsWindow.DrawBoundText(100,300,300,"J'espère que vous ferez mieux la prochaine fois....entraînez vous.")
Goto exit
EndIf
EndFor
GraphicsWindow.DrawBoundText(100,300,150,"Bien joué ! Vous l'avez fait !")
exit:
Voir Aussi
Autres Langues
- Article original : Small Basic Sample: Graphic Car Race