【问题标题】:key event triggers and moving objects on screen关键事件触发器和屏幕上的移动对象
【发布时间】:2016-11-19 06:41:46
【问题描述】:

所以我正在用 python 中的 pygame 模块制作游戏。游戏是Breakout。游戏的机制之一是左右移动玩家。我是如何做到这一点的,当用户按下向左或向右箭头键时,玩家砖块会根据按下的键向左或向右移动,但要注意的是,如果玩家按住向左或向右按​​钮;玩家砖不会继续移动...我的问题是如何让玩家砖在按住键时继续移动而不是移动一次?!

这是我的代码

import pygame
pygame.init()
#colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
GREEN    = (   0, 255,   0)
RED      = ( 255,   0,   0)

#the Brick
class goodbrick:
    def __init__ (self, color):
        self.color_scheme=color
        ##################X,  Y     L    F
        self.cordinates= [20, 450, 100, 0]

    def move (self, x):
            self.cordinates[0]+=x

    def draw (self):
        pygame.draw.rect(screen, self.color_scheme, self.cordinates, 0)


#class enemyBrick:
#the ball

#pygame stuff
size = (700, 500)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("BREAKOUT")
done= False
clock = pygame.time.Clock()

#init stuff
player1= goodbrick(GREEN)
#main loop
while not done:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT:
            done=True
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player1.move(-1)
            if event.key == pygame.K_RIGHT:
                player1.move(1)
        elif event.type ==pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player1.move(-1)
                print("yup")
            if event.key == pygame.K_RIGHT:
                player1.move(1)
    #art
    screen.fill(BLACK)
    player1.draw()
    #screent
    pygame.display.flip()
    clock.tick(60)

pygame.quit()

【问题讨论】:

    标签: python pygame


    【解决方案1】:
    import pygame
    pygame.init()
    #colors
    BLACK    = (   0,   0,   0)
    WHITE    = ( 255, 255, 255)
    GREEN    = (   0, 255,   0)
    RED      = ( 255,   0,   0)
    
    #the Brick
    class goodbrick:
        def __init__ (self, color):
            self.color_scheme=color
            ##################X,  Y     L    F
            self.cordinates= [20, 450, 100, 0]
    
        def move (self, x):
                self.cordinates[0]+=x
    
        def draw (self):
            pygame.draw.rect(screen, self.color_scheme, self.cordinates, 0)
    
    
    #class enemyBrick:
    #the ball
    
    #pygame stuff
    size = (700, 500)
    screen = pygame.display.set_mode(size)
    pygame.display.set_caption("BREAKOUT")
    done= False
    clock = pygame.time.Clock()
    
    #init stuff
    player1= goodbrick(GREEN)
    #main loop
    change = 0
    while not done:        
        for event in pygame.event.get(): # User did something
            if event.type == pygame.QUIT:
                done=True
            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    change = -1                
                if event.key == pygame.K_RIGHT:
                    change = 1
            elif event.type ==pygame.KEYUP:
                if event.key == pygame.K_LEFT:
                    change = 0
                    print("yup")
                if event.key == pygame.K_RIGHT:
                    change = 0
        player1.move(change)
        #art
        screen.fill(BLACK)
        player1.draw()
        #screent
        pygame.display.flip()
        clock.tick(60)
    
    pygame.quit()
    

    【讨论】:

    • elif event.type ==pygame.KEYUP: if event.key == pygame.K_LEFT: change = 0 print("yup") if event.key == pygame.K_RIGHT: change = 0
    • 我做了我所理解的,来自问题陈述。我已根据您的评论更正了我的答案
    • 非常感谢
    猜你喜欢
    • 2017-07-21
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-29
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多