如何在Pygame中重新启动游戏

时间:2019-05-28 13:44:52

标签: python pygame

我正在开发射击游戏。每当我在游戏结束时尝试重新启动游戏时,它都会卡在位置上,什么也没有发生,我尝试了此方法-How do I insert a restart game option?但没有解决

每当敌人击中英雄时,它都会调用startGame函数,该函数必须重新启动游戏,但不会重启

任何人都可以帮助我重新启动整个游戏

这是我的代码,

thePlayer Class:

# Delcaring thePlayer class which contains data about player(hero)
class thePlayer(pygame.sprite.Sprite):
heroWalkRight =[pygame.image.load('HeroWalkRight/PenRunR0.png')....
hero_Death = [pygame.image.load('hero_death/Armature_DEAD_00.png')..
              pygame.image.load('hero_death/Armature_DEAD_32.png')]

heroShootR = [pygame.image.load('HeroShootR/shot0.png')...pygame.image.load('HeroShootR/shot7.png')]

heroShootL = [pygame.image.load('HeroShootL/shot0.png')....pygame.image.load('HeroShootL/shot7.png')]

hero_charR = pygame.image.load('HeroStandChar/PStandStillR.png')
hero_charL = pygame.image.load('HeroStandChar/PStandStillL.png')

def __init__(self, x, y, width, height):
    super().__init__()
    self.x = x
    self.y = y
    self.width = width
    self.height = height
    self.velocity = 5
    self.isJump = False
    self.jumpCount = 10
    self.left = False
    self.right = False
    self.shoot = False
    self.walkCount = 0
    self.shootCount = 0
    self.standing = True
    self.hitbox = (self.x + 20, self.y, 28, 60)
    self.deathCount = 0
    self.gameOver = False

def draw(self, window):
    if self.walkCount + 1 >= 15:
        self.walkCount = 0

    if self.shootCount + 1 >= 7:
        self.shootCount = 0

    if not self.standing:
        if self.left:
            window.blit(self.heroWalkLeft[self.walkCount], (self.x, self.y))
            self.walkCount += 1

        elif self.right:
            window.blit(self.heroWalkRight[self.walkCount], (self.x, self.y))
            self.walkCount += 1

    else:
        if self.left:
            if self.shoot:
                window.blit(self.heroShootL[self.shootCount], (self.x, self.y))
                self.shootCount += 1
            else:
                window.blit(self.hero_charL, (self.x, self.y))
        elif self.right:
            if self.shoot:
                window.blit(self.heroShootR[self.shootCount], (self.x, self.y))
                self.shootCount += 1
            else:
                window.blit(self.hero_charR, (self.x, self.y))

    self.hitbox = (self.x + 20, self.y, 55, 80)
    # pygame.draw.rect(win, (0, 0, 0), self.hitbox, 2)
    # pygame.draw.rect(win, (0, 0, 0), self.hitbox, 2)

def restart(self):
    startGame()

主循环

def redrawGameWindow(CameraX):
# This statement continuosly keep drawing background image according to 
the camera value
win.blit(bg, (0 - CameraX, 0 - CameraY))

text = font.render('Score: ' + str(score), 1, (0, 0, 0))
win.blit(text, (390, 10))

# Drawing hero and enemies on Screen
hero.draw(win)
enemy2.draw(win)
enemy.draw(win)

for bullet in bullets:
    bullet.draw(win)

pygame.display.flip()

pygame.display.set_caption("First Game")
bg = pygame.image.load('LongBGx8.png')
bgWidth, bgHeight = bg.get_rect().size
win = pygame.display.set_mode((928, bgHeight))
startScrollingPos = bgWidth / 28

score = 0
clock = pygame.time.Clock()
CameraY = 0

font = pygame.font.SysFont('comicsans', 30, True, True)
hero = thePlayer(45, 625, 40, 40)
enemy2 = enemy(1450, 625, 40, 40, 1900)
enemy = enemy(400, 625, 40, 40, 850)

alternateX = 0

bullets = []
run = True

def startGame():`
clock.tick(60)

noBorderCross = True
noCameraCross = True
CameraX = 0

# This statement will start reacting when player touches enemy
if hero.hitbox[1] < enemy.hitbox[1] + enemy.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > enemy.hitbox[1]:
    if hero.hitbox[0] + hero.hitbox[2] > enemy.hitbox[0] and hero.hitbox[0] < enemy.hitbox[0] + enemy.hitbox[2]:
        print("GOT HIT!")
        hero.restart()

# This statement will start reacting when player touches enemy
if hero.hitbox[1] < enemy2.hitbox[1] + enemy2.hitbox[3] and hero.hitbox[1] + hero.hitbox[3] > enemy2.hitbox[1]:
    if hero.hitbox[0] + hero.hitbox[2] > enemy2.hitbox[0] and hero.hitbox[0] < enemy2.hitbox[0] + enemy2.hitbox[2]:
        print("GOT HIT!")
        # hero.hit(win)

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

# This loop is used for detecting bullets that will hit enemy
# One statement for enemy and other one for enemy2
for bullet in bullets:
    if bullet.y - bullet.radius < enemy.hitbox[1] + enemy.hitbox[3] and bullet.y + bullet.radius > enemy.hitbox[1]:
        if bullet.x + bullet.radius > enemy.hitbox[0] and bullet.x - bullet.radius < enemy.hitbox[0] + enemy.hitbox[2]:
            enemy.hit()
            score += 1
            bullets.pop(bullets.index(bullet))

    if bullet.y - bullet.radius < enemy2.hitbox[1] + enemy2.hitbox[3] and bullet.y + bullet.radius > enemy2.hitbox[1]:
        if bullet.x + bullet.radius > enemy2.hitbox[0] and bullet.x - bullet.radius < enemy2.hitbox[0] + \
                enemy2.hitbox[2]:
            enemy2.hit()
            score += 1
            bullets.pop(bullets.index(bullet))

    # This won't let bullets travel more than screen size i.e 928
    if bullet.x < 928 and bullet.x > 0:
        bullet.x += bullet.vel
    else:
        bullets.pop(bullets.index(bullet))

keys = pygame.key.get_pressed()

if keys[pygame.K_r]:
    if hero.left:
        facing = -1
    else:
        facing = 1

    if len(bullets) < 1:
        bullets.append(projectile(round(hero.x + hero.width), round(hero.y + hero.height), 6, (0, 0, 0), facing))

if keys[pygame.K_LEFT] and hero.x > hero.velocity:
    hero.standing = False
    hero.x -= hero.velocity
    hero.left = True
    hero.right = False
    noBorderCross = True

elif keys[pygame.K_RIGHT] and hero.x < bgWidth - hero.velocity - hero.width:
    hero.standing = False

    if noBorderCross:
        hero.x += hero.velocity

    if noCameraCross:
        CameraX += hero.velocity
        enemy.path[0] -= hero.velocity
        enemy.path[1] -= hero.velocity

        enemy2.path[0] -= hero.velocity
        enemy2.path[1] -= hero.velocity

        if enemy.enemyDead:
            enemy.x += enemy.velocity
            enemy2.x += enemy2.velocity

    if hero.x >= startScrollingPos:
        noBorderCross = False

    # 6500 is Camera Position Limit - If increased Camera will behave abnormally
    if CameraX >= 6500:
        noCameraCross = False
        if hero.x < 890 - hero.velocity - hero.width:
            hero.x += hero.velocity

    hero.left = False
    hero.right = True

elif keys[pygame.K_r]:
    hero.shoot = True

else:
    hero.standing = True
    hero.shoot = False
    hero.walkCount = 0

if not hero.isJump:
    if keys[pygame.K_SPACE]:
        hero.isJump = True
        hero.walkCount = 0
else:
    if hero.jumpCount >= -10:
        hero.y -= (hero.jumpCount * abs(hero.jumpCount)) * 0.5
        hero.jumpCount -= 1
    else:
        hero.jumpCount = 10
        hero.isJump = False

redrawGameWindow(CameraX)

while run:
startGame()

pygame.quit()

0 个答案:

没有答案