• Matthew
    2022-12-14 来自江苏
    from pynput.keyboard import Key, Listener, KeyCode # 函数:显示位置 def show_location(loc): for a in loc: print(a) background = [[0, 0, 0], [0, "x", 0], [0, 0, 0]] # 调用 显示位置 函数 show_location(background) # 函数:响应按键 def on_press(key): if key == KeyCode.from_char('w'): for i in 0,1,2: for j in 0,1,2: if background[i][j] == "x" and i >0 : background[i][j] = "0" background[i-1][j] = "x" elif key == KeyCode.from_char('s'): for i in 2,1,0: for j in 0,1,2: if background[i][j] == "x" and i < 2 : background[i][j] = "0" background[i+1][j] = "x" elif key == KeyCode.from_char('a'): for i in 0,1,2: for j in 0,1,2: if background[i][j] == "x" and j > 0 : background[i][j] = "0" background[i][j-1] = "x" break elif key == KeyCode.from_char('d'): for i in 0,1,2: for j in 0,1,2: if background[i][j] == "x" and j < 2 : background[i][j] = "0" background[i][j+1] = "x" break print() # 调用 显示位置 函数 show_location(background) with Listener(on_press=on_press) as listener: listener.join()
    展开
    
    3
  • yanyu-xin
    2023-01-30 来自广东
    from pynput.keyboard import Key, Listener, KeyCode # 位置列表,全局变量 background = [['0', '0', '0'], ['0', 'x', '0'], ['0', '0', '0']] # 打印列表,显示位置信息的函数 def show_background(): print(background[0]) print(background[1]) print(background[2]) print() return # 显示初始位置 show_background() # 按键动作函数 def on_press(key): # 按w键,循环找到位置,修改相应位置信息 if key == KeyCode.from_char('w'): for i in 0,1,2: for j in 0,1,2: if background[i][j] == "x" and i >0 : background[i][j] = "0" background[i-1][j] = "x" # 按s键,循环找到位置,修改相应位置信息 elif key == KeyCode.from_char('s'): for i in 2,1,0: for j in 0,1,2: if background[i][j] == "x" and i < 2 : background[i][j] = "0" background[i+1][j] = "x" # 按a键,循环找到位置,修改相应位置信息 elif key == KeyCode.from_char('a'): for i in 0,1,2: for j in 0,1,2: if background[i][j] == "x" and j > 0 : background[i][j] = "0" background[i][j-1] = "x" break # 按d键,循环找到位置,修改相应位置信息 elif key == KeyCode.from_char('d'): for i in 0,1,2: for j in 0,1,2: if background[i][j] == "x" and j < 2 : background[i][j] = "0" background[i][j+1] = "x" break # 增加按q键,退出程序 elif key == KeyCode.from_char('q'): exit(-1) # 按键处理后,显示新位置 show_background() # 调用按键处理函数库 with Listener(on_press=on_press) as listener: listener.join()
    展开
    
    2
  • yanyu-xin
    2023-01-31 来自广东
    from pynput.keyboard import Key, Listener, KeyCode # 用x , y 变量作为位置参数 # 打印位置,显示位置信息的函数 def show_background(x, y) : print(x,y) for y1 in range(5) : for x1 in range(5) : if (x1 == x and y1 == y) : print('x',end=' ') else : print('0',end=' ') print() return # 调整x y的限制范围,控制在限制范围内 def limit(x, y) : if x > 4: x = 4 if x < 0: x = 0 if y > 4: y = 4 if y < 0: y = 0 return x, y # 按键动作函数 def on_press(key): global x_loca, y_loca # 按w键,循环找到位置,修改相应位置信息 if key == KeyCode.from_char('w'): y_loca -= 1 # 按s键,循环找到位置,修改相应位置信息 elif key == KeyCode.from_char('s'): y_loca += 1 # 按a键,循环找到位置,修改相应位置信息 elif key == KeyCode.from_char('a'): x_loca -= 1 # 按d键,循环找到位置,修改相应位置信息 elif key == KeyCode.from_char('d'): x_loca += 1 # 增加按q键,退出程序 elif key == KeyCode.from_char('q'): exit(-1) # 调整x y的限制范围,控制在限制范围内 x_loca, y_loca = limit(x_loca, y_loca) # 按新坐标显示新位置 show_background(x_loca, y_loca) # 位置列表,全局变量.x,y表示,0,0 为第一行第一列,范围为5 x 5 x_loca , y_loca = 1 , 1 # 显示初始位置 show_background(x_loca, y_loca) # 调用按键处理函数库 with Listener(on_press=on_press) as listener: listener.join()
    展开
    
    1
  • 不再是入门水平pytho...
    2023-09-02 来自上海
    # 定义输出飞机位置的函数 def print_background(): for site in background: print(site) print(f"health:{health}")
    
    
  • acondess
    2023-08-31 来自浙江
    from pynput.keyboard import Key,KeyCode,Listener # 生成地图 background = [ [0, 0, 0], [0,"x", 0], [0, 0, 0] ] ##定义地图显示函数 def show_map(): for i in background: print(i) def on_press(key): if key == KeyCode.from_char('w'): for i in 0,1,2: for j in 0,1,2: if background[i][j]=='x' and i>0: background[i][j] = '0' background[i-1][j] = 'x' break elif key == KeyCode.from_char('s'): #注意此处如果i从0,1,2顺序判断时,x字符在第0行,执行了一次移动,此时x在第1行, #再判断时,又会从第1行移动到第2行 for i in 2,1,0: for j in 0,1,2: if background[i][j]=='x' and i<2: background[i][j] = '0' background[i+1][j] = 'x' break elif key == KeyCode.from_char('a'): for i in 0,1,2: for j in 0,1,2: if background[i][j]=='x' and j>0: background[i][j] = '0' background[i][j-1] = 'x' break elif key == KeyCode.from_char('d'): for i in 0,1,2: for j in 0,1,2: if background[i][j]=='x' and j<2: background[i][j] = '0' background[i][j+1] = 'x' break print() #调用地图显示 show_map() # for i in background: # print(i) # 调用地图显示 show_map() with Listener(on_press = on_press) as listener: listener.join()
    展开
    
    
  • 朱雯
    2023-01-30 来自北京
    from pynput.keyboard import Key,Listener,KeyCode import random background = [[0,0,0],[0,"x",0],[0,0,0]] # for i in background: # print(i) health = 100 print(health) def on_press(key): global health if key == KeyCode.from_char("w"): movehead(background) if key == KeyCode.from_char("s"): moveback(background) if key == KeyCode.from_char("a"): moveleft(background) if key == KeyCode.from_char("d"): moveright(background) if key == KeyCode.from_char("="): health = addhealth(health) if key == KeyCode.from_char("-"): health = subhealth(health) print(health) def moveleft(background): for i in range(0,3): for j in range(0,3): if background[i][j] == 'x' and j > 0: background[i][j] = 0 background[i][j-1] = 'x' def moveright(background): for i in range(0,3): for j in 2,1,0: if background[i][j] == 'x' and j < 2: background[i][j] = 0 background[i][j+1] = 'x' print(i,j) def movehead(background): for i in range(0,3): for j in range(0,3): if background[i][j] == 'x' and i > 0: background[i][j] = 0 background[i-1][j] = 'x' break def moveback(background): for i in 2,1,0: for j in range(0,3): if background[i][j] == 'x' and i < 2: background[i][j] = 0 background[i+1][j] = 'x' def addhealth(health): if health < 100: health += random.randint(0,100) if health > 100: health = 100 return health def subhealth(health): if health > 0: health -= random.randint(0,100) if health <= 0: health = 0 exit(-1) return health with Listener(on_press=on_press) as listner: listner.join()
    展开
    
    