乒乓球拍对象的平滑Keypressed事件

时间:2017-11-29 20:03:47

标签: python events pygame

我正试图在我的乒乓球比赛中使我的球拍运动更顺畅。我的player_paddle1运动平稳,每当我松开钥匙时都会停止。但是,我的另一个player_paddle2,包含相同的按键算法,另一个paddle,但没有这样做。即使我发布密钥,它也会继续运行。

var dataTable = new gapi.analytics.googleCharts.DataChart({
  query: {
    'ids': 'ga:1234',
    'dimensions': 'ga:country',
    'metrics': 'ga:sessions',
    'start-date': '30daysAgo',
    'end-date': 'yesterday',
  },
  chart: {
    type: 'TABLE',
    container: 'data-table'
  }
});

此外,虽然上下键响应非常灵敏,但W和S键的反应不太敏感,这意味着按键不会立即导致桨板运动。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您在K_UP中检查KEYUP第二位玩家,但必须是K_wK_s

除此之外,您还不必查看player_paddle2.direction

        if event.type == KEYUP:
            if event.key == K_UP:
                player_paddle1.direction = 0
            elif event.key == K_DOWN:
                player_paddle1.direction = 0

            elif event.key == K_w: # <-- there was K_UP
                print("The key is now up!")
                player_paddle2.direction = 0 
            elif event.key == K_s: # <-- there was K_UP
                player_paddle2.direction = 0

它可以更短

        elif event.type == KEYUP:
            if event.key in (K_UP, K_DOWN)
                player_paddle1.direction = 0

            elif event.key in (K_w, K_s):
                player_paddle2.direction = 0 

顺便说一句:你可以在某些地方使用elif