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

时间:2019-03-22 00:11:38

标签: python pygame

我正在编写带有移动盒子的游戏。我已经完成了所有工作,让它可以运行并显示,但是这些框不能超出屏幕范围。我认为这与小精灵或其中的东西有关。我的错误在哪里,如何使它们移动?

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()

1 个答案:

答案 0 :(得分:1)

变量名称,赋值和逻辑存在多个(很多)问题。

问题的核心在于运动代码。 Box是表面box1,并具有坐标box1Xbox1Y。布尔值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

因此,如果用户试图移动box2,则不可能,并且会损坏存储在box2中的表面,编号为box2X-30。同样,所有其他移动代码也存在问题。

K_RIGHT运动子句中:

box2X = box2X + 540

哪个将框移动到屏幕的另一侧?

我将代码修补到可以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()

我不想听起来很刺耳-因为我们都是初学者,但是这段代码很好地说明了为什么数据结构和类(尽管最初工作更多)却使编码很多更加容易且更快。

OP的box可轻松存储在列表中,例如:

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

这消除了拥有大量相似名称的位置变量的麻烦。拥有这么多的人,很容易对他们的来来往往感到困惑。

因此,更好的是,将有关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()