如何解决 python 中 pygame 中 screen.blit 遇到的问题?

发布于 2025-01-20 09:50:04 字数 3728 浏览 0 评论 0原文

我正在YouTube上遵循有关如何制作太空入侵者游戏的教程,但是我偶然发现了一个错误,我遇到了一个错误:Blit的目标无效的目标位置,我如何解决此错误?谢谢。 我正在使用Windows 10家用计算机上使用Spyder IDE的Python 3.10.4。 任何答案都非常感谢。

该控制台说该错误来自第54行,在这种情况下,这是此代码:

53> def enemy(x, y, i):
54>     screen.blit(enemyImg[i], (x, y))

代码:

import pygame
import random
import math

# Intiatize the pygame
pygame.init()

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

# Background
background = pygame.image.load("background.png")

# Caption and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load("player.png")
playerX = 370
playerY = 480
playerX_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

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

# Bullet1
bullet1Img = pygame.image.load("bullet1.png")
bullet1X = 0
bullet1Y = 480
bullet1X_change = 0
bullet1Y_change = 0.5
bullet_state = "ready"

score = 0

def player(x, y):
    screen.blit(playerImg, (x, y))
    
def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))
    
def fire_bullet1(x, y): 
    global bullet_state
    bullet_state = "fire"
    screen.blit(bullet1Img, (x, y + 10))
    
def isCollision(enemyX, enemyY, bullet1X, bullet1Y):
    distance = math.sqrt(math.pow(enemyX-bullet1X,2)) + (math.pow(enemyY-bullet1Y,2))
    if distance < 27:
        return True
    else:
        return False

# Game Loop
running = True
while running:
    screen.fill((255, 255, 255))
    # Background Image
    screen.blit(background, (0,0))
    
    # Checking for boundaries of spaceship
    playerX += playerX_change
    
    if playerX <=0:
        playerX = 0
    elif playerX >=736:
        playerX = 736
    
    # Enemy Movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <=0:
            enemyX_change[i] = 0.1
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >=736:
            enemyX_change[i] = -0.1
            enemyY[i] += enemyY_change[i]
            
        # Collision
        collision = isCollision(enemyX[i], enemyY[i], bullet1X, bullet1Y)
        if collision:
            bullet1Y = 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 bullet1Y <=0:
        bullet1Y = 480
        bullet_state = "ready"
        
    if bullet_state is "fire":
        fire_bullet1(bullet1X, bullet1Y)
        bullet1Y -= bullet1Y_change
    
    player(playerX, playerY)
    enemy(enemyX, enemyY, i)
    pygame.display.update()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -0.3
            if event.key == pygame.K_RIGHT:
                playerX_change = +0.3
            if event.key == pygame.K_SPACE:
                if bullet_state is "ready":
                    bullet1X = playerX
                    fire_bullet1(bullet1X, bullet1Y)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0 

I am following a tutorial on YouTube on how to make a space invaders game, but I have stumbled into an issue where I am getting an error saying: invalid destination position for blit, how can I fix this error? Thanks.
I am using python 3.10.4 with spyder IDE on a windows 10 home computer.
Any answer is very much appreciated.

The console is saying that the error is coming from line 54, which in this case is this code:

53> def enemy(x, y, i):
54>     screen.blit(enemyImg[i], (x, y))

Code:

import pygame
import random
import math

# Intiatize the pygame
pygame.init()

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

# Background
background = pygame.image.load("background.png")

# Caption and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)

# Player
playerImg = pygame.image.load("player.png")
playerX = 370
playerY = 480
playerX_change = 0

# Enemy
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
num_of_enemies = 6

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

# Bullet1
bullet1Img = pygame.image.load("bullet1.png")
bullet1X = 0
bullet1Y = 480
bullet1X_change = 0
bullet1Y_change = 0.5
bullet_state = "ready"

score = 0

def player(x, y):
    screen.blit(playerImg, (x, y))
    
def enemy(x, y, i):
    screen.blit(enemyImg[i], (x, y))
    
def fire_bullet1(x, y): 
    global bullet_state
    bullet_state = "fire"
    screen.blit(bullet1Img, (x, y + 10))
    
def isCollision(enemyX, enemyY, bullet1X, bullet1Y):
    distance = math.sqrt(math.pow(enemyX-bullet1X,2)) + (math.pow(enemyY-bullet1Y,2))
    if distance < 27:
        return True
    else:
        return False

# Game Loop
running = True
while running:
    screen.fill((255, 255, 255))
    # Background Image
    screen.blit(background, (0,0))
    
    # Checking for boundaries of spaceship
    playerX += playerX_change
    
    if playerX <=0:
        playerX = 0
    elif playerX >=736:
        playerX = 736
    
    # Enemy Movement
    for i in range(num_of_enemies):
        enemyX[i] += enemyX_change[i]
        if enemyX[i] <=0:
            enemyX_change[i] = 0.1
            enemyY[i] += enemyY_change[i]
        elif enemyX[i] >=736:
            enemyX_change[i] = -0.1
            enemyY[i] += enemyY_change[i]
            
        # Collision
        collision = isCollision(enemyX[i], enemyY[i], bullet1X, bullet1Y)
        if collision:
            bullet1Y = 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 bullet1Y <=0:
        bullet1Y = 480
        bullet_state = "ready"
        
    if bullet_state is "fire":
        fire_bullet1(bullet1X, bullet1Y)
        bullet1Y -= bullet1Y_change
    
    player(playerX, playerY)
    enemy(enemyX, enemyY, i)
    pygame.display.update()
    
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
            pygame.quit()
            
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                playerX_change = -0.3
            if event.key == pygame.K_RIGHT:
                playerX_change = +0.3
            if event.key == pygame.K_SPACE:
                if bullet_state is "ready":
                    bullet1X = playerX
                    fire_bullet1(bullet1X, bullet1Y)
        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                playerX_change = 0 

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

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

发布评论

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

评论(1

时光与爱终年不遇 2025-01-27 09:50:04

eneyxeneyy是列表:

敌人(eNeyx,eneyy,i)

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

enemyX and enemyY are lists:

enemy(enemyX, enemyY, i)

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