2016-12-23 3 views
1

私は動いているプラ​​ットフォームを作ろうとしているので、プラットフォームがカーテンポイントに達すると方向を逆転させて戻ってきますが、見えるものから見えます背中や砦 プログラムリンク振動:ここ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) 
+0

変数 'direction = 'left''を使用する – furas

+0

意図が正しくなく、テストするためのコードを実行できません。 – furas

+0

私は完全なプログラムをダウンロードして実行できるリンクを貼っています –

答えて

1

あなたの問題はwhile True内部spd = 5です。

あなたは

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() 
関連する問題