我如何解决 screen.blit in pygame in python 时遇到的问题?
How can I fix this problem that I am having with screen.blit in pygame in python?
我正在关注 YouTube 上关于如何制作 space 入侵者游戏的教程,但我遇到了一个问题,我收到一条错误消息:invalid destination position for blit
,我该如何解决这个错误?谢谢。
我在 windows 10 家用电脑上使用 python 3.10.4 和 spyder IDE。
非常感谢任何答案。
控制台说错误来自第 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
enemyX
和 enemyY
是列表:
enemy(enemyX, enemyY, i)
enemy(enemyX[i], enemyY[i], i)
我正在关注 YouTube 上关于如何制作 space 入侵者游戏的教程,但我遇到了一个问题,我收到一条错误消息:invalid destination position for blit
,我该如何解决这个错误?谢谢。
我在 windows 10 家用电脑上使用 python 3.10.4 和 spyder IDE。
非常感谢任何答案。
控制台说错误来自第 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
enemyX
和 enemyY
是列表:
enemy(enemyX, enemyY, i)
enemy(enemyX[i], enemyY[i], i)