2017-03-28 6 views
0

私は現在、プロジェクト用のゲームを作成する必要があるレベルのコンピュータサイエンスコースを受講しています。私はこのコードがおそらく効率的ではないことを知っていますが、私はそれを自分の仕事にとどめようとしており、私が混乱している1ビットの手が欲しいです。Pygame Scrolling

私は左右の画面をスクロールしようとしていますが、画面上のすべてのオブジェクトを適切な方向に動かすことで行われます。私はロボットがそれらの中にいるときに画面が動くはずの境界を作りました。しかし、私が書いたコードでは、画面上で更新されずにプラットフォームが動いて、ロボットの速度がこれらの境界内にあるときに変化します。

アイデアや助けがあれば幸いです。

import pygame as pg 
import time 
import random 


pg.init()#initiates pygame 

display_height = 690#Creates width and height of screen 
display_width = 1024 
#Colours 
white = (255,255,255) 
black = (0,0,0) 
red = (255,0,0) 
green = (0,255,0) 
blue = (0,0,255) 
sky = (73,71,65) # grey 

Robot_height = 99#Height of robot 
Robot_width = 112#Width of robot 
lives = 3 #Robot Lives 
Bullet_Fired = False 
PowerUp_Active = False 
Robot_acc = 0.3 #Robot Acceleration 
vec = pg.math.Vector2 

gameDisplay = pg.display.set_mode((display_width,display_height)) #Sets display properties of window 
pg.display.set_caption ("Game") #Title on window 
clock = pg.time.Clock() 
robotImg = pg.image.load("robot1.png") #Loads robots image 

#Class for platforms 
class Platform(pg.sprite.Sprite): 
    def __init__(self, x,y,w,h): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((w,h)) 
     self.image.fill(blue) 
     self.rect = self.image.get_rect() 
     self.rect.x = x 
     self.rect.y = y 

#List of platforms 
PLATFORM_LIST = [(0,display_height - 40,display_width,40), 
       (display_width /2 - 50,display_height * 3/4,100,20)] 
#Platform group 
platforms = pg.sprite.Group() 


#class for robot 
class RobotClass(pg.sprite.Sprite): 
    def __init__(self): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((Robot_width,Robot_height)) 
     self.rect = self.image.get_rect() 
     self.rect.center = (display_width/2, display_height/2) 
     self.RobotPos = vec(display_width/2, display_height/2) 
     self.bottom = (0,0) 
     self.vel = vec(0, 0) 
     self.acc = vec(0, 0.3) 

#Creates Robot 
Robot = RobotClass() 
for plat in PLATFORM_LIST: 
    p = Platform(*plat) 
    platforms.add(p) 
def draw(): 
    for plat in PLATFORM_LIST: 
      p = Platform(*plat) 
      pg.draw.rect(gameDisplay, blue, (p)) 

#Jump function 
def jump(): 
    #Checks pixel below robot to see if there is a collision 
    Robot.rect.x = Robot.rect.x +1 
    hits = pg.sprite.spritecollide(Robot , platforms, False) 
    Robot.rect.x = Robot.rect.x -1 
    if hits: 
     #Gives robot velocity of 5 upwards 
     Robot.vel.y = -10 

#game loop 
def game_loop(): 
    pg.draw.rect(gameDisplay, red, Robot.rect, 2) 
    Robot_friction = -0.3 #Friction value 
    vec = pg.math.Vector2 #Setting vec as vector quantity 
    while True: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       pg.quit 
       quit() 
      #Starts acceleration when key is pressed 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_LEFT: 
        Robot.acc.x = -Robot_acc 
       elif event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot_acc 
       elif event.key == pg.K_UP: 
        jump() 
      #Adds friction to accleration to slow robot down when key is not being pressed 
      if event.type == pg.KEYUP: 
       if event.key == pg.K_LEFT or event.key == pg.K_RIGHT: 
        Robot.acc.x = Robot.acc.x * Robot_friction 

     #Adjusts velocity of robot by adding the acceleration on each cycle 
     Robot.vel = Robot.vel+ Robot.acc 
     #Fills background 
     gameDisplay.fill(sky) 
     #Draws the platforms to the screen and adds them to platform group 
     draw() 
     #Changes Robot position according to its velocity,acceleration and the friction 
     Robot.RobotPos = Robot.RobotPos + Robot.vel + 0.5 * Robot.acc 
     #Loads robot onto screen 
     gameDisplay.blit(robotImg,(Robot.rect)) 
     pg.draw.rect(gameDisplay, red, Robot.rect, 2) 
     #Updates display 
     pg.display.update() 
     clock.tick(60) 

     #Sets bottom of robot to its position 
     Robot.rect.midbottom = Robot.RobotPos 

     #Collision detection 
     if Robot.vel.y > 0: 
      hits = pg.sprite.spritecollide(Robot , platforms, False) 
      if hits: 
       #Puts Robot on top of platform 
       Robot.RobotPos.y = hits[0].rect.top + 1 
       Robot.vel.y = 0 

     #Scrolling 

     if Robot.rect.left < display_width/4: 
      print("left") 
      Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x) 
      print(Robot.vel) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x + abs(Robot.vel.x) 
       pg.draw.rect(gameDisplay, blue, (PLATFORM_LIST[0])) 
     if Robot.rect.right > (display_width-display_width/4): 
      print("right") 
      Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 
      print(Robot.vel) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x - abs(Robot.vel.x) 

     #Sets top velocity of robot  
     if Robot.vel.x > 6: 
      Robot.vel.x = 6 
     if Robot.vel.x < -6: 
      Robot.vel.x = -6 
     #Makes robot velocity = 0 when it is close to 0 
     if Robot.vel.x < 0.05 and Robot.vel.x > -0.05: 
      Robot.acc.x = 0 
      Robot.vel.x = 0 


game_loop() 
pg.quit() 
quit() 
+0

私はこれで遊んでいますし、プラットフォームが全く動きません。私が辺縁に近づくと、ロボットは床に落ちます。間違いなくいくつかの問題。もっと見るために入るだろう。 – The4thIceman

+1

あなたの他の質問に誰かがコメントしたので、共通の命名規則に従ってみてください。 'lowercase_with_underscore'の関数、変数、インスタンス、属性の名前、' CamelCase'のクラス、 'UPPERCASE_WITH_UNDERSCORE'の定数。これは、コードを読みやすく、変数が何であるかを理解するのに役立ちます。たとえば、何かがクラスであるかどうかを調べる必要はありません。 –

答えて

1

だからここに行きます。ロボットが上限に達した場合はここでは、プラットフォームを更新している:

for plat in platforms: 
    plat.rect.x = plat.rect.x - abs(Robot.vel.x) 

しかし、とき、あなたは、元のPLATFORM_LISTリストから描くのプラットフォームを描く:

def draw(): 
    for plat in PLATFORM_LIST: 
     p = Platform(*plat) 
     pg.draw.rect(gameDisplay, blue, (p)) 

だから何が起こって終わることも、あなたかかわらず、ですプラットフォームを適切に更新している場合は、元のリストから描画しているため、更新されたリストは描画されません。あなたが更新しているプラ​​ットフォームの一覧から引き出す必要があります。

def draw(): 
    for plat in platforms: 
     pg.draw.rect(gameDisplay, blue, plat) 

第二に、私は戻って、右方向への移動がロボットに間違った道を移動し、あなたが左スクロール制限に達し一度発見しました。

if Robot.rect.left < display_width/4: 
    Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 

ゲームで遊んでいる間私が見つけただけでカップルの事:この(マイナス記号がプラス記号に切り替え)と

if Robot.rect.left < display_width/4: 
    Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x) 

:これを交換してください。

更新

丸めの問題もあります。 Pygameの長方形は整数をとる。速度のあなたの計算はフロートをもたらし、あなたがここに長方形のXにそれを追加しようとしている。

for plat in platforms: 
    plat.rect.x = plat.rect.x - abs(Robot.vel.x) 

これは独特の方法で移動するディスプレイに表示丸めの問題(プラットフォーム)を引き起こします。あなたはそれらをint型にすることができます。そうしないと、あなたがVEC()にロボットと取引を行うようにプラットフォームを加える必要があります

for plat in platforms: 
    plat.rect.x = plat.rect.x - int(abs(Robot.vel.x)) 

+0

私はプラットフォームを左右に動かす上のコードと、境界内でどのように加速するかを、「[display_width + 50、display_height * 3/4,100,20]」に別のプラットフォームを追加することで追加しました。あなたが右にスクロールすると、このプラットフォームはもう一方に近づきます。なぜどんなアイデア? –

+0

@AshleyKelly問題に対処するために私の答えを更新します。スポイラー、それは丸めと関係がある。 – The4thIceman

+0

ああ、それは働いた、もう一度ありがとう –