473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Creating a pong type game to played inside documents

I need help with some VB code. Ultimately I would like it in VBA because that's all I have. I am making a fun game to hide in all of my docs. It is a pong like game but you need to have your pad deflect a ball that a alien drops. I also need help making the aliens move by themselves and drop the balls at a specific spot. Any help or ideas would be nice.This is my first post so go easy on me.

Thanks
Mar 29 '07 #1
26 3650
vijaydiwakar
579 512MB
I need help with some VB code. Ultimately I would like it in VBA because that's all I have. I am making a fun game to hide in all of my docs. It is a pong like game but you need to have your pad deflect a ball that a alien drops. I also need help making the aliens move by themselves and drop the balls at a specific spot. Any help or ideas would be nice.This is my first post so go easy on me.

Thanks
see we r not getting u
so if possible explain it in detail
Mar 29 '07 #2
Killer42
8,435 Expert 8TB
I need help with some VB code. Ultimately I would like it in VBA because that's all I have. I am making a fun game to hide in all of my docs. It is a pong like game but you need to have your pad deflect a ball that a alien drops. I also need help making the aliens move by themselves and drop the balls at a specific spot. Any help or ideas would be nice.This is my first post so go easy on me.
Sounds an interesting idea. I hope it works out.

However, we generally expect people to work out what they want to do, and make some attempt at it. Then, when you have specific questions about points where you get stuck we can help with the how.

What have you achieved so far on this project?
Mar 29 '07 #3
vijaydiwakar
579 512MB
Don't hate ur Computer
:)
Mar 29 '07 #4
see we r not getting u
so if possible explain it in detail
I am using VBA to make a game. The first thing that I want is to make a picture of a spaceship that I have move around the window randomly. I know that I need a randomize script of some kind but I tried and it didn't work. Help is always great.

Thanks
Mar 29 '07 #5
vijaydiwakar
579 512MB
I am using VBA to make a game. The first thing that I want is to make a picture of a spaceship that I have move around the window randomly. I know that I need a randomize script of some kind but I tried and it didn't work. Help is always great.

Thanks
for that u may use timmer ctrl with less interval or u may use system second part or u may use rnd function to do so
try it
Good Luck
Mar 29 '07 #6
Sounds an interesting idea. I hope it works out.

However, we generally expect people to work out what they want to do, and make some attempt at it. Then, when you have specific questions about points where you get stuck we can help with the how.

What have you achieved so far on this project?
I have tried to make a randomize script but I need to make that do something to the ship. I need it random so people can't find all to possible combinations and beat it. I am trying to do this with at least 4 ships.

Thanks
Mar 29 '07 #7
for that u may use timmer ctrl with less interval or u may use system second part or u may use rnd function to do so
try it
Good Luck
My random number code looks like this:

Private Function RandomNumber() As Integer
Randomize Timer
RandomNumber = Int(Rnd * 100)
Label1 = RandomNumber
End Function

Private Sub CommandButton1_Click()
Call RandomNumber
End Sub

I push the button to make random numbers in the label1. But I need to make that move the picture.

Thanks
Mar 29 '07 #8
SammyB
807 Expert 512MB
Don't hate ur Computer
:)
I don't know about that. Computers, cars, and lawn mowers are not here to stay. I agree with Thomas Watson, past president of IBM, who allegedly said, "I think there is a world market for maybe five computers." :D
Mar 29 '07 #9
I don't know about that. Computers, cars, and lawn mowers are not here to stay. I agree with Thomas Watson, past president of IBM, who allegedly said, "I think there is a world market for maybe five computers." :D
Fine that is enough I don't hate my 2 new vista computers just my 5 old ones. Now that I got that over with lets stay on topic with the game that I want to program.

Thanks
Mar 29 '07 #10
Killer42
8,435 Expert 8TB
...I push the button to make random numbers in the label1. But I need to make that move the picture.
Not to sure about VBA, but in VB to move a picture you just use the .Move method of the PictureBox or ImageBox control, or set the .Top and/or .Left properties.
Mar 29 '07 #11
Not to sure about VBA, but in VB to move a picture you just use the .Move method of the PictureBox or ImageBox control, or set the .Top and/or .Left properties.
In VBA it is .Top and/or .Left but how can I make that random?

Thanks
Mar 30 '07 #12
Killer42
8,435 Expert 8TB
In VBA it is .Top and/or .Left but how can I make that random?
Well, one way to move it at random would be to generate 2 random numbers, one for X coordinate and one for Y. Then you just add your random numbers to the .Top and .Left values.

There are better ways, of course. This tends to make them jiggle around like a mad thing.

What I like to do in such a case is initially establish a random value for the "speed" in each direction (horizontal/vertical). Then each move, you add that "speed" value to the current value. That way, the object moves in a random direction, but keep on moving that way until you change it.
Mar 30 '07 #13
Well, one way to move it at random would be to generate 2 random numbers, one for X coordinate and one for Y. Then you just add your random numbers to the .Top and .Left values.

There are better ways, of course. This tends to make them jiggle around like a mad thing.

What I like to do in such a case is initially establish a random value for the "speed" in each direction (horizontal/vertical). Then each move, you add that "speed" value to the current value. That way, the object moves in a random direction, but keep on moving that way until you change it.
That would be great. Could I do a thing where it it would bounce off the sides?

Thanks
Mar 30 '07 #14
Killer42
8,435 Expert 8TB
That would be great. Could I do a thing where it it would bounce off the sides?
Sure, that's easy. You just check when you move it. If the horizontal position is < 0 or > form width, reverse the movement in that direction. Same for Y.

I'm just leaving work, but when I get home I'll see whether I have time to put together a simple demo that bounces around a window. It'll be in VB6, but should give you some ideas to play with.
Mar 30 '07 #15
Killer42
8,435 Expert 8TB
Well, I've developed a working sample. It's a form which contains a picturebox. A number of coloured dots bounce around inside the picturebox.

There are buttons to add and remove dots, a scollbar to adjust the animation speed, a button to pause/continue the animation, and a checkbox to turn erasing of the dots on and off. If you turn the "tails" on, the dots aren't erased each time things are drawn, so they leave a trail behind. It help to show off the colour-cycling.

Sorry, I got a bit carried away.

Unfortunately, I'm not sure whether you will be able to make any use of it, but perhaps you can get some ideas from it.

First, I'll see whether I can attach a copy of the zipped project. Stay tuned...

How about that, it worked!

The zip contains a compiled EXE you should be able to run, so you can see what I'm talking about, anyway. I hope the code may give you some ideas, though it's probably not very good.
Attached Files
File Type: zip Bouncing objects.zip (16.2 KB, 142 views)
Mar 30 '07 #16
Well, I've developed a working sample. It's a form which contains a picturebox. A number of coloured dots bounce around inside the picturebox.

There are buttons to add and remove dots, a scollbar to adjust the animation speed, a button to pause/continue the animation, and a checkbox to turn erasing of the dots on and off. If you turn the "tails" on, the dots aren't erased each time things are drawn, so they leave a trail behind. It help to show off the colour-cycling.

Sorry, I got a bit carried away.

Unfortunately, I'm not sure whether you will be able to make any use of it, but perhaps you can get some ideas from it.

First, I'll see whether I can attach a copy of the zipped project. Stay tuned...

How about that, it worked!

The zip contains a compiled EXE you should be able to run, so you can see what I'm talking about, anyway. I hope the code may give you some ideas, though it's probably not very good.
I didn't understand how you did the animation. Help is great.

Thanks
Mar 31 '07 #17
Killer42
8,435 Expert 8TB
I didn't understand how you did the animation. Help is great.
Although the code may look a bit daunting, the way the animation is done is actually quite simple.

For each of the X (horizontal) and Y (vertical) axes (let's ignore the colour for now) you have a value, and a speed (which I referred to as "delta", often used to indicate a rate of change). At each tick of the timer, you simply add the delta to the current value, and like magic, you have movement.

Then there is some extra code to bounce when it hits a boundary, by reversing the delta and adding it again (twice). And a bit more to randomly change the speed now and then.
Mar 31 '07 #18
Although the code may look a bit daunting, the way the animation is done is actually quite simple.

For each of the X (horizontal) and Y (vertical) axes (let's ignore the colour for now) you have a value, and a speed (which I referred to as "delta", often used to indicate a rate of change). At each tick of the timer, you simply add the delta to the current value, and like magic, you have movement.

Then there is some extra code to bounce when it hits a boundary, by reversing the delta and adding it again (twice). And a bit more to randomly change the speed now and then.
So you have the value and the speed. The value is the X and Y. The speed is also the timer. The timer is based off of the scroll bar value. The higher the value the faster it is the lower the value the slower it is. When the timer changes you add the delta to the current value. I think I am starting to get how to do my code but how do you do the timer and make it add the delta. (code example with discription if possible) Thanks for spending your time helping me.
Mar 31 '07 #19
Killer42
8,435 Expert 8TB
So you have the value and the speed. The value is the X and Y. The speed is also the timer. The timer is based off of the scroll bar value. The higher the value the faster it is the lower the value the slower it is. When the timer changes you add the delta to the current value. I think I am starting to get how to do my code but how do you do the timer and make it add the delta. (code example with discription if possible) Thanks for spending your time helping me.
The timer code in my program was as follows...
Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.   Timer1.Interval = SpeedDelay
  3.   Static Busy As Boolean
  4.   Static I As Long
  5.   ' This is where all the animation happens...
  6.   If Not Busy Then
  7.     Busy = True
  8.     With Picture1
  9.       If Check1.Value = 0 Then
  10.         .Cls
  11.       End If
  12.       For I = 1 To NumberOfObjects
  13.         ApplyDeltaWithBounce X_pos(I), X_Delta(I), 0, .ScaleWidth, 2, -10, 10
  14.         ApplyDeltaWithBounce Y_pos(I), Y_Delta(I), 0, .ScaleHeight, 2, -10, 10
  15.         ApplyDeltaWithBounce R(I), R_Delta(I), 0, 255, 5, -15, 15
  16.         ApplyDeltaWithBounce G(I), G_Delta(I), 0, 255, 5, -15, 15
  17.         ApplyDeltaWithBounce B(I), B_Delta(I), 0, 255, 5, -15, 15
  18.         Picture1.PSet (X_pos(I), Y_pos(I)), RGB(R(I), G(I), B(I))
  19.       Next
  20.       .Refresh
  21.       DoEvents
  22.     End With
  23.     Busy = False
  24.   End If
  25. End Sub
This code is basically the entire animation.

It loops through all the dots (I probably shouldn't called them "objects", as it may cause confusion). For each one, it passes each value/delta pair to a generic routine called ApplyDeltaWithBounce. This routine is designed to take the value and the delta, and apply the delta (amount of change, remember) to the value. It also accepts limits beyond which the value should "bounce". The last three parameters allow me to insert a bit of random change in the movement (if you watch you'll see that the dots sometimes change direction for no apparent reason) by saying "n% of the time, change the delta to something within these limtis".

With this sort of "black box" approach you don't necessarily need to understand (at first) exactly how each part of the program works - just what effect it has. This routine simply changes the value by the delta amount, bouncing when it hits the specified limits. The Timer1_Timer event procedure does the animation.

If you like, we can simplify the example by skipping the colour-cycling and the check for re-entry (the Busy flag). As I said, I got a bit carried away. Here's the simpler version
Expand|Select|Wrap|Line Numbers
  1. Private Sub Timer1_Timer()
  2.   Timer1.Interval = SpeedDelay
  3.   Static I As Long
  4.   ' This is where all the animation happens...
  5.   With Picture1
  6.     If Check1.Value = 0 Then
  7.       .Cls
  8.     End If
  9.     For I = 1 To NumberOfObjects
  10.       ApplyDeltaWithBounce X_pos(I), X_Delta(I), 0, .ScaleWidth, 2, -10, 10
  11.       ApplyDeltaWithBounce Y_pos(I), Y_Delta(I), 0, .ScaleHeight, 2, -10, 10
  12.       Picture1.PSet (X_pos(I), Y_pos(I)), RGB(R(I), G(I), B(I))
  13.     Next
  14.     .Refresh
  15.     DoEvents
  16.   End With
  17. End Sub
  18.  
Apr 1 '07 #20
Private Sub Timer1_Timer()
Timer1.Interval = SpeedDelay
Static I As Long
' This is where all the animation happens...
With Picture1
If Check1.Value = 0 Then
.Cls
End If
For I = 1 To NumberOfObjects
ApplyDeltaWithBounce X_pos(I), X_Delta(I), 0, .ScaleWidth, 2, -10, 10
ApplyDeltaWithBounce Y_pos(I), Y_Delta(I), 0, .ScaleHeight, 2, -10, 10
Picture1.PSet (X_pos(I), Y_pos(I)), RGB(R(I), G(I), B(I))
Next
.Refresh
DoEvents
End With
End Sub
Can I change the Timer1.Interval = SpeedDelay to have a set interval so you wouldn't need a variable speed and scroll bar?
Apr 1 '07 #21
Killer42
8,435 Expert 8TB
Can I change the Timer1.Interval = SpeedDelay to have a set interval so you wouldn't need a variable speed and scroll bar?
Of course.

I specifically put in the scrollbar and the SpeedDelay variable to allow me to adjust the speed on the fly. There's no reason you can't just set the timer interval at design time and leave it.
Apr 1 '07 #22
Of course.

I specifically put in the scroll bar and the Speed Delay variable to allow me to adjust the speed on the fly. There's no reason you can't just set the timer interval at design time and leave it.
If your scroll bar is at the fastest side what is you speed variable and how can I put that in the timer interval thing?
Apr 1 '07 #23
Killer42
8,435 Expert 8TB
If your scroll bar is at the fastest side what is you speed variable and how can I put that in the timer interval thing?
To be honest, I don't think it matters what values I used. You should work out what you want to do, and play with values until you get the performance you want.

Just keep in mind that the timer's .Interval property specifies the number of milliseconds to pause between "ticks".

I just thought of something! Of you're working in VBA, there isn't any timer control, is there? There was a thread here not long ago in which we tracked down some alternatives. You may need to run some searches for things like "VBA timer control" and so on. (Actually, if I manage to find it, I might add it to the Tips & Tricks at the top of the forum.)
Apr 1 '07 #24
To be honest, I don't think it matters what values I used. You should work out what you want to do, and play with values until you get the performance you want.

Just keep in mind that the timer's .Interval property specifies the number of milliseconds to pause between "ticks".

I just thought of something! Of you're working in VBA, there isn't any timer control, is there? There was a thread here not long ago in which we tracked down some alternatives. You may need to run some searches for things like "VBA timer control" and so on. (Actually, if I manage to find it, I might add it to the Tips & Tricks at the top of the forum.)
Your right I don't need your value I just was lazy but I might find a better one if I try myself. I do know this timer code that spins a picture around the document maybe it could help us make a timer like yours for VBA if we change it.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2.  
  3. Sub Document_Open()
  4.     Application.OnTime When:=Now + TimeValue("00:00:02"), Name:="ShapeMover"
  5. End Sub
  6.  
  7. Sub ShapeMover()
  8.     Dim oSH As Shape
  9.     Set oSH = ActiveDocument.Shapes(1)
  10.     oSH.IncrementRotation 10
  11.  
  12.     Application.OnTime When:=Now + TimeValue("00:00:02"), Name:="ShapeMover"
  13. End Sub
The TimeValue("00:00:02") should be useful.
Apr 1 '07 #25
Killer42
8,435 Expert 8TB
The OnTime technique sounds quite useful.

I plan to gather together these sort of Timer substitutes and add them into the Tips & Tricks. Will include this one also, if it's not already there.
Apr 1 '07 #26
I found that the help in VBA said the you use an OnTime thing. As shown below.

Example
This example runs the macro named "Macro1" in the current module at 3:55 P.M.

Expand|Select|Wrap|Line Numbers
  1. Application.OnTime When:="15:55:00", Name:="Macro1"
This example runs the macro named "Macro1" 15 seconds from the time the example is run. The macro name includes the project and module name.

Expand|Select|Wrap|Line Numbers
  1. Application.OnTime When:=Now + TimeValue("00:00:15"), _
  2.     Name:="Project1.Module1.Macro1"
This example runs the macro named "Start" at 1:30 P.M. The macro name includes the project and module name.

Expand|Select|Wrap|Line Numbers
  1. Application.OnTime When:=TimeValue("1:30 pm"), _
  2.     Name:="VBAProj.Module1.Start"
So we would go:

Expand|Select|Wrap|Line Numbers
  1. Application.OnTime When:=Now + TimeValue("00:00:15"), _ Name:="J"
Where J is what we want to happen and we can change TimeValue("00:00:15") to the time that I want.
Apr 1 '07 #27

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Xad | last post by:
Hi, i'm looking for java code for a really simple, basic Pong game. As simple as possible - even just two paddles and a ball bouncing will do. Can anyone help? Thanks
0
by: junglist | last post by:
Hi, i have created a Pong game Application where two players control their pads using the keyboard. the problem is that one player is using the action keys VK_UP and VK_DOWN in keyPressed() and...
3
by: Sam | last post by:
Hey all, I want to create a computerised version of this game, though I'm not really sure how to go about it. For those who don't know how the game works, here's my attempt at a brief...
138
by: theodp | last post by:
--> From http://www.techdirt.com/articles/20040406/1349225.shtml Microsoft Patents Saving The Name Of A Game Contributed by Mike on Tuesday, April 6th, 2004 @ 01:49PM from the...
0
by: Will | last post by:
I am looking to include a pong game or similar in an application I have developed. I know it's possible to write a pong style game in VB is it possible to write a pong style game in VBA? & can...
3
by: news.iq.ca | last post by:
Hello. Under C:\Documents and Settings\Administrator\My Documents\My Projects\ I have created a folder, "ASPNET" where I plan to keep my projects. I have started Administrative Tools ->...
4
by: Dr_Locke_Z2A | last post by:
I was talking to my uncle the other day about getting into programming games and he recommended to start teaching myself with really simple games, like Pong simple. So I looked on google and...
7
by: DaVinci | last post by:
I am writing a pong game.but met some problem. the ball function to control the scrolling ball, void ball(int starty,int startx) { int di ,i; int dj,j; di = 1; dj = 1; i = starty;
6
by: drsmooth | last post by:
im just starting to work on AI things and i started with a pong game... i came up with a system that every time the ball hits a wall or a paddle it calls the alert() method of the AIController...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.