create account

Pong - A JavaScript Game by robertmacdonald

View this thread on: hive.blogpeakd.comecency.com
· @robertmacdonald · (edited)
$0.04
Pong - A JavaScript Game
<html>
<p>So I've recently gotten into JavaScript programming and I decided to make a little simple game... or so I thought... I eagerly began making the paddles as its the easiest object, it only goes up and down on the screen, after about 5-10 minutes I have the basic paddle engine working with the up and down arrows/W and S buttons depending on the player. Now comes the fun part I thought to myself, the ball! I quickly made a ball.js file and made the basic parameters thinking "well the ball needs an x and a y so I can keep track of the position, and a velocity in the x and y-direction to make it move." I made some basic functions and then proceeded to watch the ball fly across the screen and through my paddle... I probably stared at my ball.hit function for an hour before getting annoyed closing my IDE and going to bed. The next day I got a little help from a friend and he fixed my small little syntax error (rooky mistake) and the ball now bounced back and forth between the two paddles, success!! Well until I had to come up with a way to make it bounce at an angle..... so I came up with a little simple trigonometry algorithm to solve this issue and confidently ran my index.html to watch the ball and all my hopes and dreams bounce off the paddle and into the infinite void of the bottom of the screen.. I realized I didn't take into account the top and bottom of the screen. I threw together some code that I figured was logical, just reverse the y velocity as it reaches or exceeds the boundaries of the screen, 5 = -5, 10 = -10. I cannot tell you how many times I tried this method without success finally I got frustrated and went to bed. In bed, I just kept thinking about it until I thought "If I'm just going to stay up thinking about it I might as well get up and code it." so I changed what I had to two functions called this.hitTop() and this.hitBottom() and put either this.ballVy += (-2 * this.ballVy); or this.ballVy -= (2 * this.ballVy); instead of this.ballVy * -1; both ways technically change 5 to -5 and -5 to 5 but for some reason, this way worked and the other didn't. So this pretty much concludes my JavaScript journey to make Pong, I hope you enjoyed!</p>
<p><br></p>
<p><img src="https://lh3.ggpht.com/BnwplGa64eYvmoOMSSuK26UOyQXuPrcv09LKUbG_CsPGI2e4mX7MEbL5O46tqiUgotQ=w300" width="300" height="300"/></p>
<p><br></p>
<p>Github link for those interested in seeing my ugly code:</p>
<p>https://github.com/robert-macdonald/Personal-Web/tree/master/Pong</p>
</html>
👍  , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorrobertmacdonald
permlinkpong-a-javascript-game
categoryprogramming
json_metadata{"tags":["programming","games","creativity"],"links":["https://github.com/robert-macdonald/Personal-Web/tree/master/Pong"],"app":"steemit/0.1","format":"html","image":["https://lh3.ggpht.com/BnwplGa64eYvmoOMSSuK26UOyQXuPrcv09LKUbG_CsPGI2e4mX7MEbL5O46tqiUgotQ=w300"]}
created2017-02-01 05:45:30
last_update2017-02-01 05:46:39
depth0
children4
last_payout2017-03-04 15:45:30
cashout_time1969-12-31 23:59:59
total_payout_value0.034 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,509
author_reputation2,496,882,375,498
root_title"Pong - A JavaScript Game"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd0
post_id2,394,424
net_rshares1,225,621,439,081
author_curate_reward""
vote details (23)
@naquoya ·
Well done on persevering.
👍  , , ,
👎  
properties (23)
authornaquoya
permlinkre-robertmacdonald-pong-a-javascript-game-20170201t063226260z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-02-01 06:32:30
last_update2017-02-01 06:32:30
depth1
children1
last_payout2017-03-04 15:45:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length25
author_reputation49,473,351,596,458
root_title"Pong - A JavaScript Game"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id2,394,629
net_rshares57,396,864,347
author_curate_reward""
vote details (5)
@robertmacdonald ·
Thanks, a lot comes from following through with what you set out to do.
👍  ,
👎  
properties (23)
authorrobertmacdonald
permlinkre-naquoya-re-robertmacdonald-pong-a-javascript-game-20170201t153500186z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-02-01 15:35:00
last_update2017-02-01 15:35:00
depth2
children0
last_payout2017-03-04 15:45:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length71
author_reputation2,496,882,375,498
root_title"Pong - A JavaScript Game"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id2,397,255
net_rshares140,583,017
author_curate_reward""
vote details (3)
@xeleos ·
Awesome, you're more fluent in JavaScript than I am, still don't know how to use Canvases yet lol, probably will learn some time. This is cool :3
properties (22)
authorxeleos
permlinkre-robertmacdonald-pong-a-javascript-game-2017215t19579596z
categoryprogramming
json_metadata{"tags":"programming","app":"esteem/1.3.8","format":"markdown+html"}
created2017-02-16 00:57:12
last_update2017-02-16 00:57:12
depth1
children1
last_payout2017-03-04 15:45:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length145
author_reputation269,068,919,450
root_title"Pong - A JavaScript Game"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id2,518,459
net_rshares0
@robertmacdonald ·
The createCanvas function is actually part of the p5js library.  The draw and setup are also from that library. I am better at javascript than I am at java, but I'm getting there.
👍  
properties (23)
authorrobertmacdonald
permlinkre-xeleos-re-robertmacdonald-pong-a-javascript-game-2017215t19579596z-20170216t014225367z
categoryprogramming
json_metadata{"tags":["programming"],"app":"steemit/0.1"}
created2017-02-16 01:42:24
last_update2017-02-16 01:42:24
depth2
children0
last_payout2017-03-04 15:45:30
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length179
author_reputation2,496,882,375,498
root_title"Pong - A JavaScript Game"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id2,518,754
net_rshares364,588,141
author_curate_reward""
vote details (1)