本文所示代码实现python编程方向键控制图片上下左右,我们首先看下演示结果。
演示:
实例代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
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方向键控制上下左右代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!
原文链接:http://blog.csdn.net/zjb574/article/details/7017597