Python 2.7 Pygame赢了';t使用两个键进行对角输入

Python 2.7 Pygame赢了';t使用两个键进行对角输入,python-2.7,graphics,pygame,sprite,Python 2.7,Graphics,Pygame,Sprite,我有一段代码,背景中有一束星星,可以自动移动,用户可以通过按箭头键来设定方向。上、下、左、右都很好,但是,当我测试“垂直”方向时,我无法让它移动 到目前为止,我已经尝试过: while running == True: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False elif event.type == pygame.KEY

我有一段代码,背景中有一束星星,可以自动移动,用户可以通过按箭头键来设定方向。上、下、左、右都很好,但是,当我测试“垂直”方向时,我无法让它移动

到目前为止,我已经尝试过:

while running == True:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif event.type == pygame.KEYDOWN:
            #cardinal directions
                if event.key ==pygame.K_LEFT:
                    starDirection = "left"

                elif event.key == pygame.K_RIGHT:
                    starDirection = "right"


                elif event.key ==pygame.K_UP:
                    starDirection = "up"


                elif event.key ==pygame.K_DOWN:
                    starDirection ="down"

                elif event.key == pygame.K_UP and event.key == pygame.K_DOWN:
                    starDirection ="upright"




    for a_star in star_list:
        if starDirection == "right":
            a_star.rect.x += 5
            if a_star.rect.x > width:
                a_star.rect.x = 0
                a_star.rect.y = random.randint(0,height)

        elif starDirection == "left":
            a_star.rect.x -=5
            if a_star.rect.x < 0:
                a_star.rect.x = width
                a_star.rect.y = random.randint(0,height)

        elif starDirection == "up":
            a_star.rect.y -= 5
            if a_star.rect.y < 0:
                a_star.rect.x = random.randint(0,width)
                a_star.rect.y = height

        elif starDirection == "down":
            a_star.rect.y += 5
            if a_star.rect.y > height:
                a_star.rect.x = random.randint(0,width)
                a_star.rect.y = 0

        #diagonal directions
        elif starDirection == "upright":
            a_star.rect.y-=5
            a_star.rect.x+=5
            if a_star.rect.y < 0:
                a_star.rect.x = random.randint(0,width)
                a_star.rect.y = height

            elif a_star.rect.x > width:
                a_star.rect.x = 0
                a_star.rect.y = random.randint(0,height)

        elif starDirection == "upleft":

            a_star.rect.y-=5
            a_star.rect.x-=5
            if a_star.rect.y < 0:
                a_star.rect.x = random.randint(0,width)
                a_star.rect.y = height

            elif a_star.rect.x < 0:
                a_star.rect.x = width
                a_star.rect.y = random.randint(0,height)

        elif starDirection == "downright":

            a_star.rect.y += 5
            a_star.rect.x+=5
            if a_star.rect.y > height:
                a_star.rect.x = random.randint(0,width)
                a_star.rect.y = 0

            elif a_star.rect.x > width:
                a_star.rect.x = 0
                a_star.rect.y = random.randint(0,height)



        elif starDirection == "downleft":
            a_star.rect.y += 5
            a_star.rect.x-=5
            if a_star.rect.y > height:
                    a_star.rect.x = random.randint(0,width)
                    a_star.rect.y = 0

            elif a_star.rect.x < 0:
                    a_star.rect.x = width
                    a_star.rect.y = random.randint(0,height)
运行时==True:
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
运行=错误
elif event.type==pygame.KEYDOWN:
#基本方向
如果event.key==pygame.K_左:
starDirection=“左”
elif event.key==pygame.K_RIGHT:
starDirection=“右”
elif event.key==pygame.K_UP:
starDirection=“向上”
elif event.key==pygame.K_向下:
starDirection=“向下”
elif event.key==pygame.K_向上和event.key==pygame.K_向下:
starDirection=“直立”
对于星号列表中的星号:
如果starDirection==“right”:
a_星.rect.x+=5
如果a_star.rect.x>宽度:
a_星.rect.x=0
a_star.rect.y=random.randint(0,高度)
elif starDirection==“左”:
a_星矩形x-=5
如果a_星矩形x<0:
a_star.rect.x=宽度
a_star.rect.y=random.randint(0,高度)
elif starDirection==“向上”:
a_星。矩形y-=5
如果a_星矩形y<0:
a_star.rect.x=random.randint(0,宽度)
a_star.rect.y=高度
elif starDirection==“向下”:
a_星.矩形y+=5
如果a_star.rect.y>高度:
a_star.rect.x=random.randint(0,宽度)
a_star.rect.y=0
#对角线方向
elif starDirection==“直立”:
a_星。矩形y-=5
a_星.rect.x+=5
如果a_星矩形y<0:
a_star.rect.x=random.randint(0,宽度)
a_star.rect.y=高度
elif a_star.rect.x>宽度:
a_星.rect.x=0
a_star.rect.y=random.randint(0,高度)
elif starDirection==“upleft”:
a_星。矩形y-=5
a_星矩形x-=5
如果a_星矩形y<0:
a_star.rect.x=random.randint(0,宽度)
a_star.rect.y=高度
elif a_star.rect.x<0:
a_star.rect.x=宽度
a_star.rect.y=random.randint(0,高度)
elif starDirection==“完全正确”:
a_星.矩形y+=5
a_星.rect.x+=5
如果a_star.rect.y>高度:
a_star.rect.x=random.randint(0,宽度)
a_star.rect.y=0
elif a_star.rect.x>宽度:
a_星.rect.x=0
a_star.rect.y=random.randint(0,高度)
elif starDirection==“左下”:
a_星.矩形y+=5
a_星矩形x-=5
如果a_star.rect.y>高度:
a_star.rect.x=random.randint(0,宽度)
a_star.rect.y=0
elif a_star.rect.x<0:
a_star.rect.x=宽度
a_star.rect.y=random.randint(0,高度)

事件键不可能同时是两个不同的东西。您需要通过处理
KEYDOWN
keydup
事件来存储密钥状态

最简单的方法是为上、下、左、右键的每个键都有一个布尔变量。在
KEYDOWN
上设置它们,并在
keydup
上清除它们


然后,您可以测试类似于
如果leftIsPressed和upIsPressed

而不是查找
KEYDOWN
事件,只需使用
pygame.key.get\u pressed()查询所有按下的键即可。这样,很明显很容易看到此时按下了哪些键

此外,在移动星星之前,应该使用移动向量并对其进行规格化。否则,你的恒星以
5的速度水平/垂直移动,以
5的速度对角移动√(5²+5²)=7,07

下面是一个完整的示例:

import pygame
import math

from random import randint
from functools import partial

pygame.init()
width, height = (800, 600)
screen = pygame.display.set_mode((width, height))
clock = pygame.time.Clock()
rand_x = lambda: randint(0, width)
rand_y = lambda: randint(0, height)

def magnitude(v):
    """returns the lenght of a vector"""
    return math.sqrt(sum(v[i]*v[i] for i in range(len(v))))

def normalize(v):
    """normalizes a vector"""
    vmag = magnitude(v)
    return [ v[i]/vmag  for i in range(len(v)) ]


class Star(object):

    def __init__(self):
        self.rect = pygame.Rect(rand_x(), rand_y(), 1, 1)
        self.speed = randint(2, 5)

    def move(self, vec):
        if vec == [0, 0]: 
            return

        # move star by applying its speed 
        # to the normalized movement vector
        self.rect.move_ip(*[self.speed * a for a in normalize(vec)])

        # check if the star needs to appear on the opposite edge
        if self.rect.x > width:  
            self.rect.x = 0
            self.rect.y = rand_y()
        elif self.rect.x < 0:    
            self.rect.x = width
            self.rect.y = rand_y()
        if self.rect.y > height:    
            self.rect.y = 0
            self.rect.x = rand_x()
        elif self.rect.y < 0:    
            self.rect.y = height
            self.rect.x = rand_x()

    def draw(self, surface):
        pygame.draw.circle(surface, pygame.Color('white'), self.rect.center, 2)


def main():        
    # create a bunch of stars
    stars = [Star() for _ in xrange(100)]

    # a map of possible movements
    move = {pygame.K_UP: (0, -1),
            pygame.K_DOWN: (0, 1),
            pygame.K_LEFT: (-1, 0),
            pygame.K_RIGHT: (1, 0)}

    quit = False
    while not quit:
        # draw everything
        screen.fill(pygame.Color('black'))    
        for star in stars:
           star.draw(screen)
        pygame.display.flip()

        # check for pressed keys
        keys = pygame.key.get_pressed()

        # create a movement vector by adding all vectors 
        # whose key (according to 'move') is pressed
        vec = map(sum, zip(*[v for (k, v) in move.iteritems() if keys[k]])) or [0, 0]

        # move all stars
        for star in stars:
            star.move(vec)

        # check for exit
        quit = pygame.event.get(pygame.QUIT)
        pygame.event.poll()

        # limit FPS
        clock.tick(60)

if __name__ == '__main__':
    main()
导入pygame
输入数学
从随机导入randint
从functools导入部分
pygame.init()
宽度,高度=(800600)
screen=pygame.display.set_模式((宽度、高度))
clock=pygame.time.clock()
rand_x=lambda:randint(0,宽度)
rand_y=lambda:randint(0,高度)
def量级(v):
“”“返回向量的长度”“”
返回math.sqrt(范围(len(v))中i的和(v[i]*v[i]))
def正常化(v):
“”“规范化向量”“”
vmag=震级(v)
返回[v[i]/vmag(len(v))]
类星号(对象):
定义初始化(自):
self.rect=pygame.rect(rand_x(),rand_y(),1,1)
self.speed=randint(2,5)
def移动(自身、vec):
如果vec==[0,0]:
返回
#用它的速度移动星星
#到归一化的运动矢量
self.rect.move_ip(*[self.speed*a表示正常化(vec)])
#检查星星是否需要出现在对边上
如果self.rect.x>宽度:
self.rect.x=0
self.rect.y=rand_y()
elif self.rect.x<0:
self.rect.x=宽度
self.rect.y=rand_y()
如果自校正y>高度:
自校正y=0
self.rect.x=rand_x()
elif self.rect.y<0:
自校正y=高度
self.rect.x=rand_x()
def绘图(自、表面):
pygame.draw.circle(曲面,pygame.Color('white'),self.rect.center,2)
def main():
#创造一堆星星
stars=[Star()表示x范围内的uu(100)]
#可能移动的地图
move={pygame.K_UP:(0,-1),
pygame.K_向下:(0,1),
pygame.K_左:(-1,0),
P