Skip to main content
 首页 » 编程设计

python之如何让方 block 在pygame中来回移动

2024年10月17日7小虾米

我正在尝试制作一个移动平台,因此当平台到达窗帘点时,它应该反转方向并返回,但从我所看到的来看,它看起来像是在前后振动 程序链接:https://drive.google.com/file/d/0BzvvQCByWwmAQThfdkEtSlRKa1k/view?usp=sharing

这是我的代码:

class lbuild(pygame.sprite.Sprite): 
#This class represents alevel builder. It derives from the "Sprite" class in Pygame. 
 
def __init__(self, color, width, height,x,y): 
    # Call the parent class (Sprite) constructor 
    super().__init__() 
 
    self.image = pygame.Surface([width, height]) 
    self.image.fill(WHITE) 
    self.image.set_colorkey(WHITE) 
 
    # Draw the car (a rectangle!) 
    pygame.draw.rect(self.image, color, [0, 0, width, height]) 
 
     # Fetch the rectangle object that has the dimensions of the image. 
    self.rect = self.image.get_rect() 
    self.rect.x=x 
    self.rect.y=y 
all_sprites_list = pygame.sprite.Group() 
movblock=pygame.sprite.Group()#sprite group 
def level1(): 
     global all_sprites_list 
     global movblock 
     xpos=0 
 
     for x in range(50): 
 
        all_sprites_list.add(lbuild(GREY,20,20,xpos,680)) 
        xpos =xpos+20 
    ypos=660 
    xpos2 =40 
 
    for x in range(2): 
        all_sprites_list.add(lbuild(black,60,20,xpos2,ypos)) 
        ypos=ypos-20 
    mblk=lbuild(RED,100,20,120,600)#draws the block 
    movblock.add(mblk)#adds it to the sprite group 
 
 
 
 
 
   clock=pygame.time.Clock() 
 
   while True: 
            for event in pygame.event.get(): 
                if event.type==QUIT: 
                    pygame.quit() 
                    sys.exit() 
            #Game Logic 
            all_sprites_list.update() 
 
 
            #Drawing on Screen 
            screen.fill(WHITE) 
           #Draw The Road 
           spd=5 
 
           if mblk.rect.x>200:#supposed to cheak if the block x postion a has reached 200 and the reverse its direction but instead it looks like it is vibrating 
               spd= -spd 
           if mblk.rect.x<100: 
               spd= -spd 
           mblk.rect.x+=spd 
 
           #Now let's draw all the sprites in one go. (For now we only have 1 sprite!) 
           all_sprites_list.draw(screen) 
           movblock.draw(screen) 
 
        #Refresh Screen 
            pygame.display.flip() 
 
       #Number of frames per secong e.g. 60 
       clock.tick(60) 

请您参考如下方法:

您的问题是 spd = 5 在 while True 中。

你改变方向使用

spd = -spd 

但在那之后你用

覆盖它
spd = 5 

你必须在 while True 之前使用 spd = 5


具有其他修改的完整版本。

import pygame 
import sys 
 
# --- constants --- (UPPER_CASE names) 
 
WHITE  = (255, 255, 255) 
GREEN  = (20, 255, 140) 
GREY   = (210, 210 ,210) 
WHITE  = (255, 255, 255) 
RED    = (255, 0, 0) 
PURPLE = (255, 0, 255) 
BLACK  = (0,0,0) 
 
SCREEN_WIDTH=1000 
SCREEN_HEIGHT=700 
 
# --- classes --- (CamelCase names) 
 
class LBuild(pygame.sprite.Sprite): 
 
    def __init__(self, color, width, height, x, y): 
        super().__init__() 
 
        self.image = pygame.Surface([width, height]) 
        self.image.fill(WHITE) 
        self.image.set_colorkey(WHITE) 
 
        # Draw the car (a rectangle!) 
        pygame.draw.rect(self.image, color, [0, 0, width, height]) 
 
        # Fetch the rectangle object that has the dimensions of the image. 
        self.rect = self.image.get_rect() 
        self.rect.x = x 
        self.rect.y = y 
 
# --- functions --- (lower_case names) 
 
def level_1(screen, all_sprites_list, movblock): 
 
    x = 0 
 
    for _ in range(50): 
        all_sprites_list.add(LBuild(GREY, 20, 20, x, 680)) 
        x += 20 
 
    y = 660 
    x2 = 40 
 
    for _ in range(2): 
        all_sprites_list.add(LBuild(BLACK, 60, 20, x2, y)) 
        y -= 20 
 
    mblk = LBuild(RED, 100, 20, 120, 600) 
    movblock.add(mblk) 
 
    spd = 5 
 
    # - mainloop - 
 
    clock = pygame.time.Clock() 
 
    #current_time = pygame.time.get_ticks() 
    # change something after 2s 
    #change_time = current_time + 2000 # 2000ms = 2s 
 
    while True: 
 
        # - events - 
 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                # False = exit game 
                return False 
            if event.type == pygame.KEYDOWN: 
                if event.key == pygame.K_ESCAPE: 
                    # True = go to next level 
                    return True 
 
        # - updates (without draws) - 
 
        #current_time = pygame.time.get_ticks() 
 
        #if current_time >= change_time: 
        #    TODO: change something 
        #    # change something again after 2s 
        #    change_time = current_time + 2000 
 
        all_sprites_list.update() 
 
        if mblk.rect.x > 200: 
            spd = -spd 
 
        if mblk.rect.x < 100: 
            spd = -spd 
 
        mblk.rect.x += spd 
 
        # - draws (without updates) - 
 
        screen.fill(WHITE) 
 
        all_sprites_list.draw(screen) 
        movblock.draw(screen) 
 
        pygame.display.flip() 
 
        # - FPS - 
 
        clock.tick(60) 
 
# --- main --- 
 
# - init - 
pygame.init() 
 
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) 
pygame.display.set_caption("Car Racing") 
 
# - game - 
 
all_sprites_list = pygame.sprite.Group() 
movblock = pygame.sprite.Group() 
 
goto_next_level = level_1(screen, all_sprites_list, movblock) 
 
#if goto_next_level: 
#    goto_next_level = level_2(screen, all_sprites_list, movblock)  
 
#if goto_next_level: 
#    goto_next_level = level_3(screen, all_sprites_list, movblock)  
 
# - exit - 
 
pygame.quit() 
sys.exit()