dcsimg
Results 1 to 3 of 3

Thread: Excel VBA game

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2018
    Posts
    1

    Excel VBA game

    Hi there

    For a univeristy assignment, we need to create a game in excel. I was thinking about the following game, but in 2D: https://www.youtube.com/watch?v=kJCXPzRWH0k&t=76s
    Since i'm studying civil engineer and my knowledge of programming isn't that great (we saw java befeore this but the code is totally different), I bumped into some problems.

    I achieved the shape to move from side to side, but the steps are too big for it to be smooth. Also i don't know how to make it so once you press the space button, the 'bewegen' command stops. After that there needs to be a new rectangle on the second row that moves from side to side. The last thing i need is that it registers how much space the rectangles differ, so the program knows how big he needs to make the second rectangle and so on.
    I know this is a lot of work,, but I appreciate all the input, even if it's just some small tips!

    Sincerely

    CODE:
    Code:
    Sub proberen()
    
    
    Set MyDocument = Worksheets(1)
    MyDocument.Shapes.AddShape msoShapeRectangle, 0, 0, 150, 15
    Call naam
    
    End Sub
    
    
    
    Sub naam()
        ActiveSheet.Shapes(1).Select
        Selection.Name = "vierkant"
    End Sub
    
    
    Sub bewegen1()
    rep_count = 0
    
    Do
    
    rep_count = rep_count + 100
    
      Blad1.Shapes("vierkant").Left = rep_count
      Application.Wait Now + TimeSerial(0, 0, 1)
      
      Loop Until rep_count = 600
    End Sub
    
    
    
    Sub bewegen2()
    rep_count = 600
    
    Do
    
    rep_count = rep_count - 100
    
      Blad1.Shapes("vierkant").Left = rep_count
      Application.Wait Now + TimeSerial(0, 0, 1)
      
      Loop Until rep_count = 0
    End Sub
    
    
    
    Sub bewegen()
    
    Do
    Call bewegen1
    Call bewegen2
    
    Loop
    
    End Sub
    Last edited by si_the_geek; Apr 30th, 2018 at 09:08 AM. Reason: added Code tags

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    40,118

    Re: Excel VBA game

    Welcome to VBForums

    Thread moved from the 'VB.Net' forum to the 'Office Development/VBA' forum.

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    23,725

    Re: Excel VBA game

    I achieved the shape to move from side to side, but the steps are too big for it to be smooth
    the application.wait is not an adequate time interval, you need to use a timer that has a smaller time interval

    Code:
    Public Declare Function SetTimer Lib "user32" ( _
    ByVal HWnd As Long, ByVal nIDEvent As Long, _
    ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
    
    Public Declare Function KillTimer Lib "user32" ( _
    ByVal HWnd As Long, ByVal nIDEvent As Long) As Long
    
    Public TimerID As Long, timerseconds As Single, tim As Boolean
    Dim Counter As Long
    Dim countdown As Double
    Dim txt As String
    Public currentcell As Range
    
    
    '~~> Start Timer
    Sub StartTimer(timerseconds As Long)
        '~~ Set the timer for 1 second
        
        countdown = TimeSerial(0, 2, 0) 'duration 2 mins
    '    TimerSeconds = 1
        TimerID = SetTimer(0&, 0&, timerseconds * 1000&, AddressOf TimerProc)
    '    txt = Sheets("sheet1").Cells(3, 3)
    End Sub
    
    '~~> End Timer
    Sub EndTimer()
        On Error Resume Next
        KillTimer 0&, TimerID
    '    Sheets("sheet1").Cells(3, 3) = txt
    End Sub
    
    Sub TimerProc(ByVal HWnd As Long, ByVal uMsg As Long, _
    ByVal nIDEvent As Long, ByVal dwTimer As Long)
    
    '  in here put code to move the shape
    
    '  also at some point you need to run killtimer to stop the shape from moving or causing an error when it can not move further
    End Sub
    to start the shape moving just call starttimer .1, you can experiment with the time interval and the amount you move the shape each interval, till it looks smooth

    copy the above code into a module, you can starttimer from anywhere in the project
    as you want to move multiple shapes in turn you need to have some variable to specify which shape to move by the timerproc

    this timer was for some other example so some of it may not be relevant to what you are doing, though i doubt it will in any way affect the movement of your shape
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width