2016-10-22 36 views
0

嗨,我正在製作一個格鬥遊戲,以獲得pygame的一些練習,但我遇到了蹲/躲避問題。當我按下按鈕時,它會回到原來的位置,然後鴨子。如果您需要更多信息來幫助我提供。使sprite蹲在當前位置蟒蛇

import pygame 
import random 

display_height = 600 
display_width = 1000 
dis_screen = pygame.display.set_mode((display_width, display_height)) 
FPS = 30 
clock = pygame.time.Clock() 
img = pygame.image.load('foo.png') 
crouchimg = pygame.image.load('crouchimg.png') 


# Simple player object 
class Player(object): 
    def __init__(self, x, y, image): 
     self.x = x 
     self.y = y 
     self.image = image 

    # Method to draw object 
    def draw(self): 
     dis_screen.blit(self.image, (self.x, self.y)) 

    # Method to move object 
    def move(self, speedx, speedy): 
     self.x += speedx 
     self.y += speedy 


class MainRun(object): 
    def __init__(self, displayw, displayh): 
     self.dw = displayw 
     self.dh = displayh 
     self.Main() 

    def Main(self): 
     # Put all variables up here 
     stopped = False 
     x_move = 0 
     y_move = 0 
     p1_y_loc = 200 
     p1_x_loc = 200 
     x = pygame.Rect().x 
     greg = Player(p1_x_loc, p1_y_loc, img) 
     # Main Loop 
     while not stopped: 
      print(x) 
      dis_screen.fill((255, 255, 255)) # Tuple for filling display... Current is white 
      # Event Tasking 
      for event in pygame.event.get(): 
       if event.type == pygame.QUIT: 
        pygame.quit() 
        quit() 
       if event.type == pygame.KEYDOWN: 
        if event.key == pygame.K_RIGHT: 
         y_move = 0 
         x_move = 5 
        elif event.key == pygame.K_LEFT: 
         y_move = 0 
         x_move = -5 
        elif event.key == pygame.K_UP: 
         y_move = -5 
         x_move = 0 
        elif event.key == pygame.K_DOWN: 
         p1_y_loc = 300 
         p1_x_loc = 0 
         greg = Player(p1_x_loc, p1_y_loc, crouchimg) 

       if event.type == pygame.KEYUP: 
        if event.key == pygame.K_DOWN: 
         p1_y_loc = 200 
         greg = Player(p1_x_loc, p1_y_loc, img) 
        if event.key == pygame.K_UP: 
         y_move = 0 
        if event.key == pygame.K_RIGHT or event.key == pygame.K_LEFT: 
         x_move = 0 




      greg.move(x_move, y_move) 
      greg.draw() 
      pygame.display.update() 
      clock.tick(FPS) 
     pygame.quit() 
     quit() 

run = MainRun(display_width, display_height) 
run.Main() 

回答

0

這是因爲,當你按向下鍵要創建在x位置p1_x_loc一個新的播放器(您已設置爲0)和y軸位置p1_y_loc(您已設置到300)。所以當玩家蹲下時,無論玩家當前位置如何,它也會將其移動到(0,300)。

解決這個問題的一種方法是改變玩家的形象,而不是創造一個全新的玩家。你可以這樣做,player.image = crouchimg而不是greg = Player(p1_x_loc, p1_y_loc, crouchimg)。而當玩家起牀你只是再次改變形象:player.image = img

如果你有蹲下的時候改變球員的y位置,你可以做同樣的方式:player.y = 200player.y = 300