python-3.x 如何错开对象的启动?

zzzyeukh  于 2023-03-31  发布在  Python
关注(0)|答案(1)|浏览(110)

我试图做一个游戏,有多个物体下降,你必须避免,每个在不同的高度。然而,我最终只有8块在顶部和1下降的时间。有人能帮助我理解为什么并解决它吗?我怀疑与缩进的东西。
代码如下所示。

import pygame
import random
import sys
import time
import os
from tkinter import *
from tkinter import messagebox

#some config

print(os.getcwd())

pygame.init()

#actually runs the game

def game():
    print("")
    WIDTH = 800
    HEIGHT = 600

    TRASH = (110, 110, 110)
    SEA_TURTLE = (108,170,180)
    player_pos = [400, 475]
    player_size = 50
    game_over = False
    screen = pygame.display.set_mode((WIDTH, HEIGHT))
    my_image = pygame.image.load("SeaTurtle1.png").convert_alpha()
    enemy_size = 40

    SPEED = 11

    FPS = 120

    enemy_pos = [random.randint(0, WIDTH - enemy_size), 0]
    enemy_list = [enemy_pos]

    score = 0

    clock = pygame.time.Clock()

    myFont = pygame.font.SysFont("DroidSans", 35)

    pygame.display.set_caption('Trash Dodge, Science Project,  By: Benjamin Bernstein')

    pygame.mouse.set_visible(0)

#drops enemies mechanic

    def drop_enemies(enemy_list):
        if len(enemy_list) < 9:
            x_pos = random.randint(0, WIDTH - enemy_size)
            y_pos = 10
            enemy_list.append([x_pos, y_pos])

#make enemies

    def draw_enemies(enemy_list):
        for enemy_pos in enemy_list:
            pygame.draw.rect(screen, TRASH, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))

#update enemies kinda broken ngl

    def update_enemies_pos(enemy_list, score):
        for idx, enemy_pos in enumerate(enemy_list):
            if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT:
                enemy_pos[1] += SPEED
            else:
                enemy_list.pop(idx)
                score += 1
            return score

#collision detection

    def collision_check(enemy_list, player_pos):
        for enemy_pos in enemy_list:
            if detect_collision(enemy_pos, player_pos):
                return True
            return False

#edge detection so you can teleport to other side of window

    def edge_detection1(player_pos):
        if player_pos[0] > 750:
            return True
        return False

    def edge_detection2(player_pos):
        if player_pos[0] < 0:
            return True
        return False

#more collision detection

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

        e_x = enemy_pos[0]
        e_y = enemy_pos[1]

        if (e_x >= p_x and e_x < (p_x + player_size)) or (p_x >= e_x and p_x < (e_x + enemy_size)):
            if (e_y >= p_y and e_y < (p_y + player_size)) or (p_y >= e_y and p_y < (e_y + enemy_size)):
                return True
            return False

#some stuff to run all the time like controls, score and window

    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_LEFT:
                    x -= player_size
                elif event.key == pygame.K_RIGHT:
                    x += player_size
                elif event.key == pygame.K_a:
                    x -= player_size
                elif event.key == pygame.K_d:
                    x += player_size
                elif event.key == pygame.K_q:
                    x -= player_size
                elif event.key == pygame.K_ESCAPE:
                    sys.exit()

                player_pos = [x, y]
        screen.fill((108,170,180))

        drop_enemies(enemy_list)
        score = update_enemies_pos(enemy_list, score)

        text = "Score: " + str(score)
        label = myFont.render(text, 1, (0, 0, 0))
        screen.blit(label, (WIDTH - 120, HEIGHT - 40))
#HERE IT IS
#MORE EDGE DETECTION THIS MAKES ME GO INSANE

        if edge_detection1(player_pos):
            player_pos[0] = 0

        if edge_detection2(player_pos):
            player_pos[0] = 750

        if collision_check(enemy_list, player_pos):
            time.sleep(1.4)
            Tk().wm_withdraw()
            game_over = True
        draw_enemies(enemy_list)
        pygame.draw.rect(screen, SEA_TURTLE, (player_pos[0], player_pos[1], player_size, player_size))
        player = pygame.draw.rect(screen, SEA_TURTLE, (player_pos[0], player_pos[1], player_size, player_size))
        screen.blit(my_image, player)
        clock.tick(FPS)
        pygame.display.update()
#death message
    ans = messagebox.askretrycancel("You died.", "You lose! This game shows how hard litter and trash makes it for sea turtles to survive. Make sure to pick up any litter and trash you see on the beach.")
    root = Tk()
    root.destroy()
    return ans

#close the game

while True:
    if game() == True:
        pass
    else:
        break```
x8diyxa7

x8diyxa71#

你是对的,缩进是问题所在。你在update_enemies_pos函数的循环中返回。这应该可以工作:

def update_enemies_pos(enemy_list, score):
    for idx, enemy_pos in enumerate(enemy_list):
        if enemy_pos[1] >= 0 and enemy_pos[1] < HEIGHT:
            enemy_pos[1] += SPEED
        else:
            enemy_list.pop(idx)
            score += 1
    return score

相关问题