Python 如何在pygame程序中移动对象?

Python 如何在pygame程序中移动对象?,python,pygame,Python,Pygame,我正在编写一个移动盒子的游戏。我已经做了所有的事情,并让它运行和显示,但盒子不能走出屏幕。我认为这是和elifs有关的或者在他们里面。我的错误在哪里?我怎样才能让他们移动 screen = pygame.display.set_mode( (640, 480) ) pygame.display.set_caption("Wahoo") background = pygame.Surface(screen.get_size()) background = background.conver

我正在编写一个移动盒子的游戏。我已经做了所有的事情,并让它运行和显示,但盒子不能走出屏幕。我认为这是和elifs有关的或者在他们里面。我的错误在哪里?我怎样才能让他们移动

screen = pygame.display.set_mode( (640, 480) )
pygame.display.set_caption("Wahoo")




background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill( (204,204,255) )



box1 = pygame.Surface((100,100))
box1 = box1.convert()
labelBox(box1, (153, 255, 255), "1")
box1X = 0          # The starting location for this box
box1Y = 0
moveBox1 = False   # If True, this is the box that moves

box2 = pygame.Surface((100,100))
box2 = box2.convert()
labelBox(box2, (255, 255,153), "2")
box2X = 540        # The starting location for this box
box2Y = 0
moveBox2 = False   # If True, this is the box that moves

box3 = pygame.Surface((100,100))
box3 = box3.convert()
labelBox(box3, (153, 255, 153), "3")
box3X = 540        # The starting location for this box
box3Y = 380
moveBox3 = False   # If True, this is the box that moves

box4 = pygame.Surface((100,100))
box4 = box4.convert()
labelBox(box4, (255, 153, 204), "4")
box4X = 0        # The starting location for this box
box4Y = 380
moveBox4 = False   # If True, this is the box that moves



clock = pygame.time.Clock()
keepGoing = True



    clock.tick(30)   # Maintain 30 frame per second refresh.


    for event in pygame.event.get() :
        if   event.type == pygame.QUIT :
                keepGoing = False
        elif event.type == pygame.MOUSEBUTTONDOWN :
            box1X=0
            box1Y=0

            box2X=540
            box2Y=0

            box3X=540
            box3Y=380

            box4X=0
            box4Y=380





        elif event.type == pygame.KEYDOWN :
            if   event.key == pygame.K_ESCAPE :
                keepGoing = False



            elif event.key == pygame.K_1 :
                moveBox1==True
                moveBox2==False
                moveBox3==False
                moveBox4==False

            elif event.key == pygame.K_2 :
                moveBox2==True
                moveBox3==False
                moveBox4==False
                moveBox1==False


            elif event.key == pygame.K_3 :
                moveBox3==True
                moveBox4==False
                moveBox2==False
                moveBox1==False

            elif event.key == pygame.K_4:
                 moveBox4==True
                 moveBox1==False
                 moveBox2==False
                 moveBox3==False



            elif event.key == pygame.K_LEFT :
                if moveBox1==True and box1X>=30:
                    box1X=box1X-30
                elif box2==True and box2X>=30:
                    box2=box2X-30
                elif box3==True and box3X>=30:
                    box3=box3X-30
                elif box4==True and box4X>=30:
                    box4=box4X-30


            elif event.key == pygame.K_RIGHT :
                if moveBox2==True and box2X<=540:
                    box2X=box2X+540
                elif box3==True and box3X<=540:
                    box3=Box3X+540
                elif box4==True and box4X<=540:
                    box4=box4X+540
                elif box1==True and box1X<=540:
                    box1=box1X+540


            elif event.key == pygame.K_UP :
                if moveBox3==True and box3Y<=580:
                    box3Y==box3Y+580
                elif box4==True and box4Y>=580:
                    box4=box4Y+580
                elif box2==True and box2Y>=580:
                    box2=box2Y+580
                elif box1==True and box1Y>=580:
                    box1=box1Y+580



                elif event.key == pygame.K_DOWN :
                    if moveBox4==True and box4Y>=380:
                        box4Y=box4Y-380
                    elif box1==True and box1Y>=580:
                        box1=box1Y-380
                    elif box2==True and box2Y>=580:
                        box2=box2Y-380
                    elif box3==True and box3Y>=580:
                        box3=box3Y-380 






    screen.blit(background, (0,0))
    screen.blit(box1, (box1X, box1Y))
    screen.blit(box2, (box2X, box2Y))
    screen.blit(box3, (box3X, box3Y))
    screen.blit(box4, (box4X, box4Y))

    pygame.display.flip()
screen=pygame.display.set_模式((640480))
pygame.display.set_标题(“Wahoo”)
background=pygame.Surface(screen.get_size())
background=background.convert()
背景填充((204255))
box1=pygame.Surface((100100))
box1=box1.convert()
标签盒(第1栏,(153、255、255),“1”)
box1X=0#此框的起始位置
box1Y=0
moveBox1=False#如果为True,则此框将移动
box2=pygame.Surface((100100))
box2=box2.convert()
标签盒(第2框,(255,255153),“2”)
box2X=540#此框的起始位置
box2Y=0
moveBox2=False#如果为True,则此框将移动
box3=pygame.Surface((100100))
box3=box3.convert()
标签盒(第三框,(153255153),“3”)
box3X=540#此框的起始位置
box3Y=380
moveBox3=False#如果为True,则此框将移动
box4=pygame.Surface((100100))
box4=box4.convert()
标签盒(第4框,(255、153、204),“4”)
box4X=0#此框的起始位置
方框4Y=380
moveBox4=False#如果为True,则此框将移动
clock=pygame.time.clock()
继续=正确
时钟。勾选(30)#保持每秒30帧刷新。
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
继续=错误
elif event.type==pygame.MOUSEBUTTONDOWN:
box1X=0
box1Y=0
box2X=540
box2Y=0
box3X=540
box3Y=380
box4X=0
方框4Y=380
elif event.type==pygame.KEYDOWN:
如果event.key==pygame.K_退出:
继续=错误
elif event.key==pygame.K_1:
moveBox1==True
moveBox2==False
moveBox3==False
moveBox4==False
elif event.key==pygame.K_2:
moveBox2==True
moveBox3==False
moveBox4==False
moveBox1==False
elif event.key==pygame.K_3:
moveBox3==True
moveBox4==False
moveBox2==False
moveBox1==False
elif event.key==pygame.K_4:
moveBox4==True
moveBox1==False
moveBox2==False
moveBox3==False
elif event.key==pygame.K_左:
如果moveBox1==True且box1X>=30:
box1X=box1X-30
elif box2==真,box2X>=30:
box2=box2X-30
elif box3==真,box3X>=30:
box3=box3X-30
elif box4==真,box4X>=30:
box4=box4X-30
elif event.key==pygame.K_RIGHT:
如果moveBox2==True且box2X=580:
box1=box1Y-380
elif box2==真,box2Y>=580:
box2=box2Y-380
elif box3==真,box3Y>=580:
box3=box3Y-380
屏幕光点(背景,(0,0))
屏幕blit(框1,(框1X,框1Y))
屏幕。blit(框2,(框2X,框2Y))
屏幕显示板(第3框,(第3框,第3框)
屏幕。blit(第4箱,(第4X箱,第4Y箱))
pygame.display.flip()
变量名、赋值和逻辑存在多(多)个问题

问题的核心在于运动代码。
Box
是曲面
box1
,具有坐标
box1X
box1Y
。布尔值
moveBox1
确定键盘输入是否用于所选框(键:1,2,3,4)

控制向左移动的代码是:

elif event.key == pygame.K_LEFT :
    if moveBox1==True and box1X>=30:   # <-- this clause is OK
        box1X=box1X-30
    elif box2==True and box2X>=30:     # <-- WRONG, not "box2", use "moveBox2=="
        box2=box2X-30                  # <-- WRONG, not "box2", use "box2X="
    elif box3==True and box3X>=30:
        box3=box3X-30
    elif box4==True and box4X>=30:
        box4=box4X-30
这会将框移动到屏幕的另一侧,这是有意的吗

我将代码打了补丁,一次可以将
box1
左右移动10个像素:

import pygame

pygame.init()
screen = pygame.display.set_mode( (640, 480) )
pygame.display.set_caption("Wahoo")


background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill( (204,204,255) )

# Not supplied by OP
def labelBox( surface, colour, label ):
    surface.fill( colour )

box1 = pygame.Surface((100,100))
#box1 = box1.convert()
labelBox(box1, (153, 255, 255), "1")
box1X = 0          # The starting location for this box
box1Y = 0
moveBox1 = False   # If True, this is the box that moves

box2 = pygame.Surface((100,100))
box2 = box2.convert()
labelBox(box2, (255, 255,153), "2")
box2X = 540        # The starting location for this box
box2Y = 0
moveBox2 = False   # If True, this is the box that moves

box3 = pygame.Surface((100,100))
box3 = box3.convert()
labelBox(box3, (153, 255, 153), "3")
box3X = 540        # The starting location for this box
box3Y = 380
moveBox3 = False   # If True, this is the box that moves

box4 = pygame.Surface((100,100))
box4 = box4.convert()
labelBox(box4, (255, 153, 204), "4")
box4X = 0        # The starting location for this box
box4Y = 380
moveBox4 = False   # If True, this is the box that moves



clock = pygame.time.Clock()
keepGoing = True

while ( keepGoing == True ):

    clock.tick(30)   # Maintain 30 frame per second refresh.


    for event in pygame.event.get() :
        if event.type == pygame.QUIT :
            keepGoing = False
        elif event.type == pygame.MOUSEBUTTONDOWN :
            box1X=0
            box1Y=0

            box2X=540
            box2Y=0

            box3X=540
            box3Y=380

            box4X=0
            box4Y=380

        elif event.type == pygame.KEYDOWN :
            print("KEY...")
            if event.key == pygame.K_ESCAPE :
                keepGoing = False

            elif event.key == pygame.K_1 :
                moveBox1=True
                moveBox2=False
                moveBox3=False
                moveBox4=False

            elif event.key == pygame.K_2 :
                moveBox2=True
                moveBox3=False
                moveBox4=False
                moveBox1=False

            elif event.key == pygame.K_3 :
                moveBox3=True
                moveBox4=False
                moveBox2=False
                moveBox1=False

            elif event.key == pygame.K_4:
                moveBox4=True
                moveBox1=False
                moveBox2=False
                moveBox3=False

            elif event.key == pygame.K_LEFT :
                print("LEFT")
                if moveBox1==True and box1X>=30:
                    box1X -= 10
                    print("MOVE 1 LEFT, box1X=%d" % (box1X))
                elif moveBox2==True and box2X>=30:
                    box2X -= 10
                elif moveBox3==True and box3X>=30:
                    box3X -= 10
                elif moveBox4==True and box4X>=30:
                    box4X -= 10

            elif event.key == pygame.K_RIGHT :
                print("RIGHT")
                if moveBox1==True and box1X<=540:
                    box1X += 10
                    print("MOVE 1 RIGHT, box1X=%d" % (box1X))
                elif moveBox2==True and box2X<=540:
                    box2X += 10
                elif moveBox3==True and box3X<=540:
                    box3X += 10
                elif moveBox4==True and box4X<=540:
                    box4X += 10

            elif event.key == pygame.K_UP :
                if moveBox3==True and box3Y<=580:
                    box3Y==box3Y+580
                elif moveBox4==True and box4Y>=580:
                    box4Y=box4Y+580
                elif moveBox2==True and box2Y>=580:
                    box2Y=box2Y+580
                elif moveBox1==True and box1Y>=580:
                    box1Y=box1Y+580

            elif event.key == pygame.K_DOWN :
                if moveBox4==True and box4Y>=380:
                    box4Y=box4Y-380
                elif moveBox1==True and box1Y>=580:
                    box1Y=box1Y-380
                elif moveBox2==True and box2Y>=580:
                    box2Y=box2Y-380
                elif moveBox3==True and box3Y>=580:
                    box3Y=box3Y-380 

    screen.blit(background, (0,0))
    screen.blit(box1, (box1X, box1Y))
    screen.blit(box2, (box2X, box2Y))
    screen.blit(box3, (box3X, box3Y))
    screen.blit(box4, (box4X, box4Y))

    pygame.display.flip()
然后迭代列表,进行处理:

#... LEFT key pressed
for box in box_list:
    label, surface, coord, selected = box
    if ( selected == True ):
        ... do stuff to move box
这消除了拥有大量类似名称的位置变量的暴政。有这么多人,很容易对他们的来来往往感到困惑

因此,更好的是,将有关方框的所有内容都放在一个类中,这样可以将方框保持在一起,并提供更有意义的“可读”代码:


非常感谢你。我是个业余爱好者,恐怕对课程不熟悉。不幸的是,由于我的需求,我不能使用类或列表。我试着把它修好,下面是它的运行情况。不断出现的错误是:screen.blit(box4,(box4X,box4Y))TypeError:参数1必须是pygame.Surface,而不是int。下面是代码的外观。另外,3只向右移动一个单元,4只向上移动到屏幕顶部。我的目标是让数字向各个方向移动,但不要离开屏幕。再次感谢。@Josie-错误:
参数1必须是pygame.Surface,而不是int。
表示您已经覆盖了用其他内容创建的
曲面。也许还有一个
box4
赋值应该是
box4Y
?在代码中搜索
box4=
(或
box4=
)应该只有第一个赋值语句。@Josie不幸的是,由于我的要求,我不能使用类或列表:TBH这是一个奇怪的要求,因为显然您已经使用了
曲面
时钟
类。而且,您已经使用元组了,那么列表怎么会成为问题呢?
box1 = [ "Box1", Surface(100,100), (0,0),   True  ]
box2 = [ "Box2", Surface(100,100), (500,0), False ]
...

box_list = [ box1, box2, ... ] # for N boxes
#... LEFT key pressed
for box in box_list:
    label, surface, coord, selected = box
    if ( selected == True ):
        ... do stuff to move box
class Box:
    def __init__( self, label, size, position, colour ):
        self.surface  = pygame.Surface( ( size, size ) )
        self.rect     = self.surface.get_rect()
        self.rect.x   = position[0]
        self.rect.y   = position[1]
        self.selected = False
        self.surface.fill( colour )
        # TODO: put label on box

   def moveLeft( self ):
       self.rect.x -= 30

   def paint( self, window ):
       window.blit( self.surface, ( self.rect.x, self.rect.y ) )

...

#... LEFT key pressed
for box in box_list:
    if ( box.selected == True ):
        box.moveLeft()