鱼C论坛

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

[已解决]能看看我这个按键检测为啥无效吗?

[复制链接]
发表于 2020-1-1 18:05:47 | 显示全部楼层 |阅读模式
2鱼币
我的按键按下去以后,key1的值没有改变啊
  1. import random
  2. import os
  3. import pygame, sys
  4. from pygame.locals import *

  5. class SnakeGame:

  6.     # 游戏状态
  7.     class GameState:
  8.         def __init__(self):
  9.             self.GAMESTATE = 'playing'
  10.             #self.screen_size = USIZE * COLUMNS + 300, USIZE * ROWS  # 屏幕尺寸
  11.             self.score = 0  # 得分
  12.             self.full_screen = False  # 是否全屏
  13.             self.speed = INITSPEED # FPS,游戏速度

  14.         def add_score(self):
  15.             self.score += 1
  16.             if self.speed < 15:
  17.                 self.speed *= 1.1
  18.             if self.score in (30, 50, 65, 75) or\
  19.                 (self.score > 75 and (self.score - 80) % 5 == 0):
  20.                 sound_manager.play_cheer_sound()


  21.     def handle_key_event(self, event):
  22.         if event.type == KEYDOWN:
  23.             if event.key == K_ESCAPE:
  24.                 sys.exit()
  25.             if event.key == K_f:
  26.                 self.screen = self.new_screen(self.gs.screen_size, full=not self.gs.full_screen)

  27.             if self.gs.GAMESTATE == 'over' and event.key == K_RETURN:
  28.                 #print('Return press')
  29.                 self.initGame()
  30.             if event.key == K_SPACE:
  31.                 if self.gs.GAMESTATE == 'playing':
  32.                     self.gs.GAMESTATE = 'pausing'
  33.                     pygame.mixer.music.pause()
  34.                 elif self.gs.GAMESTATE == 'pausing':
  35.                     self.gs.GAMESTATE = 'playing'
  36.                     pygame.mixer.music.unpause()
  37.             if self.gs.GAMESTATE == 'playing' and not self.new_direction_setted:
  38.                 direction = ''
  39.                 if event.key in (K_DOWN, K_s):
  40.                     direction = 'down'
  41.                 elif event.key in (K_UP, K_w):
  42.                     direction = 'up'
  43.                 elif event.key in (K_LEFT, K_a):
  44.                     direction = 'left'
  45.                 elif event.key in (K_RIGHT, K_d):
  46.                     direction = 'right'
  47.                 if self.snake.isValidDirection(direction):
  48.                     self.snake.changeDirection(direction)
  49.                     self.new_direction_setted = True
  50.         if event.type == QUIT:
  51.             #  pygame.quit()
  52.             sys.exit()
复制代码
最佳答案
2020-1-1 18:05:48
你只定义了函数,并没有用啊

最佳答案

查看完整内容

你只定义了函数,并没有用啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-1-1 18:05:48 | 显示全部楼层    本楼为最佳答案   
你只定义了函数,并没有用啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2020-2-1 17:11:46 | 显示全部楼层
你这个类里面的函数都没调用,怎么判断你有没有按下按键?
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-4-25 20:55

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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