none
Help for game in small basic RRS feed

  • Question

  • Hello,

    I'm having problems with my score system as shown my code to me makes sense and you should get a point for collecting the smiley face and another one should appear, but that doesn't seem to happen can someone fix my code or give advice

    GraphicsWindow.Hide()
    w = 800
    h = 600
    GraphicsWindow.CanResize = "Flase"
    GraphicsWindow.Width = w
    GraphicsWindow.Height = h
    GraphicsWindow.Top = (Desktop.Height - h) / 2
    GraphicsWindow.Left = (Desktop.Width - w) / 2
    GraphicsWindow.Show()
    radius = 25
    score = 0
    GUI()
    pointball = "H:\Programming\smiley face.jpg"
    pointballx = Math.GetRandomNumber(750)
    pointbally = Math.GetRandomNumber(550)

    Sub GUI 
    Controls.ButtonClicked = StartBut
    button1 = Controls.AddButton("Start Game", 2,2)
    Controls.SetSize(button1, 100, 100)
    scoretext = Shapes.AddText("Score: " + score)
    Shapes.Move(scoretext, 600, 10)
    EndSub  

    Shapes.SetText(scoretext,"Score: " + score)
    Shapes.Move(scoretext, 600, 10)



    Sub Startbut
      IStartBut = Controls.LastClickedButton
      If IStartBut = button1 Then
        Mouse.HideCursor()
        GraphicsWindow.BrushColor = "Violet"
        GraphicsWindow.PenColor = "Blue"
        MainPlayer = Shapes.AddEllipse(50,50)
        Shapes.Move(MainPlayer,15,15)
        GraphicsWindow.MouseMove = MovePlayer
        active = "true"
        Controls.ButtonClicked = Contact
        Shapes.HideShape(pointballs)
        pointballs = Shapes.AddImage(pointball)
        Shapes.Move(pointballs, pointballx, pointbally)
        Shapes.ShowShape(pointballs)   
     endif  
    EndSub

    Sub MovePlayer
      If active = "true" Then
        Shapes.Move(MainPlayer, GraphicsWindow.MouseX-25,GraphicsWindow.MouseY-25)
      EndIf
    EndSub

    Sub Contact
      If GraphicsWindow.MouseX = pointballx  And GraphicsWindow.MouseY = pointbally  Then
        score = score + 1
        Shapes.Remove(pointballs)
        pointballs = Shapes.AddImage(pointball)
        Shapes.Move(pointballs, pointballx, pointbally)
        Shapes.ShowShape(pointballs)   
      EndIf
      EndSub

    Tuesday, May 27, 2014 1:23 PM

Answers

  • Welcome UnDefined,

    There are a few issues.

    1] It is best to keep all the Subroutines after the main code - not critical but will make life easier in the future. (lines 18,19 below).

    2] When the ball is moved with the mouse, it prevents us ever pressing the button a second time, therefore Contact can never be called.  So do you want the test for overlap ONLY when the button is pressed?

    3] To check for overlap it may be better to check for an overlap rather than an exact match of coordinates (line 58 below)

    I recommend putting TextWindow.WriteLine debugging in to see what is called when you move etc.

    Below is how I did it - I also comment out line 52 to prevent moving the shape with the mouse (so that the button could be clicked - we could put the button below the circle shape) - I also use a circle in place of your smiley face image which I don't have.

    This is not a solution, but a starting point to see where to go next.

    GraphicsWindow.Hide()
    w = 800
    h = 600
    GraphicsWindow.CanResize = "Flase"
    GraphicsWindow.Width = w
    GraphicsWindow.Height = h
    GraphicsWindow.Top = (Desktop.Height - h) / 2
    GraphicsWindow.Left = (Desktop.Width - w) / 2
    GraphicsWindow.Show()
    radius = 25
    score = 0
    GUI()
    'pointball = "H:\Programming\smiley face.jpg"
    pointballs = Shapes.AddEllipse(50,50)
    pointballx = Math.GetRandomNumber(750)
    pointbally = Math.GetRandomNumber(550)

    Shapes.SetText(scoretext,"Score: " + score)
    Shapes.Move(scoretext, 600, 10)

    Sub GUI
      TextWindow.WriteLine("GUI")
      Controls.ButtonClicked = StartBut
      button1 = Controls.AddButton("Start Game", 2,2)
      Controls.SetSize(button1, 100, 100)
      scoretext = Shapes.AddText("Score: " + score)
      Shapes.Move(scoretext, 600, 10)
    EndSub  

    Sub Startbut
      TextWindow.WriteLine("Startbut")
      IStartBut = Controls.LastClickedButton
      If IStartBut = button1 Then
        Mouse.HideCursor()
        GraphicsWindow.BrushColor = "Violet"
        GraphicsWindow.PenColor = "Blue"
        MainPlayer = Shapes.AddEllipse(50,50)
        Shapes.Move(MainPlayer,15,15)
        GraphicsWindow.MouseMove = MovePlayer
        active = "true"
        Controls.ButtonClicked = Contact
        'Shapes.HideShape(pointballs)
        'pointballs = Shapes.AddImage(pointball)
        Shapes.Move(pointballs, pointballx, pointbally)
        'Shapes.ShowShape(pointballs)   
      endif  
    EndSub

    Sub MovePlayer
      TextWindow.WriteLine("MovePlayer")
      If active = "true" Then
        'Shapes.Move(MainPlayer, GraphicsWindow.MouseX-25,GraphicsWindow.MouseY-25)
      EndIf
    EndSub

    Sub Contact
      TextWindow.WriteLine("Contact")
      If GraphicsWindow.MouseX = pointballx  And GraphicsWindow.MouseY = pointbally  Then
        score = score + 1
        'Shapes.Remove(pointballs)
        'pointballs = Shapes.AddImage(pointball)
        Shapes.Move(pointballs, pointballx, pointbally)
        'Shapes.ShowShape(pointballs)   
      EndIf
    EndSub

    Tuesday, May 27, 2014 6:16 PM
    Moderator
  • How is going Un DeFined?

    Here is a simple example of how what you want may be approached.

    Initialise()

    'Define Events
    GraphicsWindow.MouseDown = OnMouseDown 'For testing a hit
    GraphicsWindow.MouseMove = OnMouseMove 'For moving player
    Controls.ButtonClicked = OnButtonClicked 'For start and exit buttons

    'Infinite Game loop
    'Unused here, but for more complex game we may use it
    While ("True")
      Program.Delay(20)
    EndWhile

    '==========================================
    'SUBROUTINES
    '==========================================

    Sub Initialise 'Create all sprites (initially hidden) and main window
      gw = 600
      gh = 600
      GraphicsWindow.Width = gw
      GraphicsWindow.Height = gh
      score = 0
      updateTitle()
     
      radius = 20
      GraphicsWindow.BrushColor = "Red"
      target = Shapes.AddEllipse(2*radius,2*radius)
      Shapes.HideShape(target)
      GraphicsWindow.BrushColor = "Yellow"
      player = Shapes.AddEllipse(2*radius,2*radius)
      Shapes.HideShape(player)
      Shapes.SetOpacity(player,50)
     
      GraphicsWindow.BrushColor = "Black"
      startButton = Controls.AddButton("START",10,10)
      exitButton = Controls.AddButton("EXIT",70,10)
    EndSub

    Sub newTarget ' Update the target random position
      targetX = radius+Math.GetRandomNumber(gw-2*radius)
      targetY = radius+Math.GetRandomNumber(gh-2*radius)
      Shapes.Move(target, targetX-radius, targetY-radius)
    EndSub

    Sub updatePlayer 'Move the player with the mouse
      playerX = GraphicsWindow.MouseX
      playerY = GraphicsWindow.MouseY
      Shapes.Move(player,playerX-radius,playerY-radius)
    EndSub

    Sub hitTest 'Check for partial overlap, and update score and restart if a hit
      dx = targetX - playerX
      dy = targetY - playerY
      dist = Math.SquareRoot(dx*dx + dy*dy)
      If (dist < radius) Then
        score = score+1
        newTarget()
      Else
        score = score-1
      EndIf
      updateTitle()
    EndSub

    Sub updateTitle 'Update score in title
      GraphicsWindow.Title = "Score "+score
    EndSub

    '==========================================
    'EVENT SUBROUTINES
    '==========================================

    Sub OnMouseDown 'Handle Mouse click
      hitTest()
    EndSub

    Sub OnMouseMove 'Handle Mouse move
      updatePlayer()
    EndSub

    Sub OnButtonClicked 'Handle buttons
      button = Controls.LastClickedButton
      If (button = startButton) Then
        newTarget()
        Shapes.ShowShape(target)
        Shapes.ShowShape(player)
      ElseIf (button = exitButton) Then
        Program.End()
      EndIf
    EndSub

    Wednesday, May 28, 2014 8:24 PM
    Moderator

All replies

  • Welcome UnDefined,

    There are a few issues.

    1] It is best to keep all the Subroutines after the main code - not critical but will make life easier in the future. (lines 18,19 below).

    2] When the ball is moved with the mouse, it prevents us ever pressing the button a second time, therefore Contact can never be called.  So do you want the test for overlap ONLY when the button is pressed?

    3] To check for overlap it may be better to check for an overlap rather than an exact match of coordinates (line 58 below)

    I recommend putting TextWindow.WriteLine debugging in to see what is called when you move etc.

    Below is how I did it - I also comment out line 52 to prevent moving the shape with the mouse (so that the button could be clicked - we could put the button below the circle shape) - I also use a circle in place of your smiley face image which I don't have.

    This is not a solution, but a starting point to see where to go next.

    GraphicsWindow.Hide()
    w = 800
    h = 600
    GraphicsWindow.CanResize = "Flase"
    GraphicsWindow.Width = w
    GraphicsWindow.Height = h
    GraphicsWindow.Top = (Desktop.Height - h) / 2
    GraphicsWindow.Left = (Desktop.Width - w) / 2
    GraphicsWindow.Show()
    radius = 25
    score = 0
    GUI()
    'pointball = "H:\Programming\smiley face.jpg"
    pointballs = Shapes.AddEllipse(50,50)
    pointballx = Math.GetRandomNumber(750)
    pointbally = Math.GetRandomNumber(550)

    Shapes.SetText(scoretext,"Score: " + score)
    Shapes.Move(scoretext, 600, 10)

    Sub GUI
      TextWindow.WriteLine("GUI")
      Controls.ButtonClicked = StartBut
      button1 = Controls.AddButton("Start Game", 2,2)
      Controls.SetSize(button1, 100, 100)
      scoretext = Shapes.AddText("Score: " + score)
      Shapes.Move(scoretext, 600, 10)
    EndSub  

    Sub Startbut
      TextWindow.WriteLine("Startbut")
      IStartBut = Controls.LastClickedButton
      If IStartBut = button1 Then
        Mouse.HideCursor()
        GraphicsWindow.BrushColor = "Violet"
        GraphicsWindow.PenColor = "Blue"
        MainPlayer = Shapes.AddEllipse(50,50)
        Shapes.Move(MainPlayer,15,15)
        GraphicsWindow.MouseMove = MovePlayer
        active = "true"
        Controls.ButtonClicked = Contact
        'Shapes.HideShape(pointballs)
        'pointballs = Shapes.AddImage(pointball)
        Shapes.Move(pointballs, pointballx, pointbally)
        'Shapes.ShowShape(pointballs)   
      endif  
    EndSub

    Sub MovePlayer
      TextWindow.WriteLine("MovePlayer")
      If active = "true" Then
        'Shapes.Move(MainPlayer, GraphicsWindow.MouseX-25,GraphicsWindow.MouseY-25)
      EndIf
    EndSub

    Sub Contact
      TextWindow.WriteLine("Contact")
      If GraphicsWindow.MouseX = pointballx  And GraphicsWindow.MouseY = pointbally  Then
        score = score + 1
        'Shapes.Remove(pointballs)
        'pointballs = Shapes.AddImage(pointball)
        Shapes.Move(pointballs, pointballx, pointbally)
        'Shapes.ShowShape(pointballs)   
      EndIf
    EndSub

    Tuesday, May 27, 2014 6:16 PM
    Moderator
  • How is going Un DeFined?

    Here is a simple example of how what you want may be approached.

    Initialise()

    'Define Events
    GraphicsWindow.MouseDown = OnMouseDown 'For testing a hit
    GraphicsWindow.MouseMove = OnMouseMove 'For moving player
    Controls.ButtonClicked = OnButtonClicked 'For start and exit buttons

    'Infinite Game loop
    'Unused here, but for more complex game we may use it
    While ("True")
      Program.Delay(20)
    EndWhile

    '==========================================
    'SUBROUTINES
    '==========================================

    Sub Initialise 'Create all sprites (initially hidden) and main window
      gw = 600
      gh = 600
      GraphicsWindow.Width = gw
      GraphicsWindow.Height = gh
      score = 0
      updateTitle()
     
      radius = 20
      GraphicsWindow.BrushColor = "Red"
      target = Shapes.AddEllipse(2*radius,2*radius)
      Shapes.HideShape(target)
      GraphicsWindow.BrushColor = "Yellow"
      player = Shapes.AddEllipse(2*radius,2*radius)
      Shapes.HideShape(player)
      Shapes.SetOpacity(player,50)
     
      GraphicsWindow.BrushColor = "Black"
      startButton = Controls.AddButton("START",10,10)
      exitButton = Controls.AddButton("EXIT",70,10)
    EndSub

    Sub newTarget ' Update the target random position
      targetX = radius+Math.GetRandomNumber(gw-2*radius)
      targetY = radius+Math.GetRandomNumber(gh-2*radius)
      Shapes.Move(target, targetX-radius, targetY-radius)
    EndSub

    Sub updatePlayer 'Move the player with the mouse
      playerX = GraphicsWindow.MouseX
      playerY = GraphicsWindow.MouseY
      Shapes.Move(player,playerX-radius,playerY-radius)
    EndSub

    Sub hitTest 'Check for partial overlap, and update score and restart if a hit
      dx = targetX - playerX
      dy = targetY - playerY
      dist = Math.SquareRoot(dx*dx + dy*dy)
      If (dist < radius) Then
        score = score+1
        newTarget()
      Else
        score = score-1
      EndIf
      updateTitle()
    EndSub

    Sub updateTitle 'Update score in title
      GraphicsWindow.Title = "Score "+score
    EndSub

    '==========================================
    'EVENT SUBROUTINES
    '==========================================

    Sub OnMouseDown 'Handle Mouse click
      hitTest()
    EndSub

    Sub OnMouseMove 'Handle Mouse move
      updatePlayer()
    EndSub

    Sub OnButtonClicked 'Handle buttons
      button = Controls.LastClickedButton
      If (button = startButton) Then
        newTarget()
        Shapes.ShowShape(target)
        Shapes.ShowShape(player)
      ElseIf (button = exitButton) Then
        Program.End()
      EndIf
    EndSub

    Wednesday, May 28, 2014 8:24 PM
    Moderator