Python IDLE游戏不会运行并显示SHELL:RESTART消息

时间:2017-07-30 17:52:51

标签: python macos python-idle

当我运行时,它说:

===============RESTART: Volumes/NONAME/myuser/finalProject.py===============

===============================RESTART: Shell===============================

它刚刚赢了。

这是代码:

import pygame
import sys
import random
import math
from timeit import default_timer as timer
from pygame.locals import *
from Platform import Platform
from Player import Player
from Coin import Coin
from fireBall import fireBall
import time

pygame.init()
main_clock = pygame.time.Clock()

screen = pygame.display.set_mode((640, 460))
background = pygame.image.load('Images/cave.png')
screen.fill((255, 255, 255))
pygame.display.set_caption('Ninja Cave Escape')
screen.blit(background, background.get_rect())
font = pygame.font.SysFont(None, 36)

rndIntx = random.randint(1, 340)
rndInty = random.randint(1, 460)

x = 50
y = 50
speed = 5

lives = 10
score = 0


move_right = False
move_left = False

start = False
gameOver = False

def draw_text(display_string, font, surface, x, y, color):
    text_display = font.render(display_string, 1, color)
    text_rect = text_display.get_rect()
    text_rect.topleft = (x, y)
    surface.blit(text_display, text_rect)

    jump_state = 0
    jump_timer = 0
    grounded = False
    hasCollided = False
    time1 = timer()

while True:
    for event in pygame.event.get():
        if event.type == QUIT:
            pygame.quit()
            sys.exit()
        if event.type == KEYDOWN:
            if event.key == K_SPACE:
                if jump_state == 0 and grounded:
                    jump_state = 1
            if event.key == K_RETURN:
                if start:
                    gameOver = False
                    start = False
                else:
                    start = True
            if event.key == K_a:
                move_left = True
                move_right = False
            if event.key == K_d:
                move_left = False
                move_right = True
        if event.type == KEYUP:
            if event.key == K_a:
                move_left = False
            if event.key == K_d:
                move_right = False

    if not start:
        platforms = pygame.sprite.Group()
        platforms1 = []
        platformS = Platform(100, 350, 250, 25)
        platforms.add(platformS)
        platforms1.append(platformS.rect)
        platform1 = Platform(200, 200, 250, 25)
        platforms.add(platform1)
        platforms1.append(platform1.rect)
        platform2 = Platform(300, 280, 250, 25)
        platforms.add(platform2)
        platforms1.append(platform2.rect)
        platform3 = Platform(500, 420, 250, 25)
        platform3.image = pygame.image.load('Images/lava.png').convert_alpha()
        platforms.add(platform3)
        platforms1.append(platform3.rect)
        platform4 = Platform(600, 350, 250, 25)
        platforms.add(platform4)
        platforms1.append(platform4.rect)
        platform5 = Platform(640, 250, 250, 25)
        platforms.add(platform5)
        platforms1.append(platform5.rect)

        coins = []
        coin_group = pygame.sprite.Group()
        coin1 = Coin(200, 250)
        coin_group.add(coin1)
        coins.append(coin1.rect)
        coin2 = Coin(400, 200)
        coin_group.add(coin2)
        coins.append(coin2.rect)
        coin3 = Coin(350, 400)
        coin_group.add(coin3)
        coins.append(coin3.rect)

        fBalls_group = pygame.sprite.Group()
        fBalls1 = fireBall(640, 110)
        fBalls_group.add(fBalls1)
        fBalls3 = fireBall(640, 280)
        fBalls_group.add(fBalls3)

        player = Player(x, y)

        draw_group = pygame.sprite.Group()
        draw_group.add(platformS)
        draw_group.add(platform1)
        draw_group.add(platform2)
        draw_group.add(platform3)
        draw_group.add(platform4)
        draw_group.add(platform5)
        draw_group.add(player)
        gameOver = False
        screen.fill((255, 255, 255))
        player.rect.x = 50
        player.rect.y = 50
        x = 50
        y = 50
        speed = 5

        lives = 10
        score = 0
        timer()

        move_right = False
        move_left = False

        start = False
        gameOver = False
        jump_state = 0
        jump_timer = 0
        grounded = False
        hasCollided = False
        draw_text('Welcome to Ninja Cave Escape!', font, screen, 150, 20,  (255, 0, 0))
        draw_text('If you touch the lava, you will lose a life', font, screen, 100, 80, (0, 255, 0))
        draw_text('If the fireball hits you, you lose a life', font, screen, 100, 150, (0, 255, 0))
        draw_text('Collect Coins to score! And your ninja can also', font, screen, 10, 220, (0, 0, 255))
        draw_text('climb onto platforms by pressing SPACE under them', font, screen, 10, 250, (0, 0, 255))
        draw_text('then pressing SPACE again to go on top.', font, screen, 30, 280, (0, 0, 255))
        draw_text('You can press ENTER any time in the game to restart', font, screen, 10, 310, (0, 0, 255))
        draw_text('Press ENTER to start', font, screen, 200, 410, (0,0,0))
    elif start == True and not gameOver:
        screen.fill((255, 255, 255))
        screen.blit(background, background.get_rect())
        #print(time.time())
        timeString = "%.2f" % round(timer(), 2)
        #if timeString[-2:] == '00' or timeString[-2:]== '01':
            #score += 1
        draw_text('Time Elapsed: ' + timeString, font, screen, 400, 10, (255,255,255))
        draw_text('Score: ' + str(score), font, screen, 10, 10, (255,255,255))
        draw_text('Lives: ' + str(lives), font, screen, 10, 40, (255,255,255))
        if move_left:   
            player.rect.x -= speed
        if move_right:
            player.rect.x += speed
        #if score > 100 and len(coin_group) == 2:
     #       coin_group.add(coin1)
        for coin in coin_group:
            if player.rect.colliderect(coin.rect):
                score += 100
                coin_group.remove(coin)

        main_clock.tick(50)
        if player.rect.colliderect(platform3.rect):
            if not hasCollided:
                lives -= 1
                hasCollided = True
        else:
            hasCollided = False

        if player.rect.y > 480:
            gameOver = True
        if player.rect.x < -5:
            gameOver = True

        if lives <= 0:
            gameOver = True

        if grounded and platforms1[player.rect.collidelist(platforms1)].top - 13 >= player.rect.y:
            y = platforms1[player.rect.collidelist(platforms1)].top - 13

        if jump_state == 0:
            if jump_timer == 0:
                if player.rect.collidelist(platforms1) >= 0:
                    grounded = True
                else:
                    grounded = False
                    y += 7
        elif jump_state == 1:
            jump_timer += main_clock.get_time()
            grounded = False
            y -= 7
            if jump_timer >= 200:
                jump_state = 2
        elif jump_state == 2:
            jump_timer = 0
            jump_state = 0

        platforms.draw(screen)
        draw_group.draw(screen)
        coin_group.draw(screen)
        fBalls_group.draw(screen)
        #red = random.randint(0, 3)
  #     i = 0

        for ball in fBalls_group:
            if player.rect.colliderect(ball.rect):
                if not ball.hasDamaged:
                    lives -= 1
                    ball.hasDamaged = True
                    ball.rect.x = 0
            ball.rect.x -= 1
            if ball.rect.x == -20:
                ball.rect.x = 640
                ball.rect.y = random.randint(1, 460)
                ball.hasDamaged = False

        for coin in coins:
            if coin.x <= 0 - 250:
                #draw_group.add(coin)
                coin.x = 640
                rndInty = random.randint(1, 460)
                rndIntx = random.randint(1, 640)
                coin.y = rndInty
                coin.x = rndIntx
                continue
            else:
                coin.x -= 5
                continue

        for platform in platforms:
            if platform.rect.x <= 0 - 250:
                coin_group.add(coin1)
                coin_group.add(coin2)
         #      if i == red:
         #          i += 1
        #       platform.image = pygame.image.load('Images/lava.png').convert_alpha()
       #    else:
       #        platform.image = pygame.image.load('Images/stonePlatform.png').convert_alpha()
                platform.rect.x = 640
                rndInty = random.randint(1, 460)
                platform.rect.y = rndInty
                continue
            else:
                platform.rect.x -= 5
                continue


        #sprite_rect = pygame.sprite.spritecollideany(player, platforms).rect
        player.rect.y = y - 62
    elif gameOver:
        draw_text('GAME OVER', font, screen, 210, 210, (0,0,0))
        draw_text('Press ENTER to try again', font, screen, 190, 230, (0,0,0))

    pygame.display.update()

我可以在Python Shell中运行一个简单的猜谜游戏,但每当我运行游戏时,它出现在我制作的单独窗口中,就会显示此错误。我做了另一个游戏并且出现了同样的错误,但我把代码放在了以防万一。

任何帮助表示赞赏。谢谢你们!

0 个答案:

没有答案