如何将颜色显示在我正在制作的游戏中?

问题描述 投票:1回答:1

这是我第一次使用Pygame。我刚刚开始学习编程。目前我学习了大约4到7个月。

我按照我正在制作的游戏的教程。 (如何编写游戏!(在Python中)作者:Keith Galli)

不幸的是,游戏中不会显示颜色。所以此刻我只是盯着一个黑色的“盒子”。

游戏是一种“太空入侵者”游戏。因此,从“盒子”的顶部有蓝砖掉落,但此刻你却看不到它们。


因为我使用Linux Mint作为我的操作系统,我想:也许我需要其他颜色的代码。

我试过ANSI代码:

# ANSI escae code for colours:
#RED = "\e[0;31m"
#BLUE = "\e[34mBlue"
#YELLOW= "\e[33mYellow"
#BACKGROUND_COLOUR = "\e[30m"

instead of RGB:
# RGB colours:
RED = (255, 0, 0))
BLUE = (0, 0, 255))
YELLOW= (204, 204, 0)
BACKGROUND_COLOUR = (0, 0, 0))

但是当我运行游戏时我仍然没有看到任何东西:(


这是一些代码:

import pygame 
import random
import sys
import colorama

pygame.init()

WIDTH = 800
HEIGHT = 600

# ANSI escae code for colours:
#RED = "\e[0;31m"
#BLUE = "\e[34mBlue"
#YELLOW= "\e[33mYellow"
#BACKGROUND_COLOUR = "\e[30m"

# RGB colours:
RED = (255, 0, 0))
BLUE = (0, 0, 255))
YELLOW= (204, 204, 0)
BACKGROUND_COLOUR = (0, 0, 0))

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 = 10

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

# gameloop

game_over = False

score = 0

clock = pygame.time.Clock()

myFont = pygame.font.sysFont("monospace", 35)

def set_level(score,SPEED):
    if score < 20:
        SPEED = 5
    elif score < 40:
        SPEED = 8
    elif score < 60:
        SPEED = 12
    else:
        SPEED = 18
    return SPEED

# SPEED = score / 5 +1
# return SPEED

def drop_enemies(enemy_list):
    delay = random.random()
    if len(enemy_list) < 10 and delay < 0.1:
    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, BLUE, (enemy_pos[0], enemy_pos[1],
        enemy_size, enemy_size))

我希望看到游戏中的砖块。使用哪种颜色编码......以及如何实现这些?

蓝色为从天空落下的,红色为玩家本身。

python linux colors pygame mint
1个回答
1
投票

您需要一个主循环,它执行以下操作:

e.f.:

pygame.display.flip()
© www.soinside.com 2019 - 2024. All rights reserved.