IT序号网

python之Pygame打开窗口并立即崩溃

lvdongjie 2024年11月24日 编程语言 27 0

我以崇高的态度编写了这款游戏,并且在同一编辑器中运行该游戏时效果很好。但是,当我直接使用Python / Python3.8运行它时,窗口会打开,然后立即自行关闭。我还尝试使用cx_Freeze将其转换为可执行文件,但是在运行.exe文件时,再次打开窗口一秒钟,然后自行关闭。请帮忙!

PS:错误仅在我跳后才开始出现

编辑-我降级到python 3.7.7并安装pygame 2.0.0dev6后,错误消失了,但是当我直接使用python打开它时,它仍然立即崩溃。

import pygame 
pygame.init() 
 
win = pygame.display.set_mode((600,390)) 
 
pygame.display.set_caption("First Game") 
 
walkRight = [pygame.image.load('R1.png'), pygame.image.load('R2.png'), pygame.image.load('R3.png'), pygame.image.load('R4.png'), pygame.image.load('R5.png'), pygame.image.load('R6.png'), pygame.image.load('R7.png'), pygame.image.load('R8.png'), pygame.image.load('R9.png')] 
walkLeft = [pygame.image.load('L1.png'), pygame.image.load('L2.png'), pygame.image.load('L3.png'), pygame.image.load('L4.png'), pygame.image.load('L5.png'), pygame.image.load('L6.png'), pygame.image.load('L7.png'), pygame.image.load('L8.png'), pygame.image.load('L9.png')] 
bg = pygame.image.load('dungeon.jpg') 
char = pygame.image.load('standing.png') 
 
clock = pygame.time.Clock() 
 
bulletSound=pygame.mixer.Sound('bullet.wav') 
hitSound=pygame.mixer.Sound('hit.wav') 
music=pygame.mixer.Sound('music.wav') 
music.set_volume(0.3) 
music.play(-1) 
 
class player(object): 
    def __init__(self,x,y,width,height): 
        self.x = x 
        self.y = y 
        self.width = width 
        self.height = height 
        self.vel = 5 
        self.isJump = False 
        self.left = False 
        self.right = False 
        self.walkCount = 0 
        self.jumpCount = 10 
        self.standing = True 
        self.hitbox=(self.x+20,self.y+12,24,50) 
        self.alive=True 
 
    def draw(self, win): 
        if self.walkCount== 27: 
            self.walkCount = 0 
 
        if not(self.standing): 
            if self.left: 
                win.blit(walkLeft[self.walkCount//3], (self.x,self.y)) 
                self.walkCount += 1 
            elif self.right: 
                win.blit(walkRight[self.walkCount//3], (self.x,self.y)) 
                self.walkCount +=1 
        else: 
            if self.right: 
                win.blit(walkRight[0], (self.x, self.y)) 
            else: 
                win.blit(walkLeft[0], (self.x, self.y)) 
        self.hitbox=(self.x+20,self.y+12,24,50) 
        #pygame.draw.rect(win,(255,0,0),self.hitbox,2) 
 
    def hit(self): 
        self.alive=False 
        Font=pygame.font.SysFont('comicsans',100,True) 
        Font2=pygame.font.SysFont('comicsans',40) 
        text2=Font2.render('Press Enter to restart', 1 , (255,0,0)) 
        text=Font.render('GAME OVER', 1, (255,255,255)) 
        win.blit(text,(250-(text.get_width()/2),100)) 
        win.blit(text2,(250-(text.get_width()/2),200)) 
        pygame.display.update() 
        while not self.alive: 
            pygame.time.delay(10) 
            key=pygame.key.get_pressed() 
            if key[pygame.K_RETURN]: 
                restart() 
            for event in pygame.event.get(): 
                if event.type == pygame.QUIT: 
                    dead=False 
                    pygame.quit() 
 
 
 
class projectile(object): 
    def __init__(self,x,y,radius,color,facing): 
        self.x = x 
        self.y = y 
        self.radius = radius 
        self.color = color 
        self.facing = facing 
        self.vel = 8 * facing 
 
    def draw(self,win): 
        pygame.draw.circle(win, self.color, (self.x,self.y), self.radius) 
 
class enemy(object): 
    walkRight = [pygame.image.load('R1E.png'), pygame.image.load('R2E.png'), pygame.image.load('R3E.png'), pygame.image.load('R4E.png'), pygame.image.load('R5E.png'), pygame.image.load('R6E.png'), pygame.image.load('R7E.png'), pygame.image.load('R8E.png'), pygame.image.load('R9E.png'), pygame.image.load('R10E.png'), pygame.image.load('R11E.png')] 
    walkLeft = [pygame.image.load('L1E.png'), pygame.image.load('L2E.png'), pygame.image.load('L3E.png'), pygame.image.load('L4E.png'), pygame.image.load('L5E.png'), pygame.image.load('L6E.png'), pygame.image.load('L7E.png'), pygame.image.load('L8E.png'), pygame.image.load('L9E.png'), pygame.image.load('L10E.png'), pygame.image.load('L11E.png')] 
    def __init__(self,x,y,height,width,end): 
        self.x=x 
        self.y=y 
        self.width=width 
        self.height=height 
        self.end=end 
        self.path=[0,self.end] 
        self.walkCount=0 
        self.vel=3  
        self.hitbox=(self.x+17,self.y+2,28,57) 
        self.hitNumber=0 
        self.levelupScore = 10 
 
    def draw(self,win): 
        self.move() 
        if self.walkCount==33: 
            self.walkCount=0 
        if self.vel > 0: 
            win.blit(self.walkRight[self.walkCount//3],(self.x,self.y)) 
            self.walkCount += 1 
        else: 
             win.blit(self.walkLeft[self.walkCount//3],(self.x,self.y)) 
             self.walkCount += 1 
        if self.vel > 0: 
            self.hitbox=(self.x+15,self.y+2,25,57) 
        else: 
            self.hitbox=(self.x+25,self.y+2,25,57) 
 
        #pygame.draw.rect(win,(255,0,0),self.hitbox,2) 
 
    def move(self): 
        if self.vel>0: 
            if (self.x+self.vel)<self.path[1]: 
                self.x+=self.vel 
            else: 
                self.vel=self.vel * -1 
                self.walkcount=0 
        else: 
            if self.x-self.vel>self.path[0]: 
                self.x += self.vel 
            else: 
                self.vel=self.vel * -1 
                self.walkcount=0 
 
    def hit(self): 
        self.hitNumber+=1 
        if self.hitNumber==self.levelupScore: 
            self.levelupScore += 10 
            if self.vel > 0:    
                self.vel += 2 
            else: 
                self.vel -= 2    
 
def start(): 
    global run 
    start=True 
    while start: 
        clock.tick(27) 
        win.blit(bg,(0,0)) 
        font=pygame.font.SysFont('comicsans',80) 
        text=font.render('Goblin Dungeon', 1, (255,255,255)) 
        win.blit(text,(100,20)) 
        font2=pygame.font.SysFont('comicsans',40) 
        text2=font2.render('Press Enter to start', 1, (255,0,0))   
        win.blit(text2,(200,350))  
        pygame.display.update() 
        for event in pygame.event.get(): 
            if event.type == pygame.QUIT: 
                start = False 
                break 
                pygame.quit() 
        keys = pygame.key.get_pressed() 
        if keys[pygame.K_RETURN]: 
            run=True 
            return 
 
def redrawGameWindow(): 
    score=goblin.hitNumber + goblin2.hitNumber 
    Font=pygame.font.SysFont('comicsans',30,True) 
    text=Font.render("Score: "+ str(score),1,(255,255,255)) 
    win.blit(bg, (0,0)) 
    win.blit(text,(450,10)) 
    man.draw(win) 
    goblin.draw(win) 
    goblin2.draw(win) 
 
    for bullet in bullets: 
        bullet.draw(win) 
 
    pygame.display.update() 
 
def restart(): 
    man.alive = True 
    man.x = 250 
    goblin.x, goblin.vel, goblin.hitNumber = 0, 3 , 0 
    goblin2.x, goblin2.vel, goblin2.hitNumber = 400, 3, 0 
 
 
 
#mainloop 
man = player(250, 230, 64,64) 
goblin=enemy(0,230,64,64,550) 
goblin2=enemy(400,230,64,64,550) 
shootDelay=0 
bullets = [] 
run = False 
start() 
while run: 
    clock.tick(27) 
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            run = False 
            break 
 
    if man.hitbox[1]<goblin.hitbox[1]+goblin.hitbox[3] and man.hitbox[1] + man.hitbox[3]>goblin.hitbox[1]: 
        if man.hitbox[0] + man.hitbox[2] > goblin.hitbox[0] and man.hitbox[0] < goblin.hitbox[0] + goblin.hitbox[2]: 
            man.hit() 
 
    if man.hitbox[1]<goblin2.hitbox[1]+goblin2.hitbox[3] and man.hitbox[1] + man.hitbox[3]>goblin2.hitbox[1]: 
        if man.hitbox[0] + man.hitbox[2] > goblin2.hitbox[0] and man.hitbox[0] < goblin2.hitbox[0] + goblin2.hitbox[2]: 
            man.hit() 
 
    for bullet in bullets: 
        if bullet.y-bullet.radius>=goblin.hitbox[1] and bullet.y+bullet.radius<goblin.hitbox[1]+goblin.hitbox[3]: 
            if bullet.x+bullet.radius>goblin.hitbox[0] and bullet.x-bullet.radius<goblin.hitbox[0]+goblin.hitbox[2]: 
                goblin.hit() 
                hitSound.play() 
                bullets.remove(bullet) 
                print("Goblin1- ",goblin.hitNumber, "vel = ", goblin.vel) 
 
 
            elif bullet.y-bullet.radius>=goblin2.hitbox[1] and bullet.y+bullet.radius<goblin2.hitbox[1]+goblin2.hitbox[3]: 
                if bullet.x+bullet.radius>goblin2.hitbox[0] and bullet.x-bullet.radius<goblin2.hitbox[0]+goblin2.hitbox[2]: 
                    goblin2.hit() 
                    hitSound.play() 
                    bullets.remove(bullet) 
                    print("Goblin2- ",goblin2.hitNumber, " vel = ", goblin2.vel) 
 
        if bullet.x < 600 and bullet.x > 0: 
            bullet.x += bullet.vel 
        else: 
            bullets.remove(bullet) 
 
    keys = pygame.key.get_pressed() 
 
    if man.alive: 
        if shootDelay>0: 
            shootDelay+=1 
        if shootDelay==5: 
            shootDelay=0 
 
        if keys[pygame.K_SPACE] and shootDelay==0: 
            bulletSound.play() 
 
            if man.left: 
                facing = -1 
            else: 
                facing = 1 
 
            if len(bullets) < 5: 
                bullets.append(projectile(round(man.x + man.width //2), round(man.y + man.height//2), 5, (0,0,255), facing)) 
                shootDelay+=1 
 
        if keys[pygame.K_LEFT] and man.x > man.vel: 
            man.x -= man.vel 
            man.left = True 
            man.right = False 
            man.standing = False 
        elif keys[pygame.K_RIGHT] and man.x < 620 - man.width - man.vel: 
            man.x += man.vel 
            man.right = True 
            man.left = False 
            man.standing = False 
        else: 
            man.standing = True 
            man.walkCount = 0 
 
        if not(man.isJump): 
            if keys[pygame.K_UP]: 
                man.isJump = True 
 
                man.walkCount = 0 
        else: 
            if man.jumpCount >= -10: 
                neg = 1 
                if man.jumpCount < 0: 
                    neg = -1 
                man.y -= (man.jumpCount ** 2) * 0.3 * neg 
                man.jumpCount -= 1 
            else: 
                man.isJump = False 
                man.jumpCount = 10 
    if not man.alive: 
        man.standing=True 
 
 
    redrawGameWindow() 
 
pygame.quit() 
 
 

这些都是我在IDLE中遇到的错误,即使游戏运行正常
Python 3.8.2 (tags/v3.8.2:7b3ab59, Feb 25 2020, 22:45:29) [MSC v.1916 32 bit (Intel)] on win32 
Type "help", "copyright", "credits" or "license()" for more information. 
>>>  
= RESTART: C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py 
pygame 1.9.6 
Hello from the pygame community. https://www.pygame.org/contribute.html 
 
Warning (from warnings module): 
  File "C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py", line 43 
    win.blit(walkLeft[self.walkCount//3], (self.x,self.y)) 
DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python. 
 
Warning (from warnings module): 
  File "C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py", line 52 
    win.blit(walkLeft[0], (self.x, self.y)) 
DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python. 
 
Warning (from warnings module): 
  File "C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py", line 46 
    win.blit(walkRight[self.walkCount//3], (self.x,self.y)) 
DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python. 
 
Warning (from warnings module): 
  File "C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py", line 50 
    win.blit(walkRight[0], (self.x, self.y)) 
DeprecationWarning: an integer is required (got type float).  Implicit conversion to integers using __int__ is deprecated, and may be removed in a future version of Python. 
Traceback (most recent call last): 
  File "C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py", line 209, in <module> 
    man.hit() 
  File "C:\Users\manuv\Documents\Python Scripts\Goblin Dungeon\Goblin Dungeon.py", line 67, in hit 
    key=pygame.key.get_pressed() 
pygame.error: video system not initialized 
>>>  

请您参考如下方法:

您是否尝试在python(shell)随附的IDLE中打开它?右键单击您的.py文件,选择使用IDLE编辑。出现IDLE,然后按F5键运行代码。它应该在控制台上记录您的错误。

编辑:
看到错误日志,可能是因为您使用的是python 3.8,请在3.7或更早版本上尝试


评论关闭
IT序号网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!