2016-05-29 11 views
0

以前は、コードの遅れを修正していましたが、以前はスプライトが遅れていました。私たちはそれを修正しましたが、今度はスプライトが高すぎて画面の外に出てこないようにしています...スプライト(ポピー)があまり上がらないように私たちは何ができますか?スプライトのジャンプが高すぎる

import pygame, sys 
from pygame.locals import * 
pygame.init() 

class Poppy(pygame.sprite.Sprite): 
    def __init__(self): #making the player 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load('POPPY.png') 
     self.rect = self.image.get_rect() 
     self.grav = .5 
     self.y_vel = 5 
     self.jumping = False 

    def jump_update(self): #checking the jumps 
     if self.jumping: 
      self.y_vel += self.grav 
      self.rect.y += self.y_vel 

    def jump(self): #the jump initializer 
     if not self.jumping: 
      self.y_vel = -50 
      self.jumping = True 

    def keys(self): #the keys 
     key = pygame.key.get_pressed() 
     dist = 5 
     if key[pygame.K_RIGHT]: # right key 
      self.rect.x += dist # move right 
     elif key[pygame.K_LEFT]: # left key 
      self.rect.x -= dist 

    def collide_check(self, platform_list): #check if it hit the ground or the platforms 
     for blocks in platform_list: 
      if pygame.sprite.collide_rect(self, blocks) == True: 
       self.rect.y = blocks.top 

     if self.rect.y >= 600-self.rect.height:    # BALL HITS TOP AND BOTTOM 
      self.y_vel = 0 
      self.rect.y = 600 - self.rect.height 
      self.jumping = False 

    def move(self): 
     self.rect.y += self.y_vel 

    def draw(self, surface): 
     surface.blit(self.image, (self.rect.x, self.rect.y)) 

class Platform(pygame.sprite.Sprite): 
    def __init__(self): 
     pygame.sprite.Sprite.__init__(self) 
     self.image = pygame.image.load('levoneplatform.png') 
     self.rect = self.image.get_rect() 

class Enemy(pygame.sprite.Sprite): 

def __init__(self): 
    pygame.sprite.Sprite.__init__(self) 
    self.image = pygame.image.load('enemy.png') 
    self.rect = self.image.get_rect() 


class Menu(): 
    def __init__(self): 
     self.background_image = pygame.image.load('menu.png').convert_alpha() 

    def update(self): 
     screen.blit(self.background_image, [0,0]) 


class LevOne(): 
    def __init__(self): 

     self.background_image = pygame.image.load('night.png').convert_alpha() 


     platforms_one = [ (200,300), 
         (50,500), 
         (550,650), 
         (300,200), 
         (120,100) 
        ] 
     for k,v in platforms_one: 
      platform = Platform() 
      enemy = Enemy() 
      platform.rect.x = k 
      enemy.rect.x = k 
      platform.rect.y = v 
      enemy.rect.y = v - 44 
      platform_list.add(platform) 
      enemy_list.add(enemy) 

    def update(self): 
     screen.blit(self.background_image, [0, 0]) 

class LevTwo(): 
    def __init__(self): 

     self.background_image = pygame.image.load('night.png').convert_alpha() 

     platforms_two = platforms_two = [ (300,400), 
         (500,500), 
         (100,300), 
         (300,100), 
        ] 

     for k,v in platforms_two: 
      platform = Platform() 
      enemy = Enemy() 
      platform.rect.x = k 
      enemy.rect.x = k 
      platform.rect.y = v 
      enemy.rect.y = v - 44 
      platform_list.add(platform) 
      enemy_list.add(enemy) 

    def update(self): 
     screen.blit(self.background_image, [0,0]) 


screen = pygame.display.set_mode((800,600)) 
enemy_list = pygame.sprite.Group() 
platform_list = pygame.sprite.Group() 
Poppy = Poppy() 
Menu = Menu() 
LevOne = LevOne() 


clock = pygame.time.Clock() 
level = 1 

while True: 
    clock.tick(60) 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 
     elif event.type == KEYDOWN: 
      if event.key == K_SPACE: 
       Poppy.jump() 

    Menu.update() 
    if event.type == MOUSEBUTTONDOWN: 
     x,y = pygame.mouse.get_pos() 
     if x >= 544.86 and x <= 700.86 and y <= 385.02 and y >= 340.03: 
      level = 1 

    if level == 1: 


     Poppy.keys() 
     Poppy.move() 

     LevOne.update() 


     Poppy.draw(screen) 
     platform_list.draw(screen) 
     enemy_list.draw(screen) 

     pygame.display.update() 

    if level == 2: 

     second_lives = 3 

     LevTwo.update() 
     Poppy.keys() 
     Poppy.move() 

     platform_list.draw(screen) 
     enemy_list.draw(screen) 
     Poppy.draw(screen) 

    pygame.display.update() 

答えて

1

重力を0.5単位/秒^ 2に設定しましたが、-50単位/秒の速度でジャンプします。キャラクターが再び落ち始めるまでに2分ほどかかります!

重力を上げるか、ジャンプの速度を-3のように下げてください。

EDIT:

コールあなたmove()update()関数内のどこかにこの機能:今、それが使用されていない

def jump_update(self): #checking the jumps 
    if self.jumping: 
     self.y_vel += self.grav 
     self.rect.y += self.y_vel 

+0

ありがとうございますが、動きは止まりません。私は何ができますか? – Angela

+0

@Angelaあなたのコードを見て、いくつかの間違いがありました。私はそれらのうちの一つを含めるように私の答えを編集しました。衝突を適切にチェックしていることを忘れないでください。私はプレーヤーをプラットフォームに着陸させることができませんでした。 'self.y_vel> 12'のとき重力の追加を止めて、落ち込みが速すぎないようにすることもできます。 – Aaron3468

関連する問題