6 Replies - 12283 Views - Last Post: 22 January 2009 - 10:28 PM Rate Topic: -----

#1 C4Mouse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Ping-Pong Keyboard controlled Paddles

Posted 05 May 2008 - 12:04 PM

Greetings,

I have done one of the tutorials on this site http://www.dreaminco...wtopic39686.htm creating a fully functional Ping-Pong program. Please be aware I am extremely new to any programming language and to VB.net itself.

After I completed the tutorial I went ahead to practice messing with things to help me learn and develop my own standards. I created things like two computers fighting each other.

I went ahead to attempt to create a Ping-Pong game that was for two players, clearly this was needed to be done on the keyboard, one on mouse and one on keyboard could give one a advantage so I decided that player one is "W = up" and "S = Down" player 2 is the arrows up and down.

The tutorial gave what seemed like a door to practice and fiddle with to try and attempt to complete this.

#Region "End Game on Escape Press"
	' Escape the game when escape has been pressed.
	Private Sub pongMain_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		If e.KeyValue = Keys.Escape Then
			Me.Close()
		End If
	End Sub
#End Region


So I messed around and finally came up with something like this

	Private Sub frmPong_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
		If e.KeyValue = Keys.W Then
			picPlayer1.Location = New Point(picPlayer1.Location.X + 5)
		End If
	End Sub


Of course I messed around a lot with this, I have been doing this type of thing all day since I woke up, nothing much else to do today, after much head banging I finally realised I will not crack it without assistants. This little bit of code shows you what I have tried to do.

All things I tried gave the same result, once I pressed the key the paddle went to the top of the page and to the left, I realised after testing if I removed +5 it just moved to the top of the page I can not figure out how to work it so the paddles move up and down.

Please Help, Thanks.

Is this post to long?


Is This A Good Question/Topic? 0
  • +

Replies To: Ping-Pong Keyboard controlled Paddles

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Ping-Pong Keyboard controlled Paddles

Posted 05 May 2008 - 12:58 PM

Just keep adding in the different key movements into the KeyDown event.

	  If e.KeyValue = Keys.W Then
			picPlayer1.Location = New Point(picPlayer1.Location.X + 5)
	  Else If e.KeyValue = Keys.S
			picPlayer1.Location = New Point(picPlayer1.Location.X - 5)
		End If


Was This Post Helpful? 0
  • +
  • -

#3 C4Mouse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Re: Ping-Pong Keyboard controlled Paddles

Posted 05 May 2008 - 01:04 PM

I am glad to see that I had it right, my fault on this however it is meant to be Y axis up and down the side of the page. However here are the results with X. The paddle jumps to top of page even over the menustrip and just moves across it. If I put the location. to Y it jumps to the top of the page over menustrip and does a similar thing again, moving across the X axis but in jumps.
Was This Post Helpful? 0
  • +
  • -

#4 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Ping-Pong Keyboard controlled Paddles

Posted 05 May 2008 - 01:35 PM

Well you are increasing the amounts by 5, so it will move in jumps of 5.

However, the problem here is more likely because you are not specifying a set of X and Y points when creating the new Point. You are only giving it one axis to work with, you really should be giving it both of them even though only one is changing.

Try this:
	  If e.KeyValue = Keys.W Then
			picPlayer1.Location = New Point(picPlayer1.Location.X , picPlayer1.Location.Y + 5)
	  Else If e.KeyValue = Keys.S
			picPlayer1.Location = New Point(picPlayer1.Location.X , picPlayer1.Location.Y - 5)
		End If

Was This Post Helpful? 0
  • +
  • -

#5 C4Mouse  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 05-May 08

Re: Ping-Pong Keyboard controlled Paddles

Posted 05 May 2008 - 01:57 PM

Thank you jayman9, seems to work well. I did come across a problem with it, the buttons are the wrong way around, I did not realise at first :) + would mean its going down, keep thinking its -, clearly a common mistake as you did it as well.

Another problem seems to be if two people use the keyboard and press two buttons are once it cuts out the other person, guess one person will need to use the mouse :D

Anyway solved, thank you so much, much to learn.

Was This Post Helpful? 0
  • +
  • -

#6 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Ping-Pong Keyboard controlled Paddles

Posted 05 May 2008 - 03:19 PM

Actually, I just copied and pasted your code and then made changes. I didn't even think about which way the picturebox was moving.

Either way, I am glad I was able to help.
Was This Post Helpful? 0
  • +
  • -

#7 Brewmaster  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-January 09

Re: Ping-Pong Keyboard controlled Paddles

Posted 22 January 2009 - 10:28 PM

Sorry, I put C# in the search engine and ended up in a VB thread...

This post has been edited by Brewmaster: 22 January 2009 - 10:41 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1