Little help pls
Little help pls
Little help pls
Little help pls
Little help pls
Little help pls Little help pls Little help pls Little help pls Little help pls Little help pls Little help pls Little help pls
Little help pls Little help pls
Little help pls
Go Back  Xtreme Visual Basic Talk > > > Little help pls


Reply
 
Thread Tools Display Modes
  #1  
Old 01-28-2005, 09:44 AM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Talking Little help pls


Ok, so heres the fill in. I recently completed a space invaders game...it was my very first game! It worked great, and I couldn't have done it without the enthusiastic help of all you who participate on this forum. In any case, after completing that, I delved into GDI+.

Learning some of the basics (like namespaces, and rectangles, pens, brushes, etc), I created a pong game.

Pong works well. I have music in there and everything. Only a few little problems.

Basically, the paddles don't stop at the top and bottom of the form. My code to try to make this work looks like this:

Code:
If uk = True Then Paddle2.Offset(UpX, UpY) Me.Invalidate() End If If dk = True Then Paddle2.Offset(DownX, DownY) Me.Invalidate() End If If Paddle2.Top = 0 Then Paddle2.Offset(DownX, DownY) Me.Invalidate() ElseIf Paddle2.Top = Me.ClientSize.Height - Paddle2.Height Then Paddle2.Offset(UpX, UpY) End If

I have the same for the other paddle. Like I said though, they don't work....

What am I doing wrong???

Ok, and perhaps more importantly, there is this really weird thing in my program. The form size seems to be restricted! I can make form1 say, 600 by 600. But if I try to draw a paddle farther left than 300, it just disappears. I know that the paddle is being drawn though, because i tested this with the ball. When the ball passes the 300 mark, it just dissapears. A few seconds later, it returns to visibility, travelling back from around 300.

What is going on?!?!?!?!?!?

If you need to see my code, just ask

Thanks guys.

Last edited by N_S; 01-28-2005 at 09:46 AM. Reason: Grammar
Reply With Quote
  #2  
Old 01-28-2005, 04:47 PM
Unit92 Unit92 is offline
Regular
 
Join Date: Jul 2003
Posts: 97
Default

I can't tell from what you're posted there I'd need to see more of your code.
__________________
1337Geek.com - Technical Knowledge Base. Safe haven for Geeks of all types.
Reply With Quote
  #3  
Old 01-28-2005, 06:33 PM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Fair enough. Here is the whole project so far (basically I just have the game itself; no splash page or whatever...yet).

Code:
Imports System.Drawing Imports System.Runtime.InteropServices Public Class Form1 Inherits System.Windows.Forms.Form <DllImport("winmm.dll", EntryPoint:="mciSendStringA")> _ Public Shared Function mciSendString(ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long 'Used to call MCI API End Function 'Windows Region Code here Dim g As Graphics = Me.CreateGraphics Dim pPen As Pen = New Pen(Color.Black, 4) Dim Paddle1 As Rectangle = New Rectangle(10, 75, 20, 128) Dim fFont As Font = New Font("Verdana", 10, FontStyle.Bold) Dim Paddle2 As Rectangle = New Rectangle(267, 75, 20, 128) Dim ball As Rectangle = New Rectangle(150, 150, 10, 10) Dim UpX As Integer Dim UpY As Integer Dim DownX As Integer Dim DownY As Integer Dim VeX As Integer = 5 Dim VeY As Integer = -5 Dim p1Score As Integer = 0 Dim p2score As Integer = 0 Dim uk As Boolean 'uk for up key Dim dk As Boolean 'dk for down key Dim wk As Boolean 'wk for the w key Dim sk As Boolean 'sk for the s k 'playing a midi (no problems here) Dim filename As String = "C:\song1.mid" Public Function FindFormat(ByVal r_strFilePath As String) As String Dim strExt As String strExt = r_strFilePath.Substring(r_strFilePath.Length - 4) Select Case strExt.ToLower Case ".mid" FindFormat = "Sequencer" End Select End Function 'drawing the rectangles and stuff here Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs) g.DrawLine(pPen, 150, 10, 150, 300) g.FillRectangle(New SolidBrush(Color.Lime), ball) g.DrawRectangle(pPen, ball) g.DrawString("Player 1 Score: " & p1Score, fFont, New SolidBrush(Color.Brown), 10, 10) g.DrawString("Player 2 Score: " & p2score, fFont, New SolidBrush(Color.Brown), 160, 10) g.FillRectangle(New SolidBrush(Color.Blue), Paddle1) g.DrawRectangle(pPen, Paddle1) g.FillRectangle(New SolidBrush(Color.Red), Paddle2) g.DrawRectangle(pPen, Paddle2) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load UpX = 0 UpY = -10 DownX = 0 DownY = 10 Timer1.Enabled = True 'timer1 for the ball movement Timer1.Interval = 25 Timer2.Enabled = True 'timer2 for one of the paddles movement Timer2.Interval = 25 Timer2.Enabled = True Timer2.Interval = 25 Timer3.Enabled = True 'timer3 for the other paddles movement Timer3.Interval = 25 'just playing the song mciSendString("Open " & Chr(34) & filename & Chr(34) & " Type " & FindFormat(filename) & " Alias MedAls", 0, 0, 0) mciSendString("Play MedAls", 0, 0, 0) mciSendString("Loop MedAls", 0, 0, 0) 'for some reason, the center screen property wasn't working....so i just manually centered the form Me.Left = 250 Me.Top = 150 End Sub 'key code here Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown If e.KeyCode = Keys.Up Then uk = True dk = False ElseIf e.KeyCode = Keys.Down Then dk = True uk = False End If If e.KeyCode = Keys.W Then wk = True sk = False ElseIf e.KeyCode = Keys.S Then sk = True wk = False End If End Sub Private Sub Form1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyUp If e.KeyCode = Keys.Up Then uk = False ElseIf e.KeyCode = Keys.Down Then dk = False End If If e.KeyCode = Keys.W Then wk = False ElseIf e.KeyCode = Keys.S Then sk = False End If End Sub 'ball movement here Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick ball.Offset(VeX, VeY) Me.Invalidate() If ball.IntersectsWith(Paddle1) Then VeX = -VeX End If If ball.IntersectsWith(Paddle2) Then VeX = -VeX End If If ball.IntersectsWith(Paddle2) Then End If If ball.Top > Me.ClientSize.Height Then VeY = -VeY End If If ball.Top < 0 Then VeY = -VeY End If If ball.Left > Me.ClientSize.Width Then VeX = -VeX p1Score += 1 End If If ball.Left < 0 Then VeX = -VeX p2score += 1 End If Me.Invalidate() End Sub 'paddle2 movement Private Sub Timer2_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer2.Tick If uk = True Then Paddle2.Offset(UpX, UpY) Me.Invalidate() End If If dk = True Then Paddle2.Offset(DownX, DownY) Me.Invalidate() End If If Paddle2.Top = 0 Then Paddle2.Offset(DownX, DownY) Me.Invalidate() ElseIf Paddle2.Top = Me.ClientSize.Height - Paddle2.Height Then Paddle2.Offset(UpX, UpY) End If End Sub 'paddle1 movement Private Sub Timer3_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer3.Tick If wk = True Then Paddle1.Offset(UpX, UpY) Me.Invalidate() End If If sk = True Then Paddle1.Offset(DownX, DownY) Me.Invalidate() End If If Paddle1.Top = 0 Then Paddle1.Offset(DownX, DownY) Me.Invalidate() ElseIf Paddle1.Top = Me.ClientSize.Height - Paddle1.Height Then Paddle1.Offset(UpX, UpY) End If End Sub 'stopping the midi on leaving the form Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing mciSendString("STOP MedAls", String.Empty, 0, 0) mciSendString("STOP " & IO.Path.GetFileNameWithoutExtension(filename), String.Empty, 0, 0) mciSendString("CLOSE MedAls", String.Empty, 0, 0) End End Sub End Class

That's basically all the code that I have so far. Take a look and offer any advice/help!

One more thing. The paddles and stuff keep flickering when the program is running. I know why this is, though. It's because, for the ball movement, I am constantly calling the me.invalidate() method, so the form is constantly being redrawn. I'm wondering if there is a way to make this *smoother*, or make it so that only the ball gets redrawn (thus making the paddles stop flickering...).

Thanks

Last edited by N_S; 01-28-2005 at 06:36 PM. Reason: Forgot some stuff
Reply With Quote
  #4  
Old 01-29-2005, 01:29 PM
Unit92 Unit92 is offline
Regular
 
Join Date: Jul 2003
Posts: 97
Default

You know what I didn't need to see all that I just realized your mistake

Code:
If Paddle2.Top <= 0 Then
            Paddle2.Offset(DownX, DownY)
            Me.Invalidate()
        ElseIf Paddle2.Top >= Me.ClientSize.Height - Paddle2.Height Then
            Paddle2.Offset(UpX, UpY)
        End If
__________________
1337Geek.com - Technical Knowledge Base. Safe haven for Geeks of all types.
Reply With Quote
  #5  
Old 01-29-2005, 09:13 PM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Wow, of course. I can't believe I didn't see it myself. Have to use < and > because the offset might not leave the paddles directly on 0 and the form height. Thanks for pointing that out though (still can't believe I didnt see it.)

Thanks a lot then man, but can any1 answer my other questions??? Still having the big problem about not being able to see GDI+ past certain areas of the form. (Any input on any other of my questions would be much appreciated).

Thanks again,
Reply With Quote
  #6  
Old 01-30-2005, 12:20 PM
theController theController is offline
Centurion
 
Join Date: May 2004
Posts: 120
Default

Quote:
Originally Posted by N_S

Thanks a lot then man, but can any1 answer my other questions??? Still having the big problem about not being able to see GDI+ past certain areas of the form. (Any input on any other of my questions would be much appreciated).

Thanks again,
I had the same problem. I fixed it by, *checks over code*, I think it was, a matter of order of code.

Did you put graphics = Me.CreateGraphics after you created and set up the form and everything?

If not, try and make it the last thing you do...
Reply With Quote
  #7  
Old 01-30-2005, 06:20 PM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Awesome! Thanks a lot controller. Found where my mistake was (or at least, based on what you explained to me, i figured out how to fix it). Been a great help.

Um, this may be pushing my luck, but, as I've come this far, I might as well try. As I already mentioned, because of the balls movement, I am calling the me.invalidate() method an awful lot (each time the timer code runs). Thus, the paddles don't look very consistant (that is, they are being redrawn so much that it doesn't look very smooth). I am just starting to think about this now, so I may figure it out (*doubtful*).

Anyway, thanks again controller. Awesome job!
Reply With Quote
  #8  
Old 01-31-2005, 06:44 AM
elnerdo's Avatar
elnerdo elnerdo is offline
Contributor
 
Join Date: Jul 2004
Location: Northern New Jersey
Posts: 490
Default

The way to fix that is to declare a bitmap and draw to the bitmap instead and then draw the bitmap at the end of every loop.
__________________
nerds unite!
Reply With Quote
  #9  
Old 01-31-2005, 07:08 AM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Woa! Ok, time to read up on bitmaps....
Reply With Quote
  #10  
Old 01-31-2005, 07:33 AM
elnerdo's Avatar
elnerdo elnerdo is offline
Contributor
 
Join Date: Jul 2004
Location: Northern New Jersey
Posts: 490
Default

They're easy(I'm basically quoting what Iceplug told me a few weeks ago ;-))

You dim a bitmap
Code:
dim mybitmap as bitmap = new bitmap(widthofyourform,heightofyourform) 'that wil lmake an empty bitmap that's the same size as your form
then you create a graphics object to it almost like normal
Code:
dim bitmapg as graphics = graphics.fromimage(mybitmap)
and then you can draw to it like normal using that graphics object
and then after you've drawn everything (At the end of your loop/timer) you draw the bitmap using another graphics object that you declared like normal
Code:
dim formg as graphics = me.creategraphics 'this is the normal graphics, draws to the form
And make sure not to use the paint sub now, simply draw the new bitmap right on top of the old one
__________________
nerds unite!
Reply With Quote
  #11  
Old 01-31-2005, 09:31 AM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Quote:
Originally Posted by elnerdo
They're easy(I'm basically quoting what Iceplug told me a few weeks ago ;-))

You dim a bitmap
Code:
dim mybitmap as bitmap = new bitmap(widthofyourform,heightofyourform) 'that wil lmake an empty bitmap that's the same size as your form
then you create a graphics object to it almost like normal
Code:
dim bitmapg as graphics = graphics.fromimage(mybitmap)
and then you can draw to it like normal using that graphics object
and then after you've drawn everything (At the end of your loop/timer) you draw the bitmap using another graphics object that you declared like normal
Code:
dim formg as graphics = me.creategraphics 'this is the normal graphics, draws to the form
And make sure not to use the paint sub now, simply draw the new bitmap right on top of the old one
Ok, so...I dim the bitmap globally, right?

then this goes into the paint sub, or is it global, too???
[vb] dim bitmapg as graphics = graphics.fromimage(mybitmap)

then in the paint sub i draw like normal???
[vb]g.drawrectangle(pPen, rRect)

If this is correct, then I understand you so far. But what do you mean by don't use the paint sub now?

Srry for the trouble, and I don't like asking you to directly give me the code, so maybe if you could just explain to me how I would use the bitmap to draw.

Thanks elnerdo
Reply With Quote
  #12  
Old 01-31-2005, 09:45 AM
elnerdo's Avatar
elnerdo elnerdo is offline
Contributor
 
Join Date: Jul 2004
Location: Northern New Jersey
Posts: 490
Default

Instead of using the paint sub(It will clear the form, which will cause a flicker) make a new sub, call it draw. In that sub, draw everything that you would normally draw to the form to the bitmap using the bitmap's graphics object
Code:
private sub draw() 'First clear, normally this is what would flicker but since you don't get to see 'it clear, that's not a problem bitmapg.fillrectangle(new solidbrush(color.blue),new rectangle(0,0,Widthofform,Heightofform) 'now draw all your normal stuff bitmapg.drawline(imaginarypen,0,0,100,100) bitmapg.fillrectangle(imaginarybrush,imaginaryrectangle) bitmapg.drawimage(imaginaryimage,0,0) 'you get the point
Now after you've done that you've drawn all your stuff(ish) but nothing will show up on the form because you havn't drawn your bitmap yet, so now simply draw the bitmap at the end of your draw sub
Code:
formg.drawimage(mybitmap,0,0) end sub

'This way, the user never gets to see the clearing of the screen.
__________________
nerds unite!
Reply With Quote
  #13  
Old 01-31-2005, 10:15 AM
theController theController is offline
Centurion
 
Join Date: May 2004
Posts: 120
Default

yes, this way is perfect, it just draws ontop of the other bitmap. I actually have a question about this.. In time will the bitmap take up too much memory?

Just a thought. I use this technique in my game too, it works beautifully.

Oh P.S. and for a little trick.

You will notice that with this way, when a menu or another window goes over top of the game, it will erase the image until you redraw it. which could be pressing a move key. To get by this.. put two simple lines in the paint procedure,

Draw() ' this is your draw routine.
g.DrawImage(Bitmap, x, y) 'This is the bitmap that you are drawing to, (explained by nerd above!)

This refreshes everytime something updates the video. So there is no flicker because you are not re drawing everything, but it will update when the video is refreshed, like a window on top then shown again!

Last edited by theController; 01-31-2005 at 10:22 AM.
Reply With Quote
  #14  
Old 01-31-2005, 05:52 PM
Iceplug's Avatar
IceplugLittle help pls Iceplug is offline
MetaCenturion

Retired Moderator
* Guru *
 
Join Date: Aug 2001
Location: Iowa, USA
Posts: 16,583
Default

It is better if you declare all (both) of your graphics objects globally so that you don't have to keep creating Graphics objects over and over again.
Quote:
I actually have a question about this.. In time will the bitmap take up too much memory?
No... the bitmap takes up the same amount of memory that it does when you created it. All you are doing is changing the pixel values on its surface.
Quote:
put two simple lines in the paint procedure
If the draw event gets too large, then you can probably avoid calling it because things that you've drawn to the bitmap remain the same until you call Draw naturally within your timer/gameloop.
__________________

Iceplug, USN
Quadrill 1 Quadrill 2 (full) Quadrill 3 JumpCross .NET Website is ALIVE! - DL Platform Tour for VB.NET! Posting Guidelines Hint: Specify your location in your user cp profile if you want compassion!
Reply With Quote
  #15  
Old 01-31-2005, 06:54 PM
elnerdo's Avatar
elnerdo elnerdo is offline
Contributor
 
Join Date: Jul 2004
Location: Northern New Jersey
Posts: 490
Default

Iceplug, what he means is that if you were to drag the form off the screen and if for some reason the game loop was....dead, then the bitmap on the form would be erased, so you should call your draw sub from the paint sub too.

TheController, I agree with you on the first of the two simple lines, but the second is kind of unnecessary, the bitmap is drawn in the draw sub.
__________________
nerds unite!
Reply With Quote
  #16  
Old 01-31-2005, 06:56 PM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Hmmmmm. Sort of getting this now. Just a few problems though. I seem to be getting an error with this line of code:

Quote:
bitmapg.fillrectangle(New solidbrush(color.blue),New rectangle(0,0,Widthofform,Heightofform)
I changed the "Widthofform" and "Heightofform" to 650, 350 (as the form dimensions). However, there is a blue squiggly line at the very end of the code:

Code:
bitmapg.fillrectangle(New solidbrush(color.blue), New rectangle(0, 0, Me.Width, me.Height) 'right at the end bracket, the ")" sign, there is a squiggly line. When I hover my mouse over it, it says: ')' expected...Also, i tried making these last two things 650, 350, but the same thing occurred...

Also, there is nothing showing up when i run the program (continuing past the error). Here is my code:

Code:
Private Sub Draw() bitmapg.fillrectangle(New solidbrush(color.blue), New rectangle(0, 0, Me.Width, me.Height) bitmapg.DrawString("Player 1 Score: " & p1Score, fFont, New SolidBrush(Color.Brown), 10, 10) bitmapg.DrawString("Player 2 Score: " & p2score, fFont, New SolidBrush(Color.Brown), 500, 10) bitmapg.DrawLine(pPen, 315, 10, 315, 305) bitmapg.FillRectangle(New SolidBrush(Color.Lime), ball) bitmapg.DrawRectangle(pPen, ball) bitmapg.FillRectangle(New SolidBrush(Color.Blue), Paddle1) bitmapg.DrawRectangle(pPen, Paddle1) bitmapg.FillRectangle(New SolidBrush(Color.Red), Paddle2) bitmapg.DrawRectangle(pPen, Paddle2) formg.DrawImage(mybitmap, 0, 0) End Sub 'i also declared the graphic objects globally: Dim mybitmap As Bitmap = New Bitmap(650, 350) Dim bitmapg As Graphics = Graphics.FromImage(mybitmap) Dim formg As Graphics = Me.CreateGraphics

What's wrong!?!?!?!?!?

Last edited by N_S; 01-31-2005 at 06:58 PM. Reason: oops, wrong code lol.
Reply With Quote
  #17  
Old 01-31-2005, 07:02 PM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Oh yea, one more thing. How can I make my posts automatically wrap themselves to the next line (do I have to manually press enter?). Because it gets kind of annoying having to scroll over to read my whole post (when editing my posts especially, and i'm sure when others are reading them...)

Thanks.
Reply With Quote
  #18  
Old 01-31-2005, 07:03 PM
elnerdo's Avatar
elnerdo elnerdo is offline
Contributor
 
Join Date: Jul 2004
Location: Northern New Jersey
Posts: 490
Default

Well, for the error with clearing, That's just cause I forgot the ending parentheses. Just add a closing parenthesis (Or 2) to the end.

For the other things, this could not be the problem but just make sure to do this.
(I just remembered it now, sorry) If you dim formg as graphics and make it equal me.creategraphics globally,
it will be done before the form is created and it will think the form is 300x300.
Dim it globally, but make it = me.creategraphics in the form_load sub.


Other than that, I have absolutely no idea why your stuff isn't showing up.

Edit/Replytoyouredit: Yeah, I hate that, I'm fixing mine from that same problem now.
__________________
nerds unite!
Reply With Quote
  #19  
Old 01-31-2005, 08:00 PM
N_S N_S is offline
Freshman
 
Join Date: Dec 2004
Posts: 32
Default

Arg. Well, I did what u said; dim formg globally, then put the = me.creategraphics in form_load. Also fixed the little error (lol, ty elnerdo!). However, it still isn't showing up!

ARGGGG. lol. well, ty a lot guys, elnerdo and the controller, ice plug too. I guess i'll just think on this tomorrow. For now, I think I'll go to sleep...

If any1 else wants to help, just ask me to post the updated code (cause i'd be glad too). I wanna get this bitmap thing working!

Thanks again.

Last edited by N_S; 01-31-2005 at 08:08 PM.
Reply With Quote
  #20  
Old 01-31-2005, 08:05 PM
elnerdo's Avatar
elnerdo elnerdo is offline
Contributor
 
Join Date: Jul 2004
Location: Northern New Jersey
Posts: 490
Default

Few common errors that I make a lot:
Are you making sure to call your draw sub in your loop/timer?
Are you sure you're not invalidating anymore?
Are you sure that you're even getting to your game loop?

Try pressing...f10,f11,or f12(not sure which one) which will run the program
line by line, (I think you make it go to the next line by pressing f12) that
could help you understand what's happening. Also, try simply reading
through the program like the compiler would to pinpoint the errors
__________________
nerds unite!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Little help pls
Little help pls
Little help pls Little help pls
Little help pls
Little help pls
Little help pls Little help pls Little help pls Little help pls Little help pls Little help pls Little help pls
Little help pls
Little help pls
 
Little help pls
Little help pls
 
-->