Page 3 sur 5

Re: Crazy Snake

Publié : lun. 06/juil./2015 14:46
par falsam
Code à tester sous OSx.

Code : Tout sélectionner

;Initialisation
If InitSprite()
  InitKeyboard()
EndIf

;Creation du screen
OpenWindow(0, 0, 0, 600, 600, "Test Sprite", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 800, 600)

Game = CreateSprite(#PB_Any, 400, 400)

;Boucle evenementielle
Repeat  ;Evenement window
  
  Repeat ;Evenement screen
    Event = WindowEvent()
     
    Select Event    
      Case #PB_Event_CloseWindow
        End
    EndSelect  
  Until Event=0
    
  FlipBuffers()
  ClearScreen(RGB(135, 206, 235))
  ExamineKeyboard()
  
  ;Display game
  DisplaySprite(Game, (600 - SpriteWidth(Game))/2, ((600 - SpriteHeight(Game))/2) + PosY)
  
  RotateSprite(Game, 0.05, #PB_Relative)

  
Until KeyboardPushed(#PB_Key_Escape)

Re: Crazy Snake

Publié : lun. 06/juil./2015 14:49
par Ollivier
ZoomSprite() et RotateSprite() se mettent en conflit anarchiquement.
Ça m'avait tellement gonflé que j'ai préféré user de TransformSprite() (avec 9 paramètres, pas 13) et calculer moi-même les 4 coins.

Normalement, cette 2ème soluce est multi-plateforme. Et si elle ne l'était pas, ça ne resterait qu'une interversion de 2 coordonnées de coin.

Re: Crazy Snake

Publié : lun. 06/juil./2015 14:51
par Mindphazer
Là ça marche nickel

Ceci dit, le bug d'affichage se produisait après la rotation ; ici, la rotation est... perpétuelle :mrgreen:

Re: Crazy Snake

Publié : lun. 06/juil./2015 16:21
par SPH
10 :mrgreen:

29 :P

Re: Crazy Snake

Publié : lun. 06/juil./2015 16:52
par falsam
@SPH: /lol + /evil + /punch 3 fois

Re: Crazy Snake

Publié : lun. 06/juil./2015 21:19
par crisot
Sympa et original :)

Juste un détail niveau clavier, un appuis trop bref sur une touche "entre 2 avancées" n'est pas pris en compte.

Re: Crazy Snake

Publié : mar. 07/juil./2015 9:31
par falsam
Le code est mise à jour. Un peu de couleur et un effet bounce au début du jeu. Je ne peux pas corrigé les soucis que vous rencontrez sous OSx et aussi sous Linux.

Re: Crazy Snake

Publié : mar. 07/juil./2015 9:55
par TazNormand
92 avec ta nouvelle version, pas mal le petit effet bounce

Re: Crazy Snake

Publié : mar. 07/juil./2015 10:09
par falsam
Merci pour l'idée Taz :)

Re: Crazy Snake

Publié : mar. 07/juil./2015 10:11
par Kwai chang caine
Rien qu'en lisant le titre vous savez comment jouer
:oops:


Un collegue m'a montré ..re :oops:

Une idée, mais bon...venant d'un ignare en jeu comme moi... :roll:

Un ecran noir pendant une période de plus en plus longue
Ou alors un gros truc, bien ennervant, qui vient se foutre devant pendant aussi une periode de plus en plus longue :mrgreen:

En tout cas, comme dab, encore merci du partage FALSAM 8)

Re: Crazy Snake

Publié : mar. 07/juil./2015 10:20
par falsam
Un écran noir pour jouer ça ne va pas être simple. Le bidule qui gêne pourquoi pas.

Re: Crazy Snake

Publié : mar. 07/juil./2015 12:46
par Kwai chang caine
Non je voulais dire une seconde, voir moins...juste histoire de paumer le gars :D
Evidemment avec une securité pour que ce ne soit pas à un moment ou ça le fasse perdre, sinon c'est "dégueu" :?
Mais juste perdre pied :idea:
Un peu comme quand cette put...de lumiere de cage d'escalier s'eteint, et que t'as bien pris ton élan pour descendre les marches 4 à 4 ... 8)
Un tres petit dans le temps..mais grand moment de solitude :mrgreen:

Et un roulis comme dans les bateaux ??? droooooite gaaaaauuuche...droooite gaaaauuuche...droooooite gaaaaauuuche
Image

Effet garanti :mrgreen:

Re: Crazy Snake

Publié : mar. 07/juil./2015 13:29
par Ar-S
Image

Testé sous linux du coup. Fonctionne nikel

Même bug de décalage vers le haut au stretch de l'ecran.
Petit bug au début à la fin du gigotage ou le carré se déplace tout en bas et se repositionne l'espace d'un instant.
Le reste marche.

P.S : Press up arrow etc... sort toujours à droite et à gauche

Re: Crazy Snake

Publié : mar. 07/juil./2015 13:34
par falsam
Mais heu incroyable !! J'utilise la police System qui se trouve sur tous les pc et elle n’apparaît même pas sur ton screen.

Re: Crazy Snake

Publié : mer. 08/juil./2015 7:30
par Ar-S
Pour un rendu OK sous linux

Code : Tout sélectionner

Font15 = LoadFont(#PB_Any, "System", 15)
Font20 = LoadFont(#PB_Any, "System", 20)
Font25 = LoadFont(#PB_Any, "System", 23)
Font40 = LoadFont(#PB_Any, "System", 40,#PB_Font_Bold)
edit-

Petit ajout d'un effet
- mouvement de fenetre

Code : Tout sélectionner

;Crazy Snake by falsam

;PB 5.31

EnableExplicit

Enumeration
  #MainForm
EndEnumeration

Global Event
Global UpdateSquares.b
Global Vx=1, Vy=0         ;Velocity x & y
Global x1, y1, x2, y2
Global KLR.b = #True      ;Left Key & Right Key enable
Global KUD.b = #True      ;Up key & down key enable
Global n
Global FirstStart = #True, GameOver = 0
Global TargetCreate, tx, ty
Global SquareColor
Global Score, BestScore
Global Boom.b
Global Font15, Font20, font25, font40
Global Angle.f
Global ZoomX, ZoomY, BounceX.f, BounceY.f, Sens
Global StartTime.f, TimeOut.f
Global ScreenDefaultColor, GameDefaultColor, GameColor, TextColor, LineColor
Global SnakeHeadColor, SnakeBodyColor, SnakeOutlineColor
Global Text.s, PosX, PosY.f
Global Dir.s
Global wx,wy

;Game Sprite
Global Game

;Snake
Structure Snake
  x.i
  y.i
EndStructure
Global NewList Snakes.Snake()

;Engine Init
InitSprite()
InitKeyboard()

;Load Font
Font15 = LoadFont(#PB_Any, "System", 15)
Font20 = LoadFont(#PB_Any, "System", 20)
Font25 = LoadFont(#PB_Any, "System", 23)
Font40 = LoadFont(#PB_Any, "System", 40,#PB_Font_Bold)

;Color
ScreenDefaultColor = RGB(127, 182, 127)
GameDefaultColor   = RGB(143, 188, 143)
LineColor          = RGB(210, 180, 140)
TextColor          = RGB(255, 255, 255)

SnakeHeadColor     = RGB(210, 180, 140)
SnakeBodyColor     = RGB(255, 248, 220)
SnakeOutlineColor  = RGB(184, 134, 11)

;Screen
OpenWindow(#MainForm, 0, 0, 600, 600, "Crazy Snake", #PB_Window_SystemMenu|#PB_Window_ScreenCentered)
OpenWindowedScreen(WindowID(0), 0, 0, 600, 600)

;Create Game
Game = CreateSprite(#PB_Any, 400, 400)
TimeOut = 200

Repeat
  WX = WindowX(#MainForm, #PB_Window_InnerCoordinate)
  WY= WindowY(#MainForm, #PB_Window_InnerCoordinate)
  Repeat
    Event = WindowEvent()
    
    Select Event   
      Case #PB_Event_CloseWindow
        End
    EndSelect 
  Until Event=0
  
  FlipBuffers()
  ClearScreen(RGB(220, 220, 220))
  
  ExamineKeyboard()
  
  If FirstStart = #True
    ClearList(Snakes())
    
    For n = 0 To 3
      AddElement(Snakes())
      With Snakes()
        \x = 192 - 16 * n
        \y = 192
        x1 = \x
        y1 = \y
      EndWith
    Next
  EndIf
  
  If GameOver = 2 Or FirstStart = #True
    If KeyboardReleased(#PB_Key_Up)
      
      FirstStart = 0
      
      ;Add 4 Squares to snake
      ClearList(Snakes())
      For n = 0 To 3
        AddElement(Snakes())
        With Snakes()
          \x = 192 - 16 * n
          \y = 192
          x1 = \x
          y1 = \y
        EndWith
      Next     
      
      ;Reset Game Setup
      StartTime.f = 0
      RotateSprite(Game, 0, #PB_Absolute)
      ZoomSprite(Game, 400, 400)
      
      Boom = 0
      Score = 0     
      KLR = #True ;Left Key & Right Key enable
      KUD = #True ;Up key & down key enable
      vx = 1 : vy = 0 ;Snake starts right
      
      TargetCreate = #True
      
      Angle = 0 :
      ZoomX = 0 : ZoomY = 0
      BounceX = 0 : BounceY = 0 : Sens = 1     
      GameColor = GameDefaultColor
      GameOver = #False
    EndIf
  EndIf
  
  If GameOver = #False
    ;-Keybord events
    If KeyboardPushed(#PB_Key_Left) And KLR = #True
      vx = - 1 : vy = 0 : KLR = #False : KUD = #True
      dir.s = "G"
      
    ElseIf KeyboardPushed(#PB_Key_Right) And KLR = #True
      vx = 1 : vy = 0 : KLR = #False : KUD = #True
      dir.s = "D"
      
    ElseIf KeyboardPushed(#PB_Key_Up) And KUD = #True
      vy = -1 : vx = 0 : KUD = #False : KLR = #True
      dir.s = "H"
      
    ElseIf KeyboardPushed(#PB_Key_Down) And KUD = #True
      vy = 1 :  vx = 0 : KUD = #False : KLR = #True
      dir.s = "B"
    EndIf
    
    ;- Updates the position of the snake
    If ElapsedMilliseconds() - StartTime  > TimeOut
      StartTime = ElapsedMilliseconds()
      ForEach Snakes()
        If ListIndex(Snakes()) = 0
          ;Updates the head
          x1 = Snakes()\x ;Memorise old X position
          y1 = Snakes()\y ;Memorise old Y position
          
          PushListPosition(Snakes())
          NextElement(Snakes())
          If x1 + 16 * vx = Snakes()\x And y1 + 16 * vy = Snakes()\y 
            vx * -1
            vy * -1
          EndIf
          PopListPosition(Snakes())         
          
          Snakes()\x + 16 * vx
          Snakes()\y + 16 * vy
          
          ;-Collide(head, target)
          If Snakes()\x = tx And Snakes()\y = ty
            TargetCreate = #True
            UpdateSquares = #True
            Score + 1
          EndIf
          
          ;-Collide(head, Body)
          PushListPosition(Snakes())
          While NextElement(Snakes())
            If Snakes()\x = x1 And  Snakes()\y = y1
              Boom = #True
            EndIf
          Wend
          PopListPosition(Snakes())
          
        Else
          ;-Updates the body
          x2 = Snakes()\x
          y2 = Snakes()\y
          Snakes()\x = x1
          Snakes()\y = y1
          x1 = x2
          y1 = y2
        EndIf
      Next 
      
      If UpdateSquares = #True
        ;Adds an element to the body of the snake
        AddElement(Snakes())
        Snakes()\x = x1
        Snakes()\y = y1
        UpdateSquares = #False
      EndIf
      
      n = 0
      ;EndIf
      
      ;-Create target
      While TargetCreate = #True
        ;New X
        While TargetCreate = #True
          tx = Random(384, 0)
          If Mod(tx, 16) = 0
            TargetCreate = #False
          EndIf
        Wend
        TargetCreate = #True
        
        ;New y
        While TargetCreate = #True
          ty = Random(384, 0)
          If Mod(ty, 16) = 0
            TargetCreate = #False
          EndIf
        Wend
        TargetCreate = #True
        
        ;Intersection with the snake ?
        ForEach Snakes()
          With Snakes()
            If \x <> Tx And \y <> Ty
              ;UpdateSquares = #True
              TargetCreate = #False
            EndIf
          EndWith
        Next   
      Wend
    EndIf
  EndIf
  
  ;- Drawing Game
  
  ;Draw Score 
  StartDrawing(ScreenOutput())
  Box(0, 0, 600, 600, ScreenDefaultColor)
  
  DrawingMode(#PB_2DDrawing_Transparent)
  DrawingFont(FontID(Font20))
  DrawText(20, 15, "Crazy Snake", TextColor)
  DrawText(450, 15, "Score " + Str(Score), TextColor)
  
  DrawingFont(FontID(Font15))
  DrawText(400, 560, "falsam (2015-2015)", TextColor)
  
  StopDrawing()
  
  ;Draw GameSprite
  StartDrawing(SpriteOutput(Game))
  Box(0, 0, 400, 400, GameColor)
  
  ;Draw Grid
  Global gx
  For gx = 0 To 399 Step 16
    LineXY(gx, 0, gx, 399, LineColor)
    LineXY(0, gx, 399, gx, LineColor)
  Next 
  ;Draw Grid outline
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(0, 0, 400, 400, LineColor)
  DrawingMode(#PB_2DDrawing_Default)     
  
  ;Draw Snake
  ForEach Snakes()
    With Snakes()
      Select ListIndex(Snakes())
        Case 0 ;First square
          SquareColor = SnakeHeadColor
          
        Case ListSize(Snakes()) - 1 ;Last square
          SquareColor = SnakeHeadColor
          
        Default
          SquareColor = SnakeBodyColor
      EndSelect   
      
      DrawingMode(#PB_2DDrawing_Default)     
      Box(\x, \y, 16, 16, SquareColor)
      
      DrawingMode(#PB_2DDrawing_Outlined)
      Box(\x, \y, 16, 16, SnakeOutlineColor)     
    EndWith
  Next
  
  ;Draw target
  DrawingMode(#PB_2DDrawing_Default)
  Box(tx, ty, 16, 16, RGB(127, 255, 0))
  DrawingMode(#PB_2DDrawing_Outlined)
  Box(tx, ty, 16, 16, RGB(0, 0, 0))
  
  
  StopDrawing()
  
  ;Display game
  DisplaySprite(Game, (600 - SpriteWidth(Game))/2 + PosX, ((600 - SpriteHeight(Game))/2) + PosY)
  
  ;- Game effect
  If GameOver = #False
    
    Select Score         
      Case 0 To 2 ;Bounce game ++
        TimeOut = 200
        BounceX + 0.03 : BounceY + 0.03
        Angle + 0.1
        PosX = BounceX * Cos(Angle)
        PosY = BounceY * Cos(Angle)
        If Angle = 1
          Angle = 0
        EndIf
        
      Case 3 To 4 ;Bounce Game --
        If BounceX > 0
          BounceX - 0.04 : BounceY - 0.04
        EndIf
        
        Angle + 0.1
        
        PosX = BounceX * Cos(Angle)
        PosY = BounceY * Cos(Angle)
        
        If Angle = 1
          Angle = 0
        EndIf
        
      Case 5
        Angle = 0 : PosX = 0 : PosY = 0
        
      Case 6 To 9 ;Right Rotate
        TimeOut = 200
        If Angle < 45
          Angle + 0.05
          RotateSprite(Game, 0.05, #PB_Relative) 
        EndIf
        
      Case 10 To 14 ;Left Rotate
        TimeOut = 200
        If Angle > 0
          Angle - 0.05
          RotateSprite(Game, -0.05, #PB_Relative)
        EndIf
        
      Case 15 To 19 ;Reduce the size of the game.
        TimeOut = 200
        If SpriteWidth(Game) <> 250
          ZoomSprite(Game, SpriteWidth(Game) - 1, SpriteWidth(Game) - 1)
        EndIf
        
      Case 20 To 22 ;Reduce the size of the game.
        TimeOut = 200
        If SpriteWidth(Game) <> 400
          ZoomSprite(Game, SpriteWidth(Game) + 1, SpriteWidth(Game) + 1)
        EndIf
        
      Case 23 ;Change the speed
        TimeOut = 150
        
        
        Select Dir
          Case "G"
            ResizeWindow(#MainForm, WX-10, #PB_Ignore, #PB_Ignore, #PB_Ignore)
            Dir.s="none"
          Case "D"
            ResizeWindow(#MainForm, WX+10, #PB_Ignore, #PB_Ignore, #PB_Ignore)
            Dir.s="none"
          Case "H"
            ResizeWindow(#MainForm, #PB_Ignore, WY+10, #PB_Ignore, #PB_Ignore)
            Dir.s="none"
          Case "B"
            ResizeWindow(#MainForm, #PB_Ignore, WY-10, #PB_Ignore, #PB_Ignore)
            Dir.s="none"
        EndSelect
        
        
      Case 30 ;Change the speed
        TimeOut = 100 
        
      Case 31 ;Change the speed
        TimeOut = 50
        
      Case 32;Change the speed
        TimeOut = 150
        
        
      Case 33 To 37 ;Reduce the size of the game. (Strange: the sprite moves upward)
        TimeOut = 200
        If SpriteWidth(Game) <> 250
          ZoomX=-1
          ZoomSprite(Game, SpriteWidth(Game) + ZoomX, 400)
        EndIf
        
      Case 38 To 41 ;Original Size
        TimeOut = 200
        If SpriteWidth(Game) <> 400
          ZoomX = 1
          If SpriteHeight(Game) <> 400
            ZoomY = 1
          Else
            ZoomY = 0
          EndIf
          
          ZoomSprite(Game, SpriteWidth(Game) + ZoomX, SpriteWidth(Game) + ZoomY)
        EndIf
        
      Case 41 To 43 ;Random color
        TimeOut = 150
        GameColor = RGB(Random(255, 0),Random(255, 0),Random(255, 0))
        
      Default ; Fastest speed
        TimeOut - 0.001
        GameColor = GameDefaultColor
        
    EndSelect
  EndIf
  
  ;Out of bound or Game over
  FirstElement(Snakes())
  With Snakes()
    If (\x > 384 Or \x < 0 Or \y > 384 Or \y < 0 Or Boom = #True) And GameOver <> 2
      GameOver = 1
      
      If SpriteWidth(Game) <> 10
        ZoomSprite(Game, SpriteWidth(Game)-10, SpriteWidth(Game) - 10)
      Else
        GameOver = 2
      EndIf
    EndIf
  EndWith 
  
  ;Game Over
  If GameOver = 2 Or FirstStart = #True
    
    If Score > BestScore
      BestScore = Score
    EndIf
    
    StartDrawing(ScreenOutput())
    Box(0, 0, 600, 600, ScreenDefaultColor)
    
    DrawingMode(#PB_2DDrawing_Transparent)
    
    If FirstStart = #True
      DrawingFont(FontID(Font40))
      Text = "Crazy Snake"
      DrawText((600 - TextWidth(Text))/2, 100, Text, TextColor)   
    Else
      DrawingFont(FontID(Font40))
      Text = "You Died"
      DrawText((600 - TextWidth(Text))/2, 100, Text, TextColor)   
      
      DrawingFont(FontID(Font25))
      Text = "Score: " + Str(Score)
      DrawText((600 - TextWidth(Text))/2, 200, Text, TextColor)
      
      DrawingFont(FontID(Font25))
      Text = "Best Score: " + Str(BestScore)
      DrawText((600 - TextWidth(Text))/2, 300, Text, TextColor)
    EndIf
    
    Text = "Press up arrow key to start new game"
    Angle + 0.1
    PosY = 20 * Cos(Angle)
    If angle = 1
      angle = 0
    EndIf
    
    DrawingFont(FontID(Font25))
    DrawText((600 - TextWidth(Text))/2, 500 + PosY, Text, TextColor )
    
    StopDrawing()
  EndIf
  
Until KeyboardPushed(#PB_Key_Escape)