如何修复Python TypeError:不支持的操作数类型&x27;int';和';元组';
我试图用Youtube教程构建我的第一个游戏,但我遇到了这个错误,我不知道如何修复它。请帮助:) 回溯(最近一次呼叫最后一次): 文件“C:\Users\Admin\Documents\Python Tut\FirstGame\FirstGame.py”,第144行,在 bullet.x+=bullet.vel TypeError:不支持+=:“int”和“tuple”的操作数类型 我的代码:如何修复Python TypeError:不支持的操作数类型&x27;int';和';元组';,python,Python,我试图用Youtube教程构建我的第一个游戏,但我遇到了这个错误,我不知道如何修复它。请帮助:) 回溯(最近一次呼叫最后一次): 文件“C:\Users\Admin\Documents\Python Tut\FirstGame\FirstGame.py”,第144行,在 bullet.x+=bullet.vel TypeError:不支持+=:“int”和“tuple”的操作数类型 我的代码: import pygame pygame.init() window = pygame.displ
import pygame
pygame.init()
window = pygame.display.set_mode((600, 480))
pygame.display.set_caption('First Game')
#load character images
walkRight = [pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R1.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R2.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R3.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R4.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R5.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R6.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R7.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R8.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R9.png')]
walkLeft = [pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L1.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L2.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L3.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L4.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L5.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L6.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L7.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L8.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L9.png')]
bg = pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/bg.jpg')
char = pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/standing.png')
clock = pygame.time.Clock()
#Character
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.isJump = False
self.jumpCount = 10
self.left = False
self.right = False
self.walkCount = 0
self.standing = True
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
def draw(self, window):
if self.walkCount + 1 >= 27:
self.walkCount = 0
if not(self.standing):
if self.left:
window.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
window.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
else:
if self.right:
window.blit(walkRight[0], (self.x, self.y))
else:
window.blit(walkLeft[0], (self.x, self.y))
self.hitbox = (self.x + 17, self.y + 11, 29, 52)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
class projectile(object):
def __init__ (self, x, y, radius, facing, color):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self, window):
pygame.draw.circle(window, self.color, (self.x, self.y), self.radius)
#Enemy
class enemy(object):
walkRight = [pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R1E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R2E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R3E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R4E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R5E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R6E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R7E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R8E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R9E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R10E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/R11E.png')]
walkLeft = [pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L1E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L2E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L3E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L4E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L5E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L6E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L7E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L8E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L9E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L10E.png'),
pygame.image.load('C:/Users/Admin/Documents/Python Tut/FirstGame/L11E.png')]
def __init__ (self, x, y, width, height, end):
self.x = x
self.y = y
self.width = width
self.height = height
self.end = end
self.path = [self.x, self.end]
self.walkCount = 0
self.vel = 3
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
def draw(self, window):
self.move()
if self.walkCount + 1 >= 33:
self.walkCount = 0
if self.vel > 0:
window.blit(self.walkRight[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
else:
window.blit(self.walkLeft[self.walkCount //3], (self.x, self.y))
self.walkCount += 1
self.hitbox = (self.x + 17, self.y + 2, 31, 57)
pygame.draw.rect(window, (255, 0, 0), self.hitbox, 2)
def move(self):
if self.vel > 0:
if self.x + self.vel < self.path[1]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
else:
if self.x - self.vel > self.path[0]:
self.x += self.vel
else:
self.vel = self.vel * -1
self.walkCount = 0
def hit(self):
print('hit')
def redrawGameWindow():
window.blit(bg, (0,0))
man.draw(window)
goblin.draw(window)
for bullet in bullets:
bullet.draw(window)
pygame.display.update()
#mainloop
man = player(200, 400, 64, 64)
goblin = enemy(100, 400, 64, 64, 460)
bullets = []
run = True
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.y - bullet.radius < goblin.hitbox[1] + goblin.hitbox[3] and bullet.y + bullet.radius >
goblin.hitbox[1]:
if bullet.x + bullet.radius > goblin.hitbox[0] and bullet.x - bullet.radius <
goblin.hitbox[0] + goblin.hitbox[2]:
goblin.hit()
bullets.pop(bullets.index(bullet))
if bullet.x < 600 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
#Movement
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(round(man.x + man.width //2), round(man.y + man.height //2), 6,
(0,0,0), facing))
if keys[pygame.K_LEFT] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
man.standing = False
elif keys[pygame.K_RIGHT] and man.x < 600 - man.width - man.vel:
man.x += man.vel
man.right = True
man.left = False
man.standing = False
else:
man.standing = True
man.walkCount = 0
if not(man.isJump):
if keys[pygame.K_UP]:
man.isJump = True
man.walkCount = 0
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
redrawGameWindow()
pygame.quit()
导入pygame
pygame.init()
window=pygame.display.set_模式((600480))
pygame.display.set_标题(“第一场游戏”)
#加载字符图像
walkRight=[pygame.image.load('C:/Users/Admin/Documents/Python-Tut/FirstGame/R1.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R2.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R3.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R4.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R5.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R6.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R7.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R8.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R9.png')]
walkLeft=[pygame.image.load('C:/Users/Admin/Documents/Python-Tut/FirstGame/L1.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L2.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L3.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L4.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L5.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L6.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L7.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L8.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L9.png')]
bg=pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/bg.jpg'))
char=pygame.image.load('C:/Users/Admin/Documents/Python-Tut/FirstGame/standing.png'))
clock=pygame.time.clock()
#性格
类播放器(对象):
定义初始值(自、x、y、宽度、高度):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.vel=5
self.isJump=False
self.jumpCount=10
self.left=False
self.right=False
self.walkCount=0
自立
self.hitbox=(self.x+17,self.y+11,29,52)
def绘图(自,窗口):
如果self.walkCount+1>=27:
self.walkCount=0
如果不是(自立):
如果self.left:
blit(walkLeft[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
elif self.right:
blit(walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
其他:
如果你是对的:
blit(walkRight[0],(self.x,self.y))
其他:
blit(walkLeft[0],(self.x,self.y))
self.hitbox=(self.x+17,self.y+11,29,52)
pygame.draw.rect(窗口,(255,0,0),self.hitbox,2)
类(对象):
定义初始(自、x、y、半径、面、颜色):
self.x=x
self.y=y
自半径=半径
self.color=颜色
自我面对
self.vel=8*端面
def绘图(自,窗口):
pygame.draw.circle(窗口,self.color,(self.x,self.y),self.radius)
#敌人
类敌人(对象):
walkRight=[pygame.image.load('C:/Users/Admin/Documents/Python-Tut/FirstGame/R1E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R2E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R3E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R4E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R5E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R6E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R7E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R8E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R9E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R10E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/R11E.png')]
walkLeft=[pygame.image.load('C:/Users/Admin/Documents/Python-Tut/FirstGame/L1E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L2E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L3E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L4E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L5E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L6E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L7E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L8E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L9E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L10E.png'),
pygame.image.load('C:/Users/Admin/Documents/pythontut/FirstGame/L11E.png')]
定义初始(自、x、y、宽度、高度、终点):
self.x=x
self.y=y
self.width=宽度
自我高度=高度
self.end=结束
self.path=[self.x,self.end]
self.walkCount=0
self.vel=3
self.hitbox=(self.x+17,self.y+2,31,57)
def绘图(自,窗口):
self.move()
如果self.walkCount+1>=33:
self.walkCount=0
如果self.vel>0:
blit(self.walkRight[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
其他:
blit(self.walkLeft[self.walkCount//3],(self.x,self.y))
self.walkCount+=1
self.hitbox=(self.x+17,self.y+
bullet.x += bullet.vel.x