您的位置:首页 > 编程语言 > Python开发

python方向键控制上下左右代码

2018-01-20 14:22 2827 查看

本文所示代码实现python编程方向键控制图片上下左右,我们首先看下演示结果。

演示:

实例代码:

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()

总结

我觉得游戏编程最基础的功能就是鼠标键盘控制物品移动,还有就是物体的碰撞检测。

以上就是本文关于python方向键控制上下左右代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 方向键