Pygame event-control object (airplane) movement and pygame event

Source: Internet
Author: User

Pygame event-control object (airplane) movement and pygame event

Recently, I want to use pygame as a game. I learned this thing from xishui's eye-catching blog. I just wrote my own code for the plane war, it mainly optimizes the keyboard control plane movement.

#-*-Coding: UTF-8-*-import pygamefrom pygame. locals import * import sysimport timepygame. init () screen_width = 640screen_height = 480 # Set the size of the game window screen = pygame. display. set_mode (screen_width, screen_height), 0, 32) pygame. display. set_caption ('aircraft wars ') image = 'feiji.png' mv _ image = pygame. image. load (image) # obtain the aircraft length mv_image_width = mv_image.get_width () # obtain the aircraft width mv_image_height = mv_image.get_height () # The starting position of the aircraft, which should be in the middle of x below, y = screen_width/2-mv_image_width/2, screen_height-mv_image_height

# Mark 1
# The moving offset of an airplane. Set move_a, move_d, move_w, move_s = 0, 0, 0, 0 while True: for event in pygame. event. get (): if event. type = QUIT: sys. exit () elif event. type = KEYDOWN: if event. key = K_a or event. key = K_LEFT: move_a = 1 elif event. key = K_d or event. key = K_RIGHT: move_d = 1 elif event. key = K_UP or event. key = K_w: move_w = 1 elif event. key = K_DOWN or event. key = K_s: move_s = 1 elif event. type = KEYUP: # The keyboard in a certain direction is released, and the offset in this direction is 0 if event. key = K_a or event. key = K_LEFT: move_a = 0 elif event. key = K_d or event. key = K_RIGHT: move_d = 0 elif event. key = K_UP or event. key = K_w: move_w = 0 elif event. key = K_DOWN or event. key = K_s: move_s = 0 # When you press the keyboard, the moving offset of the plane will change to 1, and the position of the plane will be added or subtracted accordingly, refresh the display to control the movement of the aircraft # If the keyboard remains unchanged, the corresponding movement offset of the aircraft will remain 1, the position of the plane will change with the while LOOP # position after the plane moves x = x + move_d-move_a y = y + move_s-move_w
# MARK 2
# Control aircraft range, not beyond the screen, but also take into account the bullets should be able to launch to any place # Left and Right boundary aircraft should be able to enter half if x> screen_width-mv_image_width/2: x = screen_width-mv_image_width/2 elif x <0-mv_image_width/2: x = 0-mv_image_width/2 # The bottom boundary can be seen if y> screen_height-mv_image_height/5: y = screen_height-mv_image_height/5 # the upper boundary does not allow the plane to enter elif y <0: y = 0 # Fill the background color in RGB form screen. fill (255,255,255) # Draw a screen in a new position. BITs (mv_image, (x, y) pygame. display. update () # reduce the CPU burden. After testing, the value is the most appropriate time. sleep (0.001)

Here, the offset of an airplane is set to four instead of two because if two planes are set to control the x and Y axes, the plane can only control the x or Y axes,

For example, when you pressAWhen the key is used, the plane moves to the left. At this time, the plane will stop moving to the left and move to the right without releasing the key. This is obviously incorrect, our goal should be to stop moving the plane while holding down the key and d key at the same time,

Then, the d key is released, but the plane does not move at this time, but we press the key. Shouldn't he move to the left?

Therefore, only when four offsets are used to control the movement of an airplane can the aircraft move smoothly without affecting each other.

The following code is used when an airplane uses two offsets:

 
# Mark 1
move_x, move_y = 0, 0
While True: for event in pygame. event. get (): if event. type = QUIT: exit () if event. type = KEYDOWN: if event. key = K_a or event. key = K_LEFT: move_x =-1 elif event. key = K_d or event. key = K_RIGHT: move_x = 1 elif event. key = K_UP or event. key = K_w: move_y =-1 elif event. key = K_DOWN or event. key = K_s: move_y = 1 elif event. type = KEYUP: # If you open the keyboard, do not move the image move_x = 0 move_y = 0 # Calculate the new coordinate x + = move_x y + = move_y
# MARK 2

The original image feiji.png can be painted manually.

If you want to make a cool effect (it is not really cool), for example, when the game starts, the plane does not appear directly on the screen, but slowly moves from the bottom to the start position.

The Code is as follows:

# When the game starts, the plane slowly reaches the battlefield y1 = screen_heightwhile True: if y1 <= y: break screen. fill (255,255,255) screen. BITs (mv_image, (x, y1) pygame. display. update () y1-= 1 # Every 0.01 seconds. sleep (0.01)

Insert this code# Mark 1Before

 

Related Article

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.