Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use Python language to realize single squash game

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly explains "how to use Python language to achieve single squash game". The content of the article is simple and clear, and it is easy to learn and understand. let's study and learn how to use Python language to realize single-player squash game.

Project description

In this project, we try to use Python language to design a computer program to simulate the "single squash" game. The file name of the program is Squash.py, the program will use Python reserved words such as import, def, if/elif/else, return, and Python functions such as random.random (). It will also use List, variables, mathematical operation expressions, logical conditional expressions, string operation expressions, custom functions, code formats, comments and other features of Python language, the most special is to learn how to use the keyboard and canvas interaction.

When the live is 0 and the score does not reach 50, the challenge fails

This article will provide a template in which a function called new_game () has been designed to indicate the start of a new inning and serve. First of all, four global variables are initialized in the function, where paddle_pos is the position of the bezel; paddle_vel is the moving speed of the bezel, and the initial speed is set to 0% score, that is, the number of times to catch squash, the initial value is set to 0% live, and the initial value is set to 3. Secondly, the serve function spawn_ball () is called.

Need to complete the writing of the serve function. The main function of this function is to determine the initial coordinate position of the ball when serving, and the random function can be used to give the ball a speed in different directions.

Run the sample

The draw (canvas) function is mainly used to draw the game interface. The function of this function is to use draw_line (parameter) to draw the boundary line of the squash court and the baffle at the bottom; use draw_circle (parameter) to draw the ball; in addition, in this function, you have to complete the function of accumulating points in the score variable when the ball is caught, and live minus 1 point when the ball is not caught. Note that when the score is 50, the game passes; when the live is 0, the score does not reach 50, the challenge fails.

The keydown () function is used to respond to the left and right direction keys on the keypad ("→" key and "←" key). When the left key is pressed, the bezel moves to the left, and when the right button is pressed, the bezel moves to the right.

The keyup () function is used to respond to the left and right direction keys on the keypad ("→" key and "←" key). When the left key is released, the bezel moves to the left and stops, and when the right key is released, the bezel moves to the right.

Project template #-*-coding: utf-8-*-import simpleguitk as simpleguiimport random# initialization global variable WIDTH = 500 # canvas width HEIGHT = 500 # canvas height PADDLE_WIDTH = 50 # bezel width PADDLE_HEIGHT = 8 # bezel height Paddle_pos = WIDTH / 2 # bezel initial position paddle_vel = 0 # bezel initial velocity HALF_PADDLE_WIDTH = PADDLE_WIDTH / 2HALF_PADDLE_HEIGHT = PADDLE_HEIGHT / 2BALL_RADIUS = 8 # squash radius ball_pos = [WIDTH / 2 HEIGHT / 2] # initial position of squash ball_vel = [0,0] # initial speed of squash # serve def spawn_ball (): global ball_pos, ball_vel # the position and speed of squash are represented by a list containing two elements, respectively And declared as a global variable # step 2 code is written in the following passdef new_game (): global score, live # score and number of lives global paddle_pos, paddle_vel # position and speed of the bezel score = 0 live = 3 paddle_pos = WIDTH / 2 paddle_vel = 0 spawn_ball () def draw (canvas): global score, live, paddle_pos, ball_pos The ball_vel # code is written below # step 1 draw the line in the course # step 1 draw the ball # step 1 draw the bezel # step 1 draw the life and score # step 3 check the collision with the upper wall # step 3 check the collision with the left and right wall # step 4 update the horizontal position of the bezel # step 5 check and block Collision of plates passdef keydown (key): global paddle_vel if key = = simplegui.KEY_MAP ['left']: paddle_vel =-4 # change velocity direction elif key = = simplegui.KEY_MAP [' right']: paddle_vel = 4def keyup (key): global paddle_vel if key = = simplegui.KEY_MAP ['left'] or key = = simplegui.KEY_MAP [ 'right']: paddle_vel = creating frame frame = simplegui.create_frame ("single squash" WIDTH, HEIGHT) frame.set_draw_handler (draw) frame.set_canvas_background ("brown") frame.set_keydown_handler (keydown) frame.set_keyup_handler (keyup) frame.add_button ("restart", new_game,50) # run the framework new_game () frame.start () coding steps

It is recommended that you follow these steps to complete the coding in the template provided. (you can use your computer to open the article and copy the template code to PyCharm for writing.)

(1) draw the game interface, ball and bezel, that is, complete the drawing part of the draw () function

(2) write the serve function spawn_ball (), that is, give the ball the initial position and speed to send it down from the center of the screen, the direction is composed of horizontal speed and vertical speed, it is recommended to set to random.random () + 1. Because the random.random () function produces random numbers with uniform distribution between 0 and 1, random.random () + 1 is a random number with uniform distribution between 1 and 2.

(3) check the collision between the ball and the upper, left and right wall in the draw () function, and modify the ball speed according to the knowledge of rebound.

(4) in the draw () function, you only need to update the position of the bezel: paddle_pos + = paddle_vel. Note that the position of the bezel is updated only when the bezel is inside the canvas.

(5) at this time, you can use the keyboard "→" key and "←" key to control the movement of the bezel, add code to the draw () function to detect the collision between the ball and the bezel, if the collision occurs, the ball bounces, otherwise the ball flies out of the canvas, the player loses a health value, and the ball is sent out again from the center of the canvas.

Reference code #-coding: utf-8-*-import simpleguitk as simpleguiimport random# initialization global variable WIDTH = 500 # canvas width HEIGHT = 500 # canvas height PADDLE_WIDTH = 50 # bezel width PADDLE_HEIGHT = 8 # bezel height paddle_pos = WIDTH / 2 # bezel initial position paddle_vel = 0 # bezel initial speed HALF_PADDLE_WIDTH = PADDLE_WIDTH / 2HALF_PADDLE_HEIGHT = PADDLE_HEIGHT / 2BALL_RADIUS = 8 # squash radius ball_pos = [WIDTH / 2 HEIGHT / 2] # initial position of squash ball_vel = [0,0] # initial speed of squash # serve def spawn_ball (): global ball_pos, ball_vel # the position and speed of squash are represented by a list containing two elements, respectively And declared as a global variable # step 2 code is written below ball_vel [0] = 1 ball_vel [1] = 1 ball_pos [0] = WIDTH / 2 ball_pos [1] = HEIGHT / 2def new_game (): global score, live # score and global paddle_pos Position and speed of paddle_vel # bezel score = 0 live = 3 paddle_pos = WIDTH / 2 paddle_vel = 0 spawn_ball () def draw (canvas): global score, live, paddle_pos, ball_pos, ball_vel # code is written below # step 1 draw the line canvas.draw_line ([0, HEIGHT/2], [WIDTH, HEIGHT/2], 3 'white') # draw horizontal lines canvas.draw_line ([WIDTH/2, HEIGHT/2], [WIDTH/2, HEIGHT], 3,' white') # draw vertical lines # step 1 draw ball canvas.draw_circle (ball_pos, BALL_RADIUS, 1, 'white' 'white') ball_pos [0] + = ball_vel [0] ball_pos [1] + = ball_vel [1] # step 1 draw bezel canvas.draw_line ([paddle_pos-HALF_PADDLE_WIDTH, HEIGHT], [paddle_pos+HALF_PADDLE_WIDTH, HEIGHT], PADDLE_HEIGHT,' white') # step 1 draw life and fraction canvas.draw_text ('life:', [15 15], 10, 'yellow') canvas.draw_text (live, [50,15], 10,' yellow') canvas.draw_text ('score:', [80,15], 10, 'yellow') canvas.draw_text (score, [120,15], 10 'yellow') # step 3 check collision with upper wall if ball_pos [1] = = BALL_RADIUS: ball_vel [1] =-ball_vel [1] # step 3 check collision with left and right wall if ball_pos [0] = = BALL_RADIUS or ball_pos [0] = = WIDTH-BALL_RADIUS: ball_vel [0] =-ball_vel [0] # step 4 more Horizontal position of the new bezel paddle_pos + = paddle_vel # step 5 check the collision with the bezel if ball_pos [1] = = HEIGHT- PADDLE_HEIGHT-BALL_RADIUS: if paddle_pos-HALF_PADDLE_WIDTH-BALL_RADIUS 0: spawn_ball () else: live = 0 # when the live is 0 and the score does not reach 50 Challenge failed if live = 0 and score

< 50: canvas.draw_text('挑战失败', [150, 200], 50, 'red') # 当分数为50分时,游戏过关 if score >

= 50: canvas.draw_text ('Challenge successful', [150,200], 50 'green') def keydown (key): global paddle_vel if key = = simplegui.KEY_MAP [' left']: paddle_vel =-4 # change the speed direction elif key = = simplegui.KEY_MAP ['right']: paddle_vel = 4def keyup (key): global paddle_vel if key = simplegui.KEY_MAP [' left'] or key = = simplegui.KEY_MAP ['right']: paddle _ vel = creating frame frame = simplegui.create_frame ("single squash" WIDTH, HEIGHT) frame.set_draw_handler (draw) frame.set_canvas_background ("brown") frame.set_keydown_handler (keydown) frame.set_keyup_handler (keyup) frame.add_button ("restart", new_game, 50) # run the framework new_game () frame.start () Thank you for reading The above is the content of "how to use Python language to achieve single squash game". After the study of this article, I believe you have a deeper understanding of how to use Python language to achieve single squash game, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report