2017-04-30 6 views
1

私は自分のゲームに問題があります。画面上のゲームからゲームを再開しようとすると、ロボット(プレイヤー)はその開始位置に戻りますが、スクロールされた画面はまだ同じ位置ゲームを再開する(スクロールした画面をリセットする)

簡単な方法で画面の位置をリセットするか、元のプラットフォームをすべて再描画する必要がありますか?

ここには、使用されているコードの主要部分があります。私はゲームを再起動して実行するコードが含まれていないが、それは単に(game_loopを実行します)任意のヘルプ

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) 
grass = (24,85,36) 
yellow = (255,255,0) 
lightGrey = (184,184,184) 
grey = (73,71,65) 

Robot_height = 99#Height of robot 
Robot_width = 112#Width of robot 
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() 

#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))#sets width and height of platform 
     self.image.fill(grass)#Fills rectangle with blue 
     self.rect = self.image.get_rect()#Rectangle set 
     self.rect.x = x#x position 
     self.rect.y = y#y position 

#List of platforms x , y , width , height 
PLATFORM_LIST = [[0,display_height - 40,2000,40], 
       [2300,display_height - 40,1000,40], 
       ] 
#Platform group 
platforms = pg.sprite.Group() 

#Checks through "PLATFORM_LIST" and adds all the platforms the the grounp "platforms" 
for plat in PLATFORM_LIST: 
    p = Platform(*plat) 
    platforms.add(p) 

#Draws platforms to the screen 
def draw(): 
    for plat in platforms: 
     pg.draw.rect(gameDisplay, grass, plat) 

#Class for robot 
class RobotClass(pg.sprite.Sprite): 
    def __init__(self): 
     pg.sprite.Sprite.__init__(self) 
     self.image = pg.Surface((Robot_width,Robot_height))#Height and width of the robot 
     self.rect = self.image.get_rect()#Gets rectangle of robot 
     self.rect.center = (display_width/2, display_height/2)#Location of center of robot 
     self.RobotPos = vec(display_width/2, display_height/2)#Position of robot as vector 
     self.bottom = (0,0)#Bottom of robot 
     self.vel = vec(0, 0)#Robots velocity 
     self.acc = vec(0, 0.3)#Robots Acceleration 

startX = display_width/2 
startY = display_height/2 

#Creates Robot 
Robot = RobotClass() 

#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 

def game_loop(): 
    global PLATFORM_LIST 
    global startX 
    global startY 
    global backgroundImg 
    Robot.RobotPos = (startX,startY) 
    score = 0 #Score 
    lives = 3 #Robot Lives 
    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 
     #gameDisplay.fill(sky) 
     gameDisplay.blit(backgroundImg,(0,0)) 
     #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 
     display_lives(lives) 
     display_score(score)#lives 

     #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: 
      Robot.RobotPos.x = Robot.RobotPos.x + abs(Robot.vel.x) 
      startX = startX + abs(Robot.vel.x) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x + int(abs(Robot.vel.x)) 
     if Robot.rect.right > (display_width-display_width/4): 
      Robot.RobotPos.x = Robot.RobotPos.x - abs(Robot.vel.x) 
      startX = startX - abs(Robot.vel.x) 
      for plat in platforms: 
       plat.rect.x = plat.rect.x - int(abs(Robot.vel.x)) 

     draw() 
     #Losing a life 
     if Robot.rect.top > display_height: 
      lives = lives - 1 
      Robot.RobotPos.y = Robot.RobotPos.y - (40+Robot_height) 
      Robot.RobotPos.x = Robot.RobotPos.x - 200 
      Robot.vel.x = 0 


     #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 

     #Draws the platforms to the screen and adds them to platform group 
     pg.display.update()#Updates display 
     clock.tick(60) 

game_intro() 
pg.quit() 
quit() 
+0

なぜそれを再描画しないのですか? –

+0

画面がスクロールされているときに、プラットフォームのすべての値が調整されているので、理論的には最後に最後の位置に再描画されます –

+0

すべてのスプライトを画面から削除する方法はありますか? –

答えて

1

ため

おかげであなただけの主な機能(およびグローバル)のすべての値をリセットするために持っていますゲーム中に再起動するように変更されました。私はという新しいスプライトグループとプレーヤースプライトを作成する関数を呼び出します(dキーでプレーヤの矩形を移動し、initializeを再度呼び出すrキーでゲームをリセットします)。 )。

import sys 
import pygame as pg 


class Player(pg.sprite.Sprite): 

    def __init__(self, pos, *groups): 
     super().__init__(groups) 
     self.image = pg.Surface((40, 70)) 
     self.image.fill(pg.Color('royalblue')) 
     self.rect = self.image.get_rect(center=pos) 
     self.vel = pg.math.Vector2(0, 0) 
     self.pos = pg.math.Vector2(pos) 

    def update(self): 
     self.pos += self.vel 
     self.rect.center = self.pos 


def initialize(): 
    sprite_group = pg.sprite.Group() 
    player = Player((100, 300), sprite_group) 
    return sprite_group, player 


def main(): 
    screen = pg.display.set_mode((640, 480)) 
    clock = pg.time.Clock() 
    sprite_group, player = initialize() 

    done = False 

    while not done: 
     for event in pg.event.get(): 
      if event.type == pg.QUIT: 
       done = True 
      if event.type == pg.KEYDOWN: 
       if event.key == pg.K_d: 
        player.vel.x = 5 
       if event.key == pg.K_r: 
        sprite_group, player = initialize() 
      if event.type == pg.KEYUP: 
       if event.key == pg.K_d: 
        player.vel.x = 0 

     sprite_group.update() 
     screen.fill(pg.Color('gray12')) 
     sprite_group.draw(screen) 

     pg.display.flip() 
     clock.tick(30) 


if __name__ == '__main__': 
    pg.init() 
    main() 
    pg.quit() 
    sys.exit() 
+0

ゲームに関する具体的なヘルプが必要な場合は、サンプルを更新する必要があります。そうしないと、何が起こっているのかわからなくなります。 – skrx

+0

答えをありがとう、私は理解し、私のゲームにそれを実装するのに役立ちました。もう一度ありがとうございます –

+0

ようこそ。 – skrx

関連する問題