Coursera-an Introduction to Interactive programming in Python (Part 1)-mini-project #4-"Pong"

Source: Internet
Author: User

In this project, we'll build a version of Pong, one of the first arcade video games (1972). While Pong isn't particularly exciting compared to today's video games, Pong was relatively simple to build and provides a Nice opportunity to work on the skills so you'll need to build a game likeasteroids. As usual, we have the provided a program template , which can be used to the guide your development of Pong.

#implementation of classic arcade game PongImportSimpleguiImportRandom#Initialize Globals-pos and vel encode vertical info for paddlesWIDTH = 600HEIGHT= 400Ball_radius= 20Pad_width= 8Pad_height= 80Half_pad_width= PAD_WIDTH/2Half_pad_height= PAD_HEIGHT/2 Left=Falseright=True#Initialize Ball_pos and ball_vel for new bal in middle of table#If direction is right, the ball's velocity is upper right, and else upper leftdefSpawn_ball (direction):GlobalBall_pos, Ball_vel#these is vectors stored as lists        ifDirection = =Right :#ball_vel[0] = Random.randrange (2, 4)        #ball_vel[1] = Random.randrange (1, 3)Ball_vel[0] + = 1ball_vel[1] + = 1elifDirection = =Left :#ball_vel[0] =-Random.randrange (2, 4)        #ball_vel[1] = Random.randrange (1, 3)Ball_vel[0]-= 1ball_vel[1]-= 1defIni_pad ():GlobalScore1, Score2, Paddle1_pos, Paddle1_p, Paddle2_pos, paddle2_p, Paddle1_vel , Paddle2_vel paddle1_p= HEIGHT/2-half_pad_height paddle2_p= HEIGHT/2-half_pad_height Score1=0 Score2=0 Paddle1_vel=0 Paddle2_vel=0 Paddle1_pos=[ [0,0], [0,0], [0,0], [0,0]] Paddle2_pos=[ [0,0], [0,0], [0,0], [0,0]]#Define event handlersdefnew_game ():GlobalPaddle1_pos, Paddle1_p, Paddle2_pos, Paddle2_p, Paddle1_vel, Paddle2_vel#these is numbers    GlobalScore1, Score2#these is ints    GlobalBall_pos, Ball_vel ball_pos= [WIDTH/2, HEIGHT/2] Ball_vel= [1, 1] Ini_pad () Spawn_ball (right)defDraw (Canvas):GlobalScore1, Score2, Ball_pos, Ball_velGlobalPaddle1_pos, Paddle1_p, Paddle2_pos, Paddle2_p, Paddle1_vel, Paddle2_vel#draw mid line and guttersCanvas.draw_line ([Width/2, 0],[WIDTH/2, HEIGHT], 1," White") Canvas.draw_line ([Pad_width, 0],[pad_width, HEIGHT],1," White") Canvas.draw_line ([WIDTH-Pad_width, 0],[width-pad_width, HEIGHT], 1," White")            #Update BallBall_pos[0] + =Ball_vel[0] ball_pos[1] + = Ball_vel[1]    #Collision Detection    ifBall_pos[0] <= Ball_radius +Pad_width:ball_vel[0]= -Ball_vel[0]elifBall_pos[0] >= Width-pad_width-Ball_radius:ball_vel[0]= -Ball_vel[0]ifBALL_POS[1] <=ball_radius:ball_vel[1] =-ball_vel[1]    elifBALL_POS[1] >= HEIGHT-ball_radius:ball_vel[1] =-ball_vel[1]    #Draw BallCanvas.draw_circle (Ball_pos, Ball_radius, 5,'Blue',' White')    #Update paddle ' s vertical position, keep paddle on the screenpaddle1_p+=Paddle1_velifPaddle1_p <=0:paddle1_p=0ifPaddle1_p >= HEIGHT-pad_width:paddle1_p= HEIGHT-Pad_width Paddle1_pos[0]=[0, paddle1_p] paddle1_pos[1] =[Pad_width, paddle1_p] paddle1_pos[2] = [Pad_width, paddle1_p +Pad_height] paddle1_pos[3] = [0, paddle1_p +Pad_height] Paddle2_p+=Paddle2_velifPaddle2_p <=0:paddle2_p=0ifPaddle2_p >= HEIGHT-pad_width:paddle2_p= HEIGHT-Pad_width Paddle2_pos[0]= [WIDTH-pad_width, paddle2_p] paddle2_pos[1] =[WIDTH, paddle2_p] paddle2_pos[2] = [WIDTH, paddle2_p +Pad_height] paddle2_pos[3] = [Width-pad_width, paddle2_p +Pad_height]#Draw PaddlesCanvas.draw_polygon (Paddle1_pos, 1,'Blue',' White') Canvas.draw_polygon (Paddle2_pos,1,'Blue',' White')    #determine whether paddle and ball collide    ifBall_pos[0] <= Ball_radius +Pad_width:ifBALL_POS[1] >= paddle1_pos[0][1] andBALL_POS[1] <= paddle1_pos[2][1]: Spawn_ball (right)Else: Score2+ = 1ifBall_pos[0] >= Width-pad_width-Ball_radius:ifBALL_POS[1] >= paddle2_pos[0][1] andBALL_POS[1] <= paddle2_pos[2][1]: Spawn_ball (left)Else: Score1+ = 1#Draw scoresCanvas.draw_text (str (score1), [WIDTH/2-40, 40], 30,' White') Canvas.draw_text (str (score2), [WIDTH/2 + 20, 40], 30,' White')defKeyDown (key):GlobalPaddle1_vel, Paddle2_velifKey = = Simplegui. key_map['W']: Paddle1_vel=-8elifKey = = Simplegui. key_map['s']: Paddle1_vel= 8ifKey = = Simplegui. key_map[' up']: Paddle2_vel=-8elifKey = = Simplegui. key_map[' Down']: Paddle2_vel= 8defKeyUp (key):GlobalPaddle1_vel, Paddle2_velifKey = = Simplegui. key_map['W']: Paddle1_vel=0elifKey = = Simplegui. key_map['s']: Paddle1_vel=0ifKey = = Simplegui. key_map[' up']: Paddle2_vel=0elifKey = = Simplegui. key_map[' Down']: Paddle2_vel=0defButton_restart (): New_game ()#Create frameframe = Simplegui.create_frame ("Pong", WIDTH, HEIGHT) Frame.set_draw_handler (Draw) Frame.set_keydown_handler (KeyDown) Frame.set_keyup_handler (KeyUp) Frame.add_button ("Restart", button_restart,100)#Start Framenew_game () Frame.start ( )

Coursera-an Introduction to Interactive programming in Python (Part 1)-mini-project #4-"Pong"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.