2017-11-10 20 views
0

私は外国人の侵略者の船を上下に動かすようにしようとしていますが、何かを乱さずに正しく動かせるようには見えません。上下に移動するには

私のコードでは、何を追加する必要がありますか?

alien_invasion.py:

import sys 
import pygame 

from settings import Settings 
from ship import Ship 
import game_functions as gf 

def run_game(): 
    #Initialize pygame, settings, and screen object 
    pygame.init() 
    ai_settings = Settings() 
    screen = pygame.display.set_mode(
     (ai_settings.screen_width, ai_settings.screen_height)) 
    pygame.display.set_caption("Alien Invasion") 

    #Draw the ship 
    ship = Ship(ai_settings, screen) 



    #Start the main loop for the game. 
    while True: 
     #Watch for keyboard and mouse events 
     gf.check_events(ship) 
     ship.update() 
     gf.update_screen(ai_settings, screen, ship) 


run_game() 

ship.py:

import pygame 

class Ship(): 
    def __init__(self, ai_settings, screen): 
     """Initialize teh ship and set its starting position""" 
     self.screen = screen 
     self.ai_settings = ai_settings 

    #Load teh ship image and get its rect 
    self.image = pygame.image.load('ship.bmp') 
    self.rect = self.image.get_rect() 
    self.screen_rect = screen.get_rect() 

    #Start each new ship at the bottom center of the screen 
    self.rect.centerx = self.screen_rect.centerx 
    self.rect.bottom = self.screen_rect.bottom 

    # Store a decimal value for the ship's center. 
    self.center = float(self.rect.centerx) 

    # Movement flag 
    self.moving_right = False 
    self.moving_left = False 

def update(self): 
    """Update the ship's postion based on the movement flag""" 
    # Update the ship's center value, not the rect. 
    if self.moving_right and self.rect.right < self.screen_rect.right: 
     self.center += self.ai_settings.ship_speed_factor 
    if self.moving_left and self.rect.left > 0: 
     self.center -= self.ai_settings.ship_speed_factor 

    # Update rect object from self.center 
    self.rect.centerx = self.center 

def blitme(self): 
    """Draw the ship at its current location""" 
    self.screen.blit(self.image, self.rect) 

game_fuction.py:

import sys 

import pygame 

def check_keydown_events(event, ship): 
    """Responds to keypresses""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = True 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = True 

def check_keyup_events(event, ship): 
    """Respoinds to key releases""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = False 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = False 

def check_events(ship): 
    """Respond to keypresses and mouse events.""" 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      sys.exit() 

    elif event.type == pygame.KEYDOWN: 
     check_keydown_events(event, ship) 

    elif event.type == pygame.KEYUP: 
     check_keyup_events(event, ship) 

def update_screen(ai_settings, screen, ship): 
    """Update images on the screen and flip to the new screen""" 
    #Redraw the screen during each pass through the loop 
    screen.fill(ai_settings.bg_color) 
    ship.blitme() 

#Make the most recently drawn screen visible. 
pygame.display.flip() 

settings.py:

class Settings(): 
    """A Class to store all settings for ALein INvasion""" 
    def __init__(self): 
     """Initialize the game's settings""" 
     #screen settings 
     self.screen_width = 1200 
     self.screen_height = 800 
     self.bg_color = (255,255,255) 

    # Ship Settings 
    self.ship_speed_factor = 1.5 

これはPython Crash Courseのプロジェクトです。 この特定のプロジェクトには、

の画面中央にロケットで始まるゲームがあります。 4つの矢印キーを使用して、プレーヤーがロケットを上下左右に動かせるようにします。ロケットが決して端を越えて移動しないようにするか、または 画面を確認してください。

+0

あなたは「何かをつぶすことなく正しく動作させることができないようだ」と言っています - それがどうしたら上手くいくのか、何を期待するのかを説明してください。ああ、 'game_fuction.py'では、あなたの' elif'sが間違ってインデントされています - それはタイプミスですか? –

+0

あなたはkeydownを操作します。つまり、キーがすでに*ダウンしているときは、イベントは呼び出されません。これにより、*連続的な*移動が防止されます。たとえば、keydown(すなわち、whileループのように更新され続ける)のような動きを初期化し、キ​​ーアップでそれを壊すことができます。 – CermakM

答えて

0

私はチェックするためにこれをタイプインしていませんが、私はpygameで同様の問題やプロジェクトを解決しました。

1)ダウンキーとアップキーを押したときのイベントをキャッチします。

# from your game_function.py 
def check_keydown_events(event, ship): 
    """Responds to keypresses""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = True 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = True 
    elif event.key == pygame.K_DOWN: 
     ship.moving_down = True 
    elif event.key == pygame.K_UP: 
     ship.moving_up = True 

def check_keyup_events(event, ship): 
    """Respoinds to key releases""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = False 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = False 
    elif event.key == pygame.K_DOWN: 
     ship.moving_down = False 
    elif event.key == pygame.K_UP: 
     ship.moving_up = False 

2)実際の動きを処理するために、あなたのオリジナルの船クラス: これはおそらくのようなものです。これは次のようになります :

# Movement flag 
self.moving_right = False 
self.moving_left = False 
self.moving_down = False 
self.moving_up = False 

def update(self): 
    """Update the ship's postion based on the movement flag""" 
    # Update the ship's center value, not the rect. 
    if self.moving_right and self.rect.right < self.screen_rect.right: 
     self.center += self.ai_settings.ship_speed_factor 
    if self.moving_left and self.rect.left > 0: 
     self.center -= self.ai_settings.ship_speed_factor 
    if self.moving_down and self.rect.bottom > self.screen_rect.bottom: 
     self.center -= self.ai_settings.ship_speed_factor 
    if self.moving_up and self.rect.top < self.screen_rect.top: 
     self.center -= self.ai_settings.ship_speed_factor 

    # Update rect object from self.center - might need to be changed now to handle 
    # movement in two directions properly. 
    if self.moving_up or self.moving_down: 
     self.rect.centery = self.center 
    if self.moving_left or self.moving_right: 
     self.rect.centerx = self.center 
+0

私はこのような試みをしましたが、船を連続的に上下させようとすると、それは許されません – CholoBoy

+0

これはおそらく、現在xとyを持たないself.centerで起こることをリファクタリングする必要があるためです。これらを個別に処理する必要があります。あなたが試したことを私に見せてください。私はすべての変更を行い、あなたに実用的なバージョンを与えることができるかどうかを見ていきます。 –

0

[OK]を、私はそれを割れてきました - このソリューションは、現在取り組んでいる:

settings.py

class Settings: 
    """A Class to store all settings for ALien INvasion""" 
    def __init__(self): 
     """Initialize the game's settings""" 
     #screen settings 
     self.screen_width = 1200 
     self.screen_height = 800 
     self.bg_color = (255,255,255) 

    # Ship Settings 
    ship_speed_factor = 1.5 

game_functions.py

import sys 

import pygame 

def check_keydown_events(event, ship): 
    """Responds to keypresses""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = True 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = True 
    elif event.key == pygame.K_DOWN: 
     ship.moving_down = True 
    elif event.key == pygame.K_UP: 
     ship.moving_up = True 

def check_keyup_events(event, ship): 
    """Responds to key releases""" 
    if event.key == pygame.K_RIGHT: 
     ship.moving_right = False 
    elif event.key == pygame.K_LEFT: 
     ship.moving_left = False 
    elif event.key == pygame.K_DOWN: 
     ship.moving_down = False 
    elif event.key == pygame.K_UP: 
     ship.moving_up = False 

def check_events(ship): 
    """Respond to keypresses and mouse events.""" 
    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
     elif event.type == pygame.KEYDOWN: 
      check_keydown_events(event, ship) 
     elif event.type == pygame.KEYUP: 
      check_keyup_events(event, ship) 

def update_screen(ai_settings, screen, ship): 
    """Update images on the screen and flip to the new screen""" 
    #Redraw the screen during each pass through the loop 
    screen.fill(ai_settings.bg_color) 
    ship.blitme() 

    #Make the most recently drawn screen visible. 
    pygame.display.flip() 

ship.py

import pygame 

class Ship: 
    def __init__(self, ai_settings, screen): 
     """Initialize teh ship and set its starting position""" 
     self.screen = screen 
     self.ai_settings = ai_settings 

     #Load the ship image and get its rect 
     self.image = pygame.image.load('ship.bmp') 
     self.rect = self.image.get_rect() 
     self.screen_rect = screen.get_rect() 

     #Start each new ship at the bottom center of the screen 
     self.rect.centerx = self.screen_rect.centerx 
     self.rect.bottom = self.screen_rect.bottom 

     # Store a decimal value for the ship's x and y center. 
     self.centerx = float(self.rect.centerx) 
     self.centery = float(self.rect.centery) 

     # Movement flag 
     self.moving_right = False 
     self.moving_left = False 
     self.moving_down = False 
     self.moving_up = False 

    def update(self): 
     """Update the ship's postion based on the movement flag""" 
     # Update the ship's center value, not the rect. 
     if self.moving_right and self.rect.right < self.screen_rect.right: 
      self.centerx += self.ai_settings.ship_speed_factor 
     if self.moving_left and self.rect.left > 0: 
      self.centerx -= self.ai_settings.ship_speed_factor 
     if self.moving_down and self.rect.bottom < self.screen_rect.bottom: 
      self.centery += self.ai_settings.ship_speed_factor 
     if self.moving_up and self.rect.top > self.screen_rect.top: 
      self.centery -= self.ai_settings.ship_speed_factor 

     # Update rect object from self.center - might need to be changed now to handle 
     # movement in two directions properly. 
     if self.moving_up or self.moving_down: 
      self.rect.centery = self.centery 
     if self.moving_left or self.moving_right: 
      self.rect.centerx = self.centerx 

    def blitme(self): 
     """Draw the ship at its current location""" 
     self.screen.blit(self.image, self.rect) 

alien_invasion.py

import sys 
import pygame 

from settings import Settings 
from ship import Ship 
import game_functions as gf 

def run_game(): 
    #Initialize pygame, settings, and screen object 
    pygame.init() 
    ai_settings = Settings() 
    screen = pygame.display.set_mode(
     (ai_settings.screen_width, ai_settings.screen_height)) 
    pygame.display.set_caption("Alien Invasion") 

    #Draw the ship 
    ship = Ship(ai_settings, screen) 

    #Start the main loop for the game. 
    while True: 
     #Watch for keyboard and mouse events 
     gf.check_events(ship) 
     ship.update() 
     gf.update_screen(ai_settings, screen, ship) 


run_game() 

実は、私がしたすべては、私が説明したすべての変更を実装し、レイアウト/構文でいくつかの基本的なエラーを排除することでした。赤いボタンをクリックすると、グラフィックスキャンバスを閉じるためのより良い終了関数が追加されていることに注意してください。

このソリューションもうまくいく場合は、答えとしてマークして、スタックフローで十分な評判を得ることができます。

関連する問題