微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

脚本自行关闭

如何解决脚本自行关闭

我最近创建了一个新的 python 文件,这样我就可以测试我知道多少东西。我为一个小游戏编写了一个小脚本(我不需要帮助让它变得更好,不,谢谢),每当我开始随机播放时,窗口就会关闭。我的代码中有错误吗?它只是发生在自己身上吗?我不知道!这是脚本!

import pygame
import sys
import random
import pickle
from pygame.locals import *

pygame.init()

width = 1000
height = 600

black = (0,0)
white = (255,255,255)
red = (255,0)
yellow = (255,0)
blue = (0,255)

player_pos = [450,500]
player_size = 50

speed = 50

coin_pos = [random.randint(100,900),random.randint(100,500)]
coin_size = 20

mine_pos1 = [random.randint(100,500)]
mine_pos2 = [random.randint(100,500)]
mine_pos3 = [random.randint(100,500)]
mine_pos4 = [random.randint(100,500)]
mine_pos5 = [random.randint(100,500)]
mine_pos6 = [random.randint(100,500)]
mine_pos7 = [random.randint(100,500)]
mine_pos8 = [random.randint(100,500)]
mine_pos9 = [random.randint(100,500)]
mine_pos10 = [random.randint(100,500)]
mine_pos11 = [random.randint(100,500)]
mine_size = 20
big_mine_size = 40
large_mine_size = 60

coins_count = 0

title_font = pygame.font.SysFont('unisansheavyitaliccaps.ttf',35,bold=20,italic=20)
goal_font = pygame.font.SysFont('unisansheavyitaliccaps.ttf',37,italic=20)

clock = pygame.time.Clock()
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Collect And Avoid")

click = False


def draw_text(text,font,color,surface,x,y):
    text_obj = font.render(text,5,color)
    text_rect = text_obj.get_rect()
    text_rect = (x,y)
    surface.blit(text_obj,text_rect)


def main_menu(player_pos):
    global click
    while True:
        yellow_cubes_text = goal_font.render("yellow cubes",True,yellow)
        red_cubes_text = goal_font.render("red cubes",red)
        screen.fill(black)
        draw_text('Collect And Avoid',title_font,white,screen,375,20)
        draw_text('Your goal is to collect as much coins as possible,the                     .',goal_font,25,300)
        draw_text('Careful not to step on the,they are deadly mines that will',20,350)
        draw_text('end your game if you step on them so try to avoid them!',400)
        draw_text('Click To Start -->',150,130)

        # coins_count = pickle.load(open("coins.dat","rb"))
        draw_text('Total Coins: ' + str(coins_count),400,550)

        x,y = pygame.mouse.get_pos()
        screen.blit(yellow_cubes_text,(783,300))
        screen.blit(red_cubes_text,(388,350))

        button_1 = pygame.Rect(400,120,200,50)
        if button_1.collidepoint((x,y)):
            if click:
                game(player_pos,coin_pos,coins_count)
        pygame.draw.rect(screen,red,button_1)

        click = False
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True

        pygame.display.update()
        clock.tick(60)


def detect_collision(player_pos,coin_pos):
    p_x = player_pos[0]
    p_y = player_pos[1]

    c_x = coin_pos[0]
    c_y = coin_pos[1]

    if (p_x <= c_x < (p_x + player_size)) or (c_x <= p_x < (c_x + coin_size)):
        if (p_y <= c_y < (p_y + player_size)) or (c_y <= p_y < (c_y + coin_size)):
            return True

    return False


def game(player_pos,coins_count):
    global mine_pos1,mine_pos2,mine_pos3,mine_pos4,mine_pos5,mine_pos6,mine_pos7,mine_pos8,mine_pos9,\
        mine_pos10,mine_pos11
    game_over = False
    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.KEYDOWN:

                x = player_pos[0]
                y = player_pos[1]

                if event.key == pygame.K_w:
                    y -= speed
                elif event.key == pygame.K_s:
                    y += speed
                elif event.key == pygame.K_a:
                    x -= speed
                elif event.key == pygame.K_d:
                    x += speed
                elif event.key == pygame.K_ESCAPE:
                    sys.exit()

                player_pos = [x,y]

        screen.fill(black)

        if detect_collision(player_pos,coin_pos):
            coin = coin_pos = [random.randint(100,500)]
            coins_count += 5
            # pickle.dump(coins_count,open("coins.dat","wb"))
            mine_pos1 = [random.randint(100,500)]
            mine_pos2 = [random.randint(100,500)]
            mine_pos3 = [random.randint(100,500)]
            mine_pos4 = [random.randint(100,500)]
            mine_pos5 = [random.randint(100,500)]
            mine_pos6 = [random.randint(100,500)]
            mine_pos7 = [random.randint(100,500)]
            mine_pos8 = [random.randint(100,500)]
            mine_pos9 = [random.randint(100,500)]
            mine_pos10 = [random.randint(100,500)]
            mine_pos11 = [random.randint(100,500)]

        if detect_collision(player_pos,mine_pos1):
            sys.exit()
        elif detect_collision(player_pos,mine_pos2):
            sys.exit()
        elif detect_collision(player_pos,mine_pos3):
            sys.exit()
        elif detect_collision(player_pos,mine_pos4):
            sys.exit()
        elif detect_collision(player_pos,mine_pos5):
            sys.exit()
        elif detect_collision(player_pos,mine_pos6):
            sys.exit()
        elif detect_collision(player_pos,mine_pos7):
            sys.exit()
        elif detect_collision(player_pos,mine_pos8):
            sys.exit()
        elif detect_collision(player_pos,mine_pos9):
            sys.exit()
        elif detect_collision(player_pos,mine_pos10):
            sys.exit()
        elif detect_collision(player_pos,mine_pos11):
            sys.exit()

        draw_text('Coins: ' + str(coins_count),450,20)

        if coins_count >= 5:
            mine1 = pygame.draw.rect(screen,(mine_pos1[0],mine_pos1[1],mine_size,mine_size))
            if coins_count >= 10:
                mine2 = pygame.draw.rect(screen,(mine_pos2[0],mine_pos2[1],mine_size))
                if coins_count >= 15:
                    mine3 = pygame.draw.rect(screen,(mine_pos3[0],mine_pos3[1],mine_size))
                    if coins_count >= 20:
                        mine4 = pygame.draw.rect(screen,(mine_pos4[0],mine_pos4[1],mine_size))
                        if coins_count >= 25:
                            mine5 = pygame.draw.rect(screen,(mine_pos5[0],mine_pos5[1],mine_size))
                            if coins_count >= 30:
                                mine6 = pygame.draw.rect(screen,(mine_pos6[0],mine_pos6[1],big_mine_size,big_mine_size))
                                if coins_count >= 35:
                                    mine7 = pygame.draw.rect(screen,(mine_pos7[0],mine_pos7[1],big_mine_size))
                                    if coins_count >= 40:
                                        mine8 = pygame.draw.rect(screen,(
                                        mine_pos8[0],mine_pos8[1],big_mine_size))
                                        if coins_count >= 50:
                                            mine9 = pygame.draw.rect(screen,(mine_pos9[0],mine_pos9[1],big_mine_size))
                                            if coins_count >= 60:
                                                mine10 = pygame.draw.rect(screen,(mine_pos10[0],mine_pos10[1],large_mine_size,large_mine_size))
                                                if coins_count >= 70:
                                                    mine11 = pygame.draw.rect(screen,(mine_pos11[0],mine_pos11[1],large_mine_size))

        player = pygame.draw.rect(screen,blue,(player_pos[0],player_pos[1],player_size,player_size))
        coin = pygame.draw.rect(screen,yellow,(coin_pos[0],coin_pos[1],coin_size,coin_size))

        pygame.display.update()
        clock.tick(60)


main_menu(player_pos)

解决方法

问题在于,您正在对未在屏幕上绘制的地雷进行碰撞检测,并在发生任何碰撞使游戏看起来像是随机关闭时终止程序。

还可以进行其他改进。首先,使用地雷列表,而不是手动定义地雷。

mines = [[random.randint(100,900),random.randint(100,500)] for i in range(10)]

使用 pygame.Rect 进行碰撞检测。

def detect_collision(player_pos,coin_pos):
    player_rect = Rect(player_pos[0],player_pos[1],player_size,player_size)
    coin_rect = Rect(coin_pos[0],coin_pos[1],coin_size,coin_size)
    if player_rect.colliderect(coin_rect):
        return True
    return False

使用名为 mines_showing 的变量。这就是您要在屏幕上绘制并对其进行碰撞检测的地雷数量。

for i in range(mines_showing):
     pygame.draw.rect(screen,red,(mines[i][0],mines[i][1],mine_size,mine_size))

for mine in mines[:mines_showing]:
     if detect_collision(player_pos,mine):
          sys.exit()

使用 mod 运算符每 5 分增加 mines_showing。但是我们不想影响原来的 coins_count 变量,所以在循环外创建一个名为 count_count_iter 的新初始变量只是为了做计算然后:

if count_count_iter != 0:
     if count_count_iter % 5 == 0:
          count_count_iter = 0
          mines_showing += 1

最后,如果玩家和硬币发生碰撞,我们希望更新新的 count_count_iter 变量并创建新的地雷,但使用列表而不是手动创建它们。

if detect_collision(player_pos,coin_pos):
      coin = coin_pos = [random.randint(100,500)]
      coins_count += 5
      count_count_iter += 5
      # pickle.dump(coins_count,open("coins.dat","wb"))
      mines = []
      mines = [[random.randint(100,500)] for i in range(10)]

新代码:

import pygame
import sys
import random
import pickle
from pygame.locals import *

pygame.init()

width = 1000
height = 600

black = (0,0)
white = (255,255,255)
red = (255,0)
yellow = (255,0)
blue = (0,255)

player_pos = [450,500]
player_size = 50

speed = 50

coin_pos = [random.randint(100,500)]
coin_size = 20

mines = [[random.randint(100,500)] for i in range(10)]
mine_size = 20
big_mine_size = 40
large_mine_size = 60

coins_count = 0

title_font = pygame.font.SysFont('unisansheavyitaliccaps.ttf',35,bold=20,italic=20)
goal_font = pygame.font.SysFont('unisansheavyitaliccaps.ttf',37,italic=20)

clock = pygame.time.Clock()
screen = pygame.display.set_mode((width,height))
pygame.display.set_caption("Collect And Avoid")

click = False

mines_showing = 0
count_count_iter = 0

def draw_text(text,font,color,surface,x,y):
    text_obj = font.render(text,5,color)
    text_rect = text_obj.get_rect()
    text_rect = (x,y)
    surface.blit(text_obj,text_rect)


def main_menu(player_pos):
    global click
    while True:
        yellow_cubes_text = goal_font.render("yellow cubes",True,yellow)
        red_cubes_text = goal_font.render("red cubes",red)
        screen.fill(black)
        draw_text('Collect And Avoid',title_font,white,screen,375,20)
        draw_text('Your goal is to collect as much coins as possible,the                     .',goal_font,25,300)
        draw_text('Careful not to step on the,they are deadly mines that will',20,350)
        draw_text('end your game if you step on them so try to avoid them!',400)
        draw_text('Click To Start -->',150,130)

        # coins_count = pickle.load(open("coins.dat","rb"))
        draw_text('Total Coins: ' + str(coins_count),400,550)

        x,y = pygame.mouse.get_pos()
        screen.blit(yellow_cubes_text,(783,300))
        screen.blit(red_cubes_text,(388,350))

        button_1 = pygame.Rect(400,120,200,50)
        if button_1.collidepoint((x,y)):
            if click:
                game(player_pos,coin_pos,coins_count)
        pygame.draw.rect(screen,button_1)

        click = False
        for event in pygame.event.get():
            if event.type == QUIT:
                sys.exit()
            if event.type == KEYDOWN:
                if event.key == K_ESCAPE:
                    sys.exit()
            if event.type == MOUSEBUTTONDOWN:
                if event.button == 1:
                    click = True

        pygame.display.update()
        clock.tick(60)


def detect_collision(player_pos,coin_size)
    if player_rect.colliderect(coin_rect):
        return True
    return False


def game(player_pos,coins_count):
    global mines
    global mines_showing
    global can_update
    global count_count_iter
    game_over = False
    while not game_over:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                sys.exit()

            if event.type == pygame.KEYDOWN:

                x = player_pos[0]
                y = player_pos[1]

                if event.key == pygame.K_w:
                    y -= speed
                elif event.key == pygame.K_s:
                    y += speed
                elif event.key == pygame.K_a:
                    x -= speed
                elif event.key == pygame.K_d:
                    x += speed
                elif event.key == pygame.K_ESCAPE:
                    sys.exit()

                player_pos = [x,y]

        screen.fill(black)

        if detect_collision(player_pos,coin_pos):
            coin = coin_pos = [random.randint(100,500)]
            coins_count += 5
            count_count_iter += 5
            # pickle.dump(coins_count,"wb"))
            mines = []
            mines = [[random.randint(100,500)] for i in range(10)]

        for mine in mines[:mines_showing]:
            if detect_collision(player_pos,mine):
                sys.exit()

        draw_text('Coins: ' + str(coins_count),450,20)

        if count_count_iter != 0:
            if count_count_iter % 5 == 0:
                count_count_iter = 0
                mines_showing += 1

        for i in range(mines_showing):
            pygame.draw.rect(screen,mine_size))

        player = pygame.draw.rect(screen,blue,(player_pos[0],player_size))
        coin = pygame.draw.rect(screen,yellow,(coin_pos[0],coin_size))

        pygame.display.update()
        clock.tick(60)


main_menu(player_pos)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。