Python 如何在Pygame中重新启动游戏

Python 如何在Pygame中重新启动游戏,python,pygame,Python,Pygame,我正在制作一款射击游戏。当比赛结束后,每当我试图重新开始比赛时,它就会卡在原地,什么也没发生。我试过了,但没有成功 每当敌人击中英雄,它就会调用startGame函数,该函数必须重新启动游戏,但不会。如何重新启动整个游戏 这是我的密码: 播放器类: # Delcaring thePlayer class which contains data about player(hero) class thePlayer(pygame.sprite.Sprite): heroWalkRight =[pyg

我正在制作一款射击游戏。当比赛结束后,每当我试图重新开始比赛时,它就会卡在原地,什么也没发生。我试过了,但没有成功

每当敌人击中英雄,它就会调用
startGame
函数,该函数必须重新启动游戏,但不会。如何重新启动整个游戏

这是我的密码:

播放器
类:

# 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()
def重画游戏窗口(CameraX):
#本声明持续根据以下内容绘制背景图像:
摄影机值
win.blit(背景,(0-CameraX,0-CameraY))
text=font.render('Score:'+str(Score),1,(0,0,0))
win.blit(文本,(390,10))
#在屏幕上画英雄和敌人
英雄。平局(胜利)
Enemy 2.平局(获胜)
敌人。平局(胜利)
对于子弹中的子弹:
子弹。抽签(赢)
pygame.display.flip()
pygame.display.set_标题(“第一场游戏”)
bg=pygame.image.load('LongBGx8.png')
bgWidth,bgHeight=bg.get_rect().size
win=pygame.display.set_模式((928,bgHeight))
开始滚动位置=bg宽度/28
分数=0
clock=pygame.time.clock()
摄像机=0
font=pygame.font.SysFont('comicsans',30,True,True)
英雄=玩家(45625,40,40)
敌人2=敌人(1450625,40,401900)
敌人=敌人(400625,40,40850)
alternateX=0
项目符号=[]
运行=真
def startGame():`
时钟滴答(60)
noBorderCross=True
noCameraCross=真
CameraX=0
#当玩家触碰敌人时,此声明将开始反应
如果英雄.hitbox[1]<敌人.hitbox[1]+敌人.hitbox[3]和英雄.hitbox[1]+英雄.hitbox[3]>敌人.hitbox[1]:
如果英雄.hitbox[0]+英雄.hitbox[2]>敌人.hitbox[0]和英雄.hitbox[0]<敌人.hitbox[0]+敌人.hitbox[2]:
打印(“被击中了!”)
hero.restart()
#当玩家触碰敌人时,此声明将开始反应
如果hero.hitbox[1]enemy2.hitbox[1]:
如果hero.hitbox[0]+hero.hitbox[2]>enemy2.hitbox[0]和hero.hitbox[0]敌方.hitbox[1]:
如果bullet.x+bullet.radius>敌方.hitbox[0]和bullet.x-bullet.radius<敌方.hitbox[0]+敌方.hitbox[2]:
敌人
分数+=1
子弹.弹孔(子弹.索引(子弹))
如果bullet.y-bullet.radiusenemy2.hitbox[1]:
如果bullet.x+bullet.radius>enemy2.hitbox[0]和bullet.x-bullet.radius0:
bullet.x+=bullet.vel
其他:
子弹.弹孔(子弹.索引(子弹))
keys=pygame.key.get_pressed()
如果键[pygame.K_r]:
如果hero.left:
正面=-1
其他:
朝向=1
如果长度(项目符号)<1:
子弹。附加(投射物(圆形(英雄x+英雄宽度),圆形(英雄y+英雄高度),6,(0,0,0),正面))
如果键[pygame.K_左]和hero.x>hero.velocity:
英雄站着=虚假
hero.x-=hero.velocity
hero.left=正确
正确=错误
noBorderCross=True
elif键[pygame.K_RIGHT]和hero.x=开始滚动位置:
noBorderCross=False
#6500是摄像头位置限制-如果增加,摄像头将异常运行
如果CameraX>=6500:
noCameraCross=假
如果hero.x<890-hero.velocity-hero.width:
hero.x+=hero.velocity
hero.left=False
正确的
elif keys[pygame.K_r]:
hero.shot=True
其他:
英雄站着=真的
hero.shot=False
hero.walkCount=0
如果不是hero.isJump:
如果键[pygame.K_SPACE]:
hero.isJump=True
hero.walkCount=0
其他:
如果hero.jumpCount>=-10:
hero.y-=(hero.jumpCount*abs(hero.jumpCount))*0.5
hero.jumpCount-=1
其他:
hero.jumpCount=10
hero.isJump=False
重画游戏窗口(CameraX)
运行时:
startGame()
pygame.quit()

这是我最后一年的项目,我使用pygame是因为我想成为pythondeveloper@DaduKhan你的评论与手头的问题无关。我投票结束这个问题,因为不清楚被问到什么,也没有。这是我最后一年的项目,我使用pygame是因为我想成为pythondeveloper@DaduKhan你的评论与手头的问题无关。我投票结束这个问题,因为不清楚被问到什么,也没有答案。