pygame运行窗户时黑色

发布于 2025-01-18 17:54:50 字数 2264 浏览 0 评论 0原文

我是Python新手,只是按照视频创建一个游戏,我做的一切都是正确的,但游戏窗口/屏幕又变黑了。

我期待游戏能够以正确的方式运行,但是,我似乎做错了一些事情,因为它不适合我。

我能够在 [i] 代码之前玩游戏,我可以返回并玩它,但我想要有多个敌人,一旦我关闭程序,我就能很快看到它,黑屏显示游戏,但随后它关闭。

import pygame
import random
import math

# initialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800, 600))



# enemy to many anemy multiplicar los enemigos
enemyimg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    # Player(enemy)
    enemyimg.append(pygame.image.load("enemy.png"))
    enemyX.append(random.randint(0, 735))
    enemyY.append(random.randint(50, 150))
    enemyX_change.append(0.2)
    enemyY_change.append(40)

# (bullet)
# ready_state means you cant see the bullet before firing
# fire - the bullet is currently moving
bulletimg = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 480
bulletX_change = 0.2
bulletY_change = 1
bullet_state = ("ready")

score = 0




def fire_bullet(x, y):
    global bullet_state
    bullet_state = "fire"
    screen.blit(bulletimg, (x + 16, y + 10))


def isCollision(enemyX, enemyY, bulletX, bulletY):
    distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
    if distance < 27:
        return True
    else:
        return False




        
   

    # enemy movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]

        if enemyX[i] <= 0:
            enemyX_change[i] = 0.3
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 745:
            enemyX_change[i] = -0.3
            enemyY[i] += enemyY_change[i]


            # Collission
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 480
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(0, 735)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX[i], enemyY[i], i)

# bullet movement
if bulletY <= 0:
    bulletY = 480
    bullet_state = "ready"

if bullet_state == "fire":
    fire_bullet(bulletX, bulletY)
    bulletY -= bulletY_change

player(playerX, playerY)

pygame.display.update()

I'm new to python just following a video to create a game im doing everything right but the game windows/screen comes back black.

I'm expecting the game to function the right way but, it seems that I'm doing something wrong because it's not working for me.

i was able to play the game before the [i] codes i can go back and play it but i want to have multiple enemy, im able to see it real quick once i close the program the black sceen shows the games but then it closes.

import pygame
import random
import math

# initialize the pygame
pygame.init()

# create the screen
screen = pygame.display.set_mode((800, 600))



# enemy to many anemy multiplicar los enemigos
enemyimg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

for i in range(num_of_enemies):
    # Player(enemy)
    enemyimg.append(pygame.image.load("enemy.png"))
    enemyX.append(random.randint(0, 735))
    enemyY.append(random.randint(50, 150))
    enemyX_change.append(0.2)
    enemyY_change.append(40)

# (bullet)
# ready_state means you cant see the bullet before firing
# fire - the bullet is currently moving
bulletimg = pygame.image.load("bullet.png")
bulletX = 0
bulletY = 480
bulletX_change = 0.2
bulletY_change = 1
bullet_state = ("ready")

score = 0




def fire_bullet(x, y):
    global bullet_state
    bullet_state = "fire"
    screen.blit(bulletimg, (x + 16, y + 10))


def isCollision(enemyX, enemyY, bulletX, bulletY):
    distance = math.sqrt((math.pow(enemyX - bulletX, 2)) + (math.pow(enemyY - bulletY, 2)))
    if distance < 27:
        return True
    else:
        return False




        
   

    # enemy movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]

        if enemyX[i] <= 0:
            enemyX_change[i] = 0.3
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 745:
            enemyX_change[i] = -0.3
            enemyY[i] += enemyY_change[i]


            # Collission
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 480
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(0, 735)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX[i], enemyY[i], i)

# bullet movement
if bulletY <= 0:
    bulletY = 480
    bullet_state = "ready"

if bullet_state == "fire":
    fire_bullet(bulletX, bulletY)
    bulletY -= bulletY_change

player(playerX, playerY)

pygame.display.update()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉酒的小男人 2025-01-25 17:54:50

我认为你应该在代码中添加一个 while 循环,否则它只会运行一次。据我所知,人们通常这样做:

run = True
while run:
    # enemy movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]

        if enemyX[i] <= 0:
            enemyX_change[i] = 0.3
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 745:
            enemyX_change[i] = -0.3
            enemyY[i] += enemyY_change[i]


            # Collission
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 480
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(0, 735)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX[i], enemyY[i], i)

    # bullet movement
    if bulletY <= 0:
        bulletY = 480
        bullet_state = "ready"

    if bullet_state == "fire":
        fire_bullet(bulletX, bulletY)
        bulletY -= bulletY_change

    player(playerX, playerY)

    pygame.display.update()

当然,再加上在此 for 循环之前您拥有的所有内容,您可以了解有关游戏循环的更多信息 此处

I think you should add a while loop to your code, otherwise it will run just once. From what I can see, people usually do it like this:

run = True
while run:
    # enemy movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]

        if enemyX[i] <= 0:
            enemyX_change[i] = 0.3
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >= 745:
            enemyX_change[i] = -0.3
            enemyY[i] += enemyY_change[i]


            # Collission
        collision = isCollision(enemyX[i], enemyY[i], bulletX, bulletY)
        if collision:
            bulletY = 480
            bullet_state = "ready"
            score += 1
            print(score)
            enemyX[i] = random.randint(0, 735)
            enemyY[i] = random.randint(50, 150)

        enemy(enemyX[i], enemyY[i], i)

    # bullet movement
    if bulletY <= 0:
        bulletY = 480
        bullet_state = "ready"

    if bullet_state == "fire":
        fire_bullet(bulletX, bulletY)
        bulletY -= bulletY_change

    player(playerX, playerY)

    pygame.display.update()

plus of course everything that you had before this for loop you can learn more about game loops here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文