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
  8. ball_radii = 10
  9. pad_width = 8
  10. pad_height = 80
  11. half_pad_width = pad_width/2
  12. half_pad_height = pad_height/2
  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
  17.     global paddle1_pos,paddle2_pos
  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():
  24.     global paddle1_vel,paddle2_vel #these are floats
  25.     global score1,score2 #these are int
  26.     paddle1_vel =0
  27.     paddle2_vel =0
  28.     score1 =0
  29.     score2 =0
  30.     ball_init()
  31.    
  32. def draw(c):
  33.     global score1,score2,paddle1_pos,paddle2_pos,ball_pos,ball_vel
  34.     #update paddle vertical pos & keep paddle on screen
  35.     #update mid line & gutters
  36.     c.draw_line([width/2,0],[width/2,height],1,"White")
  37.     c.draw_line([pad_width,0],[pad_width,height],1,"White")
  38.     c.draw_line([width - pad_width,0],[width - pad_width,height],1,"white")
  39.    
  40.     #####################paddies
  41.                  #paddle 1 movement
  42.     if  paddle1_pos[0][1] >0 and paddle1_pos[1][1] <400 :
  43.         paddle1_pos[0][1] +=paddle1_vel
  44.         paddle1_pos[1][1] +=paddle1_vel
  45.     elif(paddle1_pos[0][1] == 0 and paddle1_vel > 0):
  46.         paddle1_pos[0][1] +=paddle1_vel
  47.         paddle1_pos[1][1] +=paddle1_vel
  48.     elif(paddle1_pos[1][1] == 400 and  paddle1_vel < 0):
  49.         paddle1_pos[0][1] +=paddle1_vel
  50.         paddle1_pos[1][1] +=paddle1_vel
  51.        
  52.     c.draw_polygon(paddle1_pos, 8,"white")#paddle 1 draw
  53.    
  54.                #paddle 2 movement
  55.     if  paddle2_pos[0][1] > 0 and paddle2_pos[1][1] < 400 :
  56.         paddle2_pos[0][1] +=paddle2_vel
  57.         paddle2_pos[1][1] +=paddle2_vel
  58.     elif(paddle2_pos[0][1] == 0 and paddle2_vel > 0):
  59.         paddle2_pos[0][1] +=paddle2_vel
  60.         paddle2_pos[1][1] +=paddle2_vel
  61.     elif(paddle2_pos[1][1] == 400 and  paddle2_vel < 0):
  62.         paddle2_pos[0][1] +=paddle2_vel
  63.         paddle2_pos[1][1] +=paddle2_vel
  64.        
  65.     c.draw_polygon(paddle2_pos, 8,"white") #paddle 2 draw
  66.    
  67.     ########################update ball
  68.     ball_pos[0]+=ball_vel[0]
  69.     ball_pos[1]+=ball_vel[1]
  70.                                           #reflect along x
  71.     if(ball_pos[0]<=20 and  ball_pos[1]>= paddle1_pos[0][1] and ball_pos[1]<= paddle1_pos[1][1])# for paddle 1
  72.         ball_vel[0]= - ball_vel[0]
  73.     elif(ball_pos[0] <20):
  74.         score2+=1
  75.         ball_init()
  76.     elif(ball_pos[0] >= 577 and ball_pos[1]>= paddle2_pos[0][1] and ball_pos[1]<= paddle2_pos[1][1])#for paddle 2
  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
  85.     c.draw_circle((ball_pos[0], ball_pos[1]), ball_radii,20,'green')
  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):
  91.     global paddle1_vel,paddle2_vel
  92.     if key == simplegui.KEY_MAP["up"]:  #paddle 2
  93.         paddle2_vel = -3      
  94.     elif key == simplegui.KEY_MAP["down"]:
  95.         paddle2_vel = 3
  96.     elif key == simplegui.KEY_MAP["w"]#paddle 1
  97.          paddle1_vel = -3
  98.     elif key == simplegui.KEY_MAP["s"]:
  99.          paddle1_vel = 3
  100.    
  101. def keyup(key):
  102.     global paddle1_vel,paddle2_vel
  103.     paddle1_vel = 0
  104.     paddle2_vel = 0
  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)
  111. f.add_button("Restart",init,100)
  112. init()
  113. f.start()


   

5 comments:

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

    ReplyDelete
  2. I love all the posts, I really enjoyed, I would like more information about this, because it is very nice., Thanks for sharing. Write My Essays

    ReplyDelete
  3. Thanks for sharing amazing information !!!!!!
    Please keep up sharing.

    ReplyDelete
  4. The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!

    ReplyDelete
  5. The blog article very surprised to me! Your writing is good. In this I learned a lot! Thank you!

    ReplyDelete

THANKS FOR UR GREAT COMMENT

Blogger Widgets