编程Pygame,以便我可以一次按多个键,使我的角色对角线移动 [英] Programming Pygame so that I can press multiple keys at once to get my character to move diagonally
本文介绍了编程Pygame,以便我可以一次按多个键,使我的角色对角线移动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
1。 #Event处理
2.对于pygame.event.get()中的事件:
3.如果event.type == pygame.QUIT:
4. sys.exit()
5. elif(event.type == KEYDOWN):
6. if((event.key == K_ESCAPE)
7.或(event.key == K_q)):
8. sys.exit()
9. if(event.key == K_UP):
10. self.char_y = self.char_y - 10
11. if(event.key = = K_DOWN):
12. self.char_y = self.char_y + 10
13. if(event.key == K_RIGHT):
14. self.char_x = self.char_x + 10
15. if(event.key == K_LEFT):
16. self.char_x = self.char_x - 10
提前感谢。
解决方案
您可以通过 pygame.key.get_pressed():
keys = pygame。 key.get_pressed()
如果键[K_LEFT]:
self.char_x + = 10
如果键[K_RIGHT]:
self.char_x - = 10
如果键[K_UP]:
self.char_y - = 10
如果键[K_DOWN]:
self.char_y + = 10
I'm trying to program with Python. I'm pretty new to Python and computer programming in general. I want to be able to design brillant games and such. I plan to later learn C + +, but right now I'm just going to stick with python. So the problem I'm having is that I'm trying to make my character move diagonally on screen when a user presses either the K_UP key and K_RIGHT key or the K_UP key and K_DOWN key, etc. Here is my code for character movement (event handling):
1. #Event Handling
2. for event in pygame.event.get():
3. if event.type == pygame.QUIT:
4. sys.exit()
5. elif (event.type == KEYDOWN):
6. if ((event.key == K_ESCAPE)
7. or (event.key == K_q)):
8. sys.exit()
9. if (event.key == K_UP):
10. self.char_y = self.char_y - 10
11. if (event.key == K_DOWN):
12. self.char_y = self.char_y + 10
13. if (event.key == K_RIGHT):
14. self.char_x = self.char_x + 10
15. if (event.key == K_LEFT):
16. self.char_x = self.char_x - 10
Thanks in advance.
解决方案
You can do it via pygame.key.get_pressed():
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.char_x += 10
if keys[K_RIGHT]:
self.char_x -= 10
if keys[K_UP]:
self.char_y -= 10
if keys[K_DOWN]:
self.char_y += 10
这篇关于编程Pygame,以便我可以一次按多个键,使我的角色对角线移动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文