2017-04-04 11 views
0

誰かが私を助けたり、私の問題について助言を与えることができることを願っています。 私はPython用のpygameモジュールを使いこなしてきました。私は解決できないような問題に遭遇しました。 私は何が起こりますか:プレイヤーが壁に衝突したときに壁ジャンプ手順を実行する

  • ユーザーは壁に衝突します。
  • ユーザーがジャンプコマンドを入力しました。
  • プレーヤーオブジェクトがジャンプしてから落ちます。
  • 繰り返すことができます。

しかし、私のコードでは、プレイヤーは「上向き矢印」と「左向き矢印」を保持し、常に上向きに飛ぶことができます。この周り。私は多くの試みを無駄にしてきました。

これは完全なコードです:

import pygame 
from pygame import * 

WIN_WIDTH = 800 
WIN_HEIGHT = 800 
HALF_WIDTH = int(WIN_WIDTH/2) 
HALF_HEIGHT = int(WIN_HEIGHT/2) 

DISPLAY = (WIN_WIDTH, WIN_HEIGHT) 
DEPTH = 32 
FLAGS = 0 
CAMERA_SLACK = 30 

MAPLIST = ["LevelOne.txt"] 
MAPCOUNT = 0 

class Camera(object): 
    def __init__(self, camera_func, width, height): 
     self.camera_func = camera_func 
     self.state = Rect(0, 0, width, height) 
    def apply(self, target): 
     return target.rect.move(self.state.topleft) 
    def update(self, target): 
     self.state = self.camera_func(self.state, target.rect) 

def complex_camera(camera, target_rect): 
    l, t, _, _ = target_rect 
    _, _, w, h = camera 
    l, t, _, _ = -l+HALF_WIDTH, -t+HALF_HEIGHT, w, h 
    l = min(0, l)       # stop scrolling at the left edge 
    l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling at the right edge 
    t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling at the bottom 
    t = min(0, t)       # stop scrolling at the top 
    return Rect(l, t, w, h) 

class Entity(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = Surface((32, 32)) 
     self.image.convert() 
     self.image.fill(Color("#f21000")) 

class Player(Entity): 
    def __init__(self, x, y): 
     Entity.__init__(self) 

     #self.image = pygame.image.load("player.png") 
     #self.image.convert() 
     self.rect = Rect(x, y, 32, 32) 

     self.xvel = 0 
     self.yvel = 0 
     self.onGround = False 
     self.canJump = False 
     self.wallCollide = False 

    def jump(self): 

     if self.onGround: 
      self.yvel = 0 
      self.yvel -= 8 
      self.rect.top += self.yvel 
      self.onGround=False 
      self.canJump=False 

     if self.canJump and not self.onGround: 
      self.yvel = 0 
      self.yvel -= 4 
      self.rect.top += self.yvel 
      self.onGround = False 
      self.canJump = False 

    def update(self, up, down, left, right, running, platforms,entities): 
     if self.wallCollide and not self.onGround: 

     if down: 
      print(self.wallCollide) 
     if up: 
      self.jump() 
     if running: 
      self.xvel = 1 
     if left: 
      self.xvel = -4 
     if right: 
      self.xvel = 4 

     if not self.onGround: 
       # print("x") 
       # only accelerate with gravity if in the air 
      self.yvel += 0.3 
       # max falling speed 
      if self.yvel > 100: 
       self.yvel = 100 

     if not (left or right): 
      self.xvel = 0 
     # increment in x direction 
     self.rect.left += self.xvel 
     # do x-axis collisions 
     self.collide(self.xvel, 0, platforms, entities) 
     # increment in y direction 
     if not self.onGround: 
      self.rect.top += self.yvel 
     # assuming we're in the air 
     # do y-axis collisions 
     self.collide(0,self.yvel, platforms, entities) 

    def collide(self, xvel, yvel, platforms, entities): 
     # Player collisions with platforms. e.g. main blocks 
     for p in platforms: 
      if pygame.sprite.collide_rect(self, p): 
       if yvel == 0: 
        self.wallCollide = True 
       if xvel > 0: 
        self.rect.right = p.rect.left # collide right 
       if xvel < 0: 
        self.rect.left = p.rect.right # collide left 
       if yvel > 0: 
        self.rect.bottom = p.rect.top 
        self.onGround = True 
        self.canJump = True 
        self.yvel = 0 
       if yvel < 0: 
        self.rect.top = p.rect.bottom 
        # if isinstance(p,Platform): 
        # self.onGround = True 

class Platform(Entity): 
    def __init__(self, x, y): 
     Entity.__init__(self) 
     self.image = pygame.image.load("brick.png") 
     self.image.convert() 
     self.rect = Rect(x, y, 32, 32) 

def LoadMap(platforms,entities,x,y): 
    currentLevel=[] 
    mapFile = open(MAPLIST[MAPCOUNT]) 
    for line in mapFile: 
     currentLevel.append(line) 

    for row in currentLevel: 
     for col in row: 
      if col == "P": 
       p = Platform(x, y) 
       platforms.append(p) 
       entities.add(p) 
      elif col == "X": 
       player=Player(x,y) 
       entities.add(player) 
      x += 32 
     y += 32 
     x = 0 
    return currentLevel, player 

def main(): 
    pygame.init() 
    screen = pygame.display.set_mode(DISPLAY, FLAGS, DEPTH) 
    pygame.display.set_caption("Super Meat Boy") 
    myfont = pygame.font.SysFont("monospace", 40) 
    timer = pygame.time.Clock() 

    entities = pygame.sprite.Group() 
    platforms = [] 

    up = down = left = right = running = False 
    x = y = 0 

    bg = Surface((32, 32)) 
    bg.convert() 
    bg.fill(Color("#66ccff")) 
    currentLevel,player = LoadMap(platforms,entities,x,y) 

    total_level_width = len(currentLevel[0]) * 32 
    total_level_height = len(currentLevel) * 32 
    camera = Camera(complex_camera, total_level_width, total_level_height) 

    while True: 
     clock=timer.tick(60) 
     for e in pygame.event.get(): 

      if e.type == KEYDOWN and e.key == K_UP: 
       up = True 

      if e.type == KEYDOWN and e.key == K_DOWN: 
       down = True 

      if e.type == KEYDOWN and e.key == K_LEFT: 
       left = True 

      if e.type == KEYDOWN and e.key == K_RIGHT: 
       right = True 

      if e.type == KEYDOWN and e.key == K_SPACE: 
       running = True 

      if e.type == KEYUP and e.key == K_UP: 
       up = False 
      if e.type == KEYUP and e.key == K_DOWN: 
       down = False 

      if e.type == KEYUP and e.key == K_RIGHT: 
       right = False 

      if e.type == KEYUP and e.key == K_LEFT: 
       left = False 

      if e.type == KEYUP and e.key == K_SPACE: 
       running = False 

     for y in range(32): 
      for x in range(32): 
       screen.blit(bg, (x * 32, y * 32)) 

     camera.update(player) 

     player.update(up, down, left, right, running, platforms, entities) 

     for e in entities: 
      screen.blit(e.image, camera.apply(e)) 


     pygame.display.update() 

if __name__ == "__main__": 
    main() 

これは問題があるコードの一部です:

私のことができるようにするためにプレーヤーのためにしたくないという結論でそう
class Player(Entity): 
    def __init__(self, x, y): 
     Entity.__init__(self) 

     #self.image = pygame.image.load("player.png") 
     #self.image.convert() 
     self.rect = Rect(x, y, 32, 32) 

     self.xvel = 0 
     self.yvel = 0 
     self.onGround = False 
     self.canJump = False 
     self.wallCollide = False 

    def jump(self): 
     if self.onGround: 
      self.yvel = 0 
      self.yvel -= 8 
      self.rect.top += self.yvel 
      self.onGround=False 
      self.canJump=False 

     if self.canJump and not self.onGround: 
      self.yvel = 0 
      self.yvel -= 4 
      self.rect.top += self.yvel 
      self.onGround = False 
      self.canJump = False 

    def update(self, up, down, left, right, running, platforms, entities): 
     if self.wallCollide and not self.onGround: 

     if down: 
      print(self.wallCollide) 
     if up: 
      self.jump() 
     if running: 
      self.xvel = 1 
     if left: 
      self.xvel = -4 
     if right: 
      self.xvel = 4 

     if not self.onGround: 
       # print("x") 
       # only accelerate with gravity if in the air 
      self.yvel += 0.3 
       # max falling speed 
      if self.yvel > 100: 
       self.yvel = 100 

     if not (left or right): 
      self.xvel = 0 
     # increment in x direction 
     self.rect.left += self.xvel 
     # do x-axis collisions 
     self.collide(self.xvel, 0, platforms, entities) 
     # increment in y direction 
     if not self.onGround: 
      self.rect.top += self.yvel 
     # assuming we're in the air 
     # do y-axis collisions 
     self.collide(0,self.yvel, platforms, entities) 

    def collide(self, xvel, yvel, platforms, entities): 
     # Player collisions with platforms. e.g. main blocks 
     for p in platforms: 
      if pygame.sprite.collide_rect(self, p): 
       if yvel == 0: 
        self.wallCollide = True 
       if xvel > 0: 
        self.rect.right = p.rect.left # collide right 
       if xvel < 0: 
        self.rect.left = p.rect.right # collide left 
       if yvel > 0: 
        self.rect.bottom = p.rect.top 
        self.onGround = True 
        self.canJump = True 
        self.yvel = 0 
       if yvel < 0: 
        self.rect.top = p.rect.bottom 
        # if isinstance(p,Platform): 
        # self.onGround = True 

彼らは "上向きの矢印"を保持するときに壁を飛ぶ。どんな助けやアドバイスも大歓迎です。

(私は間違った形式や場所でこれを投稿した場合は申し訳ありませんが、私はこのサイトに新しいです。)

答えて

0

これは本当にコーディング答えではなく、設計1のより。

ビデオゲームでの壁ジャンプの一般的な設計の1つは、壁にジャンプするときに文字を自動的に側面に押し込むことです。 Megaman Xシリーズを考えてみましょう。あなたが右の壁に飛び込んで飛び降りると、あなたは左に押し込まれます。あなたはまだ壁から離れてバウンスを維持するために右を保持することはできますが、壁をズームアップすることはできません。

私の提案は、walljumpsに特別なタイプのジャンプをさせることです。ここでは壁の反対側の方向にシャープなブーストを与えます。

また、キャラクタが頻繁にタックする可能性のある回数を制限します。 walljumpは、設定された数のフレームのために壁のジャンプを防ぐフラグを反転させるかもしれません。

+0

こんにちは、返信ありがとう、私はあなたの提案を使用して、キャラクターを壁から「バウンス」させようとしていますが、キャラクターを反対方向に移動させましたが、まだプレイヤーとしての衝突を検出していますたとえば[左矢印]のように、プログラムが壁に当たったときに文字を右に移動させますが、左矢印を保持しているので、ほとんど瞬間的にちょうど上向きに飛びます。これを達成する方法について他のアイデアはありますか?私がスーパー肉の男の子のエスケープゲームを目指していると言ったら、助けになったかもしれない。再度、感謝します。 – Buzz

関連する問題