重启游戏(重置滚动屏幕)

时间:2017-04-30 13:25:03

标签: python scroll pygame

我的游戏出现问题,当我尝试从屏幕上的游戏重新启动游戏时,它会将机器人(玩家)放回到其起始位置,但滚动屏幕仍处于相同位置

是否有一种简单的方法可以重置屏幕位置,还是需要重绘所有原始平台。

以下是正在使用的代码的主要部分。我没有包含重新启动游戏的代码,但它只是运行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()

1 个答案:

答案 0 :(得分:1)

您只需重置在游戏过程中更改的主函数(和全局变量)中的所有值以重新启动它。我有一个通用的例子,我只调用initialize函数创建一个新的精灵组和一个玩家精灵(用d键移动玩家rect并用r键重置游戏)再次调用initialize

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()
相关问题