2017-01-29 3 views
0

EDITを更新していない:私はそれを固定している、私は宇宙の侵略者のゲームを作っていますinvader_position_xpygameのスプライトはすぐ

のリターンのインフロント自己があるはずです。しかし、私は奇妙な行動に遭遇しました。

キーボードの右のキーを押すと、スプライトは移動しませんが、数秒間押し続けると右の境界にジャンプします。

スペースインベーダーゲームは、クラスを使用しており、これはメインクラスです:

# IMPORTS 
import pygame, sys 
from pygame.locals import * 
import invader 

pygame.init() 
################### 
#IMAGE SIZE AND FPS 
width = 800 
height = 600 
fps = 30 
fpsClock = pygame.time.Clock() 
DISPLAY = pygame.display.set_mode((width,height)) 
################# 

#OTHER VARIABLES 
pygame.display.set_caption('space invaders!') 
white = (225,225,225)# The colour white 
invader_sprite = pygame.image.load("cross.png") 
invader_lenght = 40 
invader_position_x = 400 
invader_position_y = 560 
right_boundary = width- invader_lenght 

keypress = "" 
my_invader = invader.Invader(invader_position_x,right_boundary,keypress)# Initialising invader 

while True: # main game loop 
    DISPLAY.fill(white) 
    DISPLAY.blit(invader_sprite,(invader_position_x,invader_position_y)) 
    keypress = pygame.key.get_pressed() 
    invader_position_x=my_invader.invader_move(keypress,right_boundary,invader_position_x) 
    print(invader_position_x) 

    for event in pygame.event.get(): 
     if event.type == QUIT: 
      pygame.quit() 
      sys.exit() 

    pygame.display.update() 
    fpsClock.tick(fps) 

これは、別々のファイルに私のインベーダークラスは次のとおりです。

import pygame, sys 
from pygame.locals import * 

pygame.init() 

class Invader(): 

    def __init__(self,invader_position_x,right_boundary,keypress): 

     self.invader_position_x= invader_position_x   
     self.right_boundary=right_boundary 
     self.keypress= keypress 

    def invader_move(self,keypress,right_boundary,invader_position_x): 
     if self.invader_position_x> right_boundary:#Right boundary 
      invader_position_x=right_boundary-5 
     if self.invader_position_x<0: 
      invader_position_x= 5 

     if keypress[K_RIGHT]:# right 
      self.invader_position_x = self.invader_position_x+ 5 

     elif keypress[K_LEFT]:# left 
      self.invader_position_x =self.invader_position_x- 5 

     return invader_position_x 

だから、これを修正する解決策ものです奇妙な行動?

+0

私は、 'invader_position_x'と' self.invader_position_x'の間に混乱があると思います。 –

+0

私はそれを試しました。リターンは自己infrontを持っていなかった!どうもありがとう! –

+0

クラス 'Invader'を持っているなら、このクラスの中でその位置、長さ、スプライトを保持する必要があります。 – furas

答えて

0

私は問題があなたの位置を維持するために2つの変数があると思います。しかし、私はこれをチェックしなかった - 私はそれをより整理し問題が消えたようにコードを変更した。

#!/usr/bin/env python3 

import pygame 

# --- constants --- 

WIDTH = 800 
HEIGHT = 600 
FPS = 30 

WHITE = (225, 225, 225) 

# --- classes --- 

class Invader(): 

    def __init__(self, x, y): 

     self.image = pygame.image.load("cross.png") 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

    def draw(self, screen): 
     screen.blit(self.image, self.rect) 

    def update(self, screen_rect, keypress):#EDIT Replaced screen with screem_rect 

     if keypress[pygame.K_RIGHT]: # right 
      self.rect.x += 15 

     elif keypress[pygame.K_LEFT]: # left 
      self.rect.x -= 15 

     if self.rect.right > screen_rect.right: # right boundary 
      self.rect.right = screen_rect.right 

     if self.rect.left < screen_rect.left: 
      self.rect.left = screen_rect.left 

# --- main --- 

# - init - 

pygame.init() 

screen = pygame.display.set_mode((WIDTH, HEIGHT)) 
screen_rect = screen.get_rect() 

pygame.display.set_caption('space invaders!') 

# - objects - 

my_invader = Invader(400, 560) 

# - mainloop - 

fps_clock = pygame.time.Clock() 

while True: # main game loop 

    # - events - 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit() 
      quit() 

    keypress = pygame.key.get_pressed() 

    # - updates - 

    my_invader.update(screen_rect, keypress) 

    # - draws - 

    screen.fill(WHITE) 
    my_invader.draw(screen) 
    pygame.display.update() 

    # - FPS - 

    fps_clock.tick(FPS) 
+0

あなたのご意見ありがとうございます。私はこれを後で試してみます。直腸が何をするのか聞いてもよろしいですか? –

+0

[pygame.Rect()](http://pygame.org/docs/ref/rect.html)はサイズと位置を保持し、 'x'、' y'を持ちます。 'width'、' height'だけでなく、 'left'、' right'、 'top'、' bottom'、 'center'など(' center'を使って画面上にオブジェクトを配置したり、 (他の矩形との)衝突、「衝突点」(ポイント、つまりクリック可能なボタンを作るためのマウスの位置)などをチェックする方法があります。 – furas

関連する問題