2016-10-22 64 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