我正在使用pygame创建一个游戏,但遇到了一个错误,我是pygame的新手。我正在使用python 3.8和pycharm社区版2020.1
这是我制作游戏的视频:https://www.youtube.com/watch?v=FfWpgLFMI7w这里是错误:
Traceback (most recent call last):
File "C:/Users/user/PycharmProjects/Agam/Python/Pygame.py", line 131, in <module>
enemy(enemyX, enemyY)
File "C:/Users/user/PycharmProjects/Agam/Python/Pygame.py", line 28, in enemy
screen.blit(enemyImg, (x, y))
TypeError: argument 1 must be pygame.Surface, not list
这是我的密码:
import math
import random
import pygame
pygame.init()
# Screen (Pixels by Pixels (X and Y (X = right and left Y = up and down)))
screen = pygame.display.set_mode((800, 600))
running = True
# Title and Icon
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('Icon.png')
pygame.display.set_icon(icon)
# Player Icon/Image
playerimg = pygame.image.load('Player.png')
playerX = 370
playerY = 480
playerX_change = 0
def player(x, y):
# Blit means Draw
screen.blit(playerimg, (x, y))
def enemy(x, y):
# Blit means Draw
screen.blit(enemyImg, (x, y))
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[i] - bulletX, 2) + (math.pow(enemyY[i] - bulletY, 2))))
if distance < 27:
return True
else:
return False
background = pygame.image.load('247.jpg')
# Enemy
num_of_enemy = 6
enemyImg = []
enemyX = []
enemyY = []
enemyX_change = []
enemyY_change = []
for i in range(num_of_enemy):
enemyImg.append(pygame.image.load('space-invaders.png'))
enemyX.append(random.randint(0, 735))
enemyY.append(random.randint(50, 150))
enemyX_change.append(4)
enemyY_change.append(40)
bulletimg = pygame.image.load('bullet.png')
bulletX = 0
bulletY = 450
bulletX_change = 480
bulletY_change = 10
bullet_state = "ready"
score = 0
# Game loop (Put most of code for game in this loop)
while running:
screen.fill((255, 0, 0))
# BAckground
screen.blit(background, (0, 0))
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# if keystroke is pressed check whether is right or left
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
playerX_change = -5
if event.key == pygame.K_RIGHT:
playerX_change = 5
if event.key == pygame.K_SPACE:
if bullet_state == "ready":
bulletX = playerX
fire_bullet(playerX, bulletY)
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
playerX_change = 0
# RGB (screen.fill) = red green blue
# 5 = 5 + - 0.1 -> 5 = 5 - 0.1
# making so nothing can go out of bounds
for i in range(num_of_enemy):
enemyX[i] += enemyX_change[i]
if enemyX[i] <= 0:
enemyX_change[i] = 4
enemyY[i] += enemyY_change[i]
elif enemyX[i] >= 736:
enemyX_change[i] = -4
enemyY[i] += enemyY_change[i]
playerX += playerX_change
if playerX <= 0:
playerX = 0
elif playerX >= 736:
playerX = 736
# Bullet movement
if bulletY <= 0:
bulletY = 480
bullet_state = "ready"
if bullet_state == "fire":
fire_bullet(bulletX, bulletY)
bulletY -= bulletY_change
# Collison
collision = isCollision(enemyX, enemyY, bulletX, bulletY)
if collision:
bulletY = 480
bullet_state = "ready"
score += 1
print(score)
enemyX = random.randint(0, 735)
enemyY = random.randint(50, 150)
player(playerX, playerY)
enemy(enemyX, enemyY)
pygame.display.update()
enemyImg
是一个图像列表:
enemyImg = []
enemyImg.append(pygame.image.load('space-invaders.png'))
因此,您必须按订阅从列表中选择图像。
无论如何,您有一个敌人图像列表enemyImg
和一个x坐标列表(enemyX
,enemyY
)。
将敌人图像的参数添加到函数敌人
:
def enemy(img, x, y):
# Blit means Draw
screen.blit(img, (x, y))
把敌人画成一个循环:
while running:
# [...]
# draw player
player(playerX, playerY)
# draw enemies in a loop
for i in range(len(enemyImg)):
enemy(enemyImg[i], enemyX[i], enemyY[i])
# update dieplay
pygame.display.update()
我有一个问题,我做了一个乒乓游戏,但我有一个问题,把分数打印到pyplay窗口。 我得到错误'TypeError:参数1必须是pygame.Surface,而不是str 我在文本中输入了blit,但出现了一个错误。我知道代码乱七八糟,我稍后会修复它
我正在做一个游戏,这里似乎有一些问题。这是我第一次使用sprite床单,如果我的错误很简单,我很抱歉。 对于大量的代码,我提前表示歉意,但不幸的是,我相信问题可能存在于代码中的任何地方。我收到的错误是“TypeError:参数1必须是pygame.Surface,而不是method”。这是指all_sprites.draw(screen)命令。我知道屏幕的格式是正确的,所以不是这样。我的老师说很可
我无法理解Stackoverflow中的其他问题。当圆圈向屏幕末端移动时,它会向相反方向移动。 发出错误消息而未执行。 screen.blit(圆圈,[x,y]) 类型错误:参数1必须是pygame.Surface,而不是pygame.Rect 有什么问题吗?
我正在用python制作一个潜艇游戏,但当我试图运行它时,解释器给了我一个非常奇怪的错误:“TypeError:参数1必须是pygame.Surface,而不是type。”我试图在网上搜索我的答案,但这似乎不是很常见的错误。我也试着自己去发现错误,但我觉得一切都很好。下面是我认为错误所在的部分代码:
你好,我正在创建一个游戏使用pyplay和我有一些问题。我创建了一些按钮(使用图像),并根据某些操作,此按钮的图像将发生变化。我将向您展示代码中存在错误的部分。抱歉,我是西班牙人,我的英语不好。这是代码: 那么错误就是那个: 例如,在代码的最后一行: 如果我把默认,图像是那些所谓的default_ap和default_ip,那些: 如果我把丘巴卡,图像将被称为chewbacca_ap和chewba