Python arrow keys control up and down code, python arrow keys up and down

Source: Internet
Author: User

Python arrow keys control up and down code, python arrow keys up and down

The code shown in this article implements the python programming arrow keys to control the image from top to bottom. First, let's take a look at the demo results.

Demo:

Instance code:

bif="1.jpg" mif="2.jpg" import pygame,sys from pygame.locals import *  pygame.init()  screen=pygame.display.set_mode((640,360),0,32) background=pygame.image.load(bif).convert() mouse_c=pygame.image.load(mif).convert_alpha()  x,y=0,0 movex,movey=0,0  while True:   for event in pygame.event.get():     if event.type ==QUIT:       pygame.quit()       sys.exit()     if event.type==KEYDOWN:       if event.key==K_LEFT:         movex=-1       if event.key==K_RIGHT:         movex=+1       elif event.key==K_UP:         movey=-1       elif event.key==K_DOWN:         movey=+1     if event.type==KEYUP:       if event.key==K_LEFT:         movex=0       if event.key==K_RIGHT:         movex=0       elif event.key==K_UP:         movey=0       elif event.key==K_DOWN:         movey=0    x+=movex   y+=movey      screen.blit(background,(0,0))   screen.blit(mouse_c,(x,y))      pygame.display.update() 

Summary

I think the most basic function of game programming is to control the movement of items through the mouse and keyboard, as well as the collision detection of objects.

The above is all the content of this article on the python arrow keys to control the top, bottom, and left code. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!

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.