## Tuesday, 10 November 2015

### Pong Game In python

This is very easy if you are intermediate in python and good in basic physics and basic maths.
You need to know basics of reflection, velocity, acceleration, and basics mathematics in order to calculate collision .When boundary of pong just touches wall or paddle are situation of collision. rest of thing is just basic python.

We need to increase velocity of pong with time so we add some constant small  acceleration that is being triggered after some small time interval . why small because if we use large acceleration or time then change in velocity will be sudden, so speed of pong increase suddenly.

So now things you don't know is frames & its functions . So i suggest you go through  Basic Documentation of codeskulptor.org.
Documentation is very easy to understand, I suggest you to go through the code and follow function that looks unfamiliar to you.

## Pong Game [ Python ] :

To run this code open http://www.codeskulptor.org/ , then past the below [ Github Link ] code & Run .
1.     #Pong game : in Python
2.     #using Online plateform  http://www.codeskulptor.org/
3. import simplegui
4. import random
5. #globals
6. width =600
7. height = 400
13. #helper function that spawn ball,return position vector & velocity vector
14. #if right is true spawn to right ,else spawn to left
15. def ball_init():
16.     global ball_pos,ball_vel #these are vector stored in list
18.     ball_pos=[300,200]
19.     ball_vel=[random.choice([-3, 3]),random.choice([3,-3])]
20.     paddle1_pos = [[4, 165], [4, 235]]
21.     paddle2_pos = [[596, 165], [596, 235]]
22.
23. def init():
25.     global score1,score2 #these are int
28.     score1 =0
29.     score2 =0
30.     ball_init()
31.
32. def draw(c):
35.     #update mid line & gutters
36.     c.draw_line([width/2,0],[width/2,height],1,"White")
39.
51.
53.
64.
66.
67.     ########################update ball
68.     ball_pos[0]+=ball_vel[0]
69.     ball_pos[1]+=ball_vel[1]
70.                                           #reflect along x
72.         ball_vel[0]= - ball_vel[0]
73.     elif(ball_pos[0] <20):
74.         score2+=1
75.         ball_init()
77.         ball_vel[0]= - ball_vel[0]
78.     elif(ball_pos[0] > 577):
79.         score1+=1
80.         ball_init()
81.
82.     elif(ball_pos[1] <= 15 or ball_pos[1] >= 385 ):
83.         ball_vel[1] = -ball_vel[1]          #reflect along y
84.     #######################draw ball & score
86.
87.     c.draw_text(str(score1), (150, 50), 24, 'Yellow')
88.     c.draw_text(str(score2), (450, 50), 24, 'Yellow')
89.
90. def keydown(key):
92.     if key == simplegui.KEY_MAP["up"]:  #paddle 2
94.     elif key == simplegui.KEY_MAP["down"]:
96.     elif key == simplegui.KEY_MAP["w"]#paddle 1
98.     elif key == simplegui.KEY_MAP["s"]:
100.
101. def keyup(key):
105.
106. # create Frame
107. = simplegui.create_frame("Pong",width,height)
108. f.set_draw_handler(draw)
109. f.set_keydown_handler(keydown)
110. f.set_keyup_handler(keyup)
112. init()
113. f.start()

1. Really great post, Thank you for sharing this knowledge. Excellently written article, if only all bloggers offered the same level of content as you, the internet would be a much better place. Please keep it up!..

Working at college paper writing service

2. Amazing article. I am so impressed. I think you have a great knowledge especially while dealings with such subjects. Custom Essay Writing Service