Python: pygame game programming tour 3 (player controlled ball)

Source: Internet
Author: User

In the previous section, the ball is moved freely. In this section, the arrow keys are used to control the ball movement and background images are added to the game.

I. Implementation:


[Python]
#-*-Coding: UTF-8 -*-
 
Import OS
Import sys
 
Import pygame
From pygame. locals import *
 
 
Def load_image (pic_name ):
'''''
Function: Image Loading Function
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Obtain the absolute path of the directory where the current script file is located
Current_dir = OS. path. split (OS. path. abspath (_ file _) [0]

# Specify the image directory
Path = OS. path. join (current_dir, 'image', pic_name)

# Loading Images
Return pygame. image. load (path). convert ()

Def control_ball (event ):
'''''
Function: Controls ball movements.
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
# Relative offset coordinates
Speed = [x, y] = [0, 0]

# Speed
Speed_offset = 1

# When the direction key is pressed, the position is calculated.
If event. type = pygame. KEYDOWN:
If event. key = pygame. K_LEFT:
Speed [0]-= speed_offset
If event. key = pygame. K_RIGHT:
Speed [0] = speed_offset
If event. key = pygame. K_UP:
Speed [1]-= speed_offset
If event. key = pygame. K_DOWN:
Speed [1] = speed_offset

# When the direction keys are released, the relative offset is 0, that is, they are not moved.
If event. type in (pygame. KEYUP, pygame. K_LEFT, pygame. K_RIGHT, pygame. K_DOWN ):
Speed = [0, 0]

Return speed

Def play_ball ():
'''''
Function: Main Function
Input: NONE
Output: NONE
Author: socrates
Blog: http://blog.csdn.net/dyx1024
Date: 2012-04-15
'''
Pygame. init ()

# Window size
Window_size = Rect (0, 0,700,500)

# Set window mode
Screen = pygame. display. set_mode (window_size.size)

# Set the window title
Pygame. display. set_caption ('moving ball (2)-control the ball moving through the arrow key ')

# Loading small ball Images
Ball_image = load_image('ball.gif ')

# Loading window background images
Back_image = load_image('back_image.jpg ')

# Obtain the opening area of a small ball Image
Ball_rect = ball_image.get_rect ()

While True:

# Exit event processing
For event in pygame. event. get ():
If event. type = pygame. QUIT:
Pygame. quit ()
Sys. exit ()

# Make the ball move, and the speed is controlled by the speed variable
Cur_speed = control_ball (event)

# The clamp method of the Rect uses a moving range within the window.
Ball_rect = ball_rect.move (cur_speed). clamp (window_size)

# Set the window background www.2cto.com
Screen. Bits (back_image, (0, 0 ))

# Draw a ball on the background Surface
Screen. Bits (ball_image, ball_rect)

# Update window content
Pygame. display. flip ()

If _ name _ = '_ main __':
Play_ball ()


Ii. Test
1. Start running. The ball is located at the coordinates of the window (0, 0.

2. Press the right arrow key and move the ball to the right.

3. Press the downward arrow key and move the ball downward.

 


From Socrates Column

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.