鱼C论坛

 找回密码
 立即注册
查看: 4160|回复: 3

[已解决]Python中pygame没有KEYDOWN,运行出现NameError: name 'KEYDOWM' is not defined

[复制链接]
发表于 2018-1-14 10:46:25 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 838262020 于 2018-1-14 10:47 编辑

import pygame
import sys
from  pygame.locals import *
#初始化Pygame
pygame.init()
size = width,height = 600,800
speed = [-2,1]
bg = (255,255,255)#RGB

fullscreen = False

#创建指定大小的窗口
screen = pygame.display.set_mode(size)
#设置窗口标题
pygame.display.set_caption('初次见面,请大家多多关注!')

#加载图片
turtle = pygame.image.load('D:\\longma.jpg')
#获得图像的位置矩形pn
position = turtle.get_rect()

while True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        if event.type==KEYDOWM:
            if event.key == K_LEFT:
                speed = [-1,0]
            if event.key == K_RIGHT:
                speed =[1,0]
            if event.key == K_UP:
                speed=[0,-1]
            if event.key == K_DOWN:
                speed =[0,1]

    #移动图像
    position = position.move(speed)

    if position.left<0 or position.right>width:
        #翻转图像
        turtle=pygame.transform.flip(turtle,True,False)
        #反方向移动
        speed[0]=-speed[0]

    if position.top<0 or position.bottom>height:
        speed[1] = -speed[1]

    #填充背景
    screen.fill(bg)
    #更新图像
    screen.blit(turtle,position)
    #更新界面
    pygame.display.flip()
    #延迟10毫秒
    pygame.time.delay(10)
最佳答案
2018-1-14 10:50:33
你写错了,是KEYDOWN,最后一个字母不对。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-1-14 10:50:33 | 显示全部楼层    本楼为最佳答案   
你写错了,是KEYDOWN,最后一个字母不对。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-14 11:06:25 | 显示全部楼层
2018-01-14_110415.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-1-14 11:47:36 | 显示全部楼层
  1. import pygame
  2. import sys
  3. from  pygame.locals import *
  4. #初始化Pygame
  5. pygame.init()
  6. size = width,height = 600,800
  7. speed = [-2,1]
  8. bg = (255,255,255)#RGB

  9. fullscreen = False

  10. #创建指定大小的窗口
  11. screen = pygame.display.set_mode(size)
  12. #设置窗口标题
  13. pygame.display.set_caption('初次见面,请大家多多关注!')

  14. #加载图片
  15. turtle = pygame.image.load('D:\\longma.jpg')
  16. #获得图像的位置矩形pn
  17. position = turtle.get_rect()

  18. while True:
  19.     for event in pygame.event.get():
  20.         if event.type == pygame.QUIT:
  21.             sys.exit()
  22.         if event.type==KEYDOWN:
  23.             if event.key == K_LEFT:
  24.                 speed = [-1,0]
  25.             if event.key == K_RIGHT:
  26.                 speed =[1,0]
  27.             if event.key == K_UP:
  28.                 speed=[0,-1]
  29.             if event.key == K_DOWN:
  30.                 speed =[0,1]

  31.     #移动图像
  32.     position = position.move(speed)

  33.     if position.left<0 or position.right>width:
  34.         #翻转图像
  35.         turtle=pygame.transform.flip(turtle,True,False)
  36.         #反方向移动
  37.         speed[0]=-speed[0]

  38.     if position.top<0 or position.bottom>height:
  39.         speed[1] = -speed[1]

  40.     #填充背景
  41.     screen.fill(bg)
  42.     #更新图像
  43.     screen.blit(turtle,position)
  44.     #更新界面
  45.     pygame.display.flip()
  46.     #延迟10毫秒
  47.     pygame.time.delay(10)
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-20 14:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表