简体   繁体   English

pygame 只是显示黑屏

[英]pygame just displays a black screen

I'm trying to get this game to work but it just displays a black screen.我试图让这个游戏正常运行,但它只是显示一个黑屏。 its a simple game where you just avoid falling blocks.这是一个简单的游戏,您只需避免掉落块。 I've looked at related questions but none of the answers worked for me.it says i have to add more details so hopefully this line will be enough cuz idk what else to write in this line to add detail to my post.我查看了相关问题,但没有一个答案对我有用。它说我必须添加更多细节,所以希望这一行就足够了,因为我想知道在这一行中还要写什么来为我的帖子添加细节。 my code:我的代码:

import pygame
import random
import sys
pygame.init()
WIDTH = 800
HEIGHT = 600

PLAYER_COLOR = (200,0,0)
ENEMY_COLOR = (0,200,0)
BACKGROUND_COLOR = (0,120,150)

player_size = 50

player_pos = [WIDTH/2, HEIGHT-2*player_size]

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

SPEED = 5
screen = pygame.display.set_mode((WIDTH,HEIGHT))

game_over = False

score = 0

clock = pygame.time.Clock

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

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

def update_enemy_position(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

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




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 e_y < (e_y+enemy_size):
            return True
    return False

while not game_over:
    screen.fill(BACKGROUND_COLOR)
    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

            player_pos = [x,y]

    drop_enemies(enemy_list)
    update_enemy_position(enemy_list, score)
    if collision_check(enemy_list, player_pos):
        game_over = True
        break

    draw_enemies(enemy_list)
    pygame.draw.rect(screen, ENEMY_COLOR, (enemy_pos[0], enemy_pos[1], enemy_size, enemy_size))
    pygame.draw.rect(screen, PLAYER_COLOR, (player_pos[0], player_pos[1], player_size, player_size))
    clock.tick(30)
    pygame.display.update()

The problem is here:问题在这里:

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

You are missing the condition check inside the for that checks for/detects the collision between the enemy_pos and the players position.您缺少for检查/检测enemy_pos 和玩家position 之间碰撞的条件检查。 It is probably supposed to look like this:它可能应该看起来像这样:

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

Right now it is always returning True and so your game is exiting before drawing anything, hence the black screen.现在它总是返回 True ,所以你的游戏在绘制任何东西之前就退出了,因此是黑屏。

Edit:编辑:

I noticed there are also two additional fairly simple typo like bugs in the code.我注意到还有两个额外的相当简单的错字,比如代码中的错误。 I was about to point them out as well.我也正要指出它们。 However something struck me about the nature of the bugs.然而,关于虫子的性质让我印象深刻。 This has the feel of an assignment: "Find the three bugs in this code and get it working".这有一种任务的感觉:“找到这段代码中的三个错误并让它工作”。 If this is really your code we are happy to help, but if this is a homework assignment, we are not helping you by doing it for you instead of you learning by doing it.如果这真的是您的代码,我们很乐意提供帮助,但如果这是家庭作业,我们不会通过为您完成它来帮助您,而不是您通过这样做来学习。

The previous bug was pretty easy to find and so are the remaining two.前一个错误很容易找到,剩下的两个也很容易找到。 Give debugging the rest a try.试一下调试rest。 I recommend using an IDE for debugging, not just running the script.我建议使用 IDE 进行调试,而不仅仅是运行脚本。 There are many good free IDE's out there that can make you programming experience and particularly the debugging experience much easier.有很多不错的免费 IDE 可以让您获得编程体验,尤其是调试体验更加轻松。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM