|
2鱼币
我的按键按下去以后,key1的值没有改变啊
- import random
- import os
- import pygame, sys
- from pygame.locals import *
- class SnakeGame:
- # 游戏状态
- class GameState:
- def __init__(self):
- self.GAMESTATE = 'playing'
- #self.screen_size = USIZE * COLUMNS + 300, USIZE * ROWS # 屏幕尺寸
- self.score = 0 # 得分
- self.full_screen = False # 是否全屏
- self.speed = INITSPEED # FPS,游戏速度
- def add_score(self):
- self.score += 1
- if self.speed < 15:
- self.speed *= 1.1
- if self.score in (30, 50, 65, 75) or\
- (self.score > 75 and (self.score - 80) % 5 == 0):
- sound_manager.play_cheer_sound()
- def handle_key_event(self, event):
- if event.type == KEYDOWN:
- if event.key == K_ESCAPE:
- sys.exit()
- if event.key == K_f:
- self.screen = self.new_screen(self.gs.screen_size, full=not self.gs.full_screen)
- if self.gs.GAMESTATE == 'over' and event.key == K_RETURN:
- #print('Return press')
- self.initGame()
- if event.key == K_SPACE:
- if self.gs.GAMESTATE == 'playing':
- self.gs.GAMESTATE = 'pausing'
- pygame.mixer.music.pause()
- elif self.gs.GAMESTATE == 'pausing':
- self.gs.GAMESTATE = 'playing'
- pygame.mixer.music.unpause()
- if self.gs.GAMESTATE == 'playing' and not self.new_direction_setted:
- direction = ''
- if event.key in (K_DOWN, K_s):
- direction = 'down'
- elif event.key in (K_UP, K_w):
- direction = 'up'
- elif event.key in (K_LEFT, K_a):
- direction = 'left'
- elif event.key in (K_RIGHT, K_d):
- direction = 'right'
- if self.snake.isValidDirection(direction):
- self.snake.changeDirection(direction)
- self.new_direction_setted = True
- if event.type == QUIT:
- # pygame.quit()
- sys.exit()
复制代码
|
|