Python 我如何修复错误;TypeError:参数1必须是pygame.Surface,而不是list“;当制作一个简单的飞禽游戏时?
我正在关注youtube上的一个教程,该教程使用python和pygame制作了一个flappy bird游戏。到目前为止,我已经完成了他所做的工作,但是当我尝试运行代码时,我得到了一个错误:Python 我如何修复错误;TypeError:参数1必须是pygame.Surface,而不是list“;当制作一个简单的飞禽游戏时?,python,pygame,Python,Pygame,我正在关注youtube上的一个教程,该教程使用python和pygame制作了一个flappy bird游戏。到目前为止,我已经完成了他所做的工作,但是当我尝试运行代码时,我得到了一个错误: TypeError:参数1必须是pygame.Surface,而不是list 我真的不知道是什么导致了这个问题 我尝试过谷歌搜索,找到了一些对我的问题有潜在答案的线程,但因为我在python和pygame方面没有太多经验,所以我很难理解其他人的代码与我的代码有什么共同之处来解决这个问题 这是我的密码: i
TypeError:参数1必须是pygame.Surface,而不是list
我真的不知道是什么导致了这个问题
我尝试过谷歌搜索,找到了一些对我的问题有潜在答案的线程,但因为我在python和pygame方面没有太多经验,所以我很难理解其他人的代码与我的代码有什么共同之处来解决这个问题
这是我的密码:
import pygame
import neat
import time
import random
import os
WIN_WIDTH= 600
WIN_HEIGHT= 800
BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load("imgs/bird1.png")), pygame.transform.scale2x(pygame.image.load("imgs/bird2.png")), pygame.transform.scale2x(pygame.image.load(("imgs/bird3.png")))]
PIPE_IMG = [pygame.transform.scale2x(pygame.image.load("imgs/pipe.png"))]
BASE_IMG = [pygame.transform.scale2x(pygame.image.load("imgs/base.png"))]
BG_IMG = [pygame.transform.scale2x(pygame.image.load("imgs/bg.png"))]
class Bird:
IMGS = BIRD_IMGS
MAX_ROTATION = 25
ROT_VEL = 20
ANIMATION_TIME = 5
def __init__(self,x,y):
self.x=x
self.y=y
self.tilt = 0
self.tick_count = 0
self.velocity = 0
self.height = self.y
self.img_count=0
self.img=self.IMGS[0]
def jump(self):
self.vel=-10.5
self.tick_count = 0
self.height = self.y
def move(self):
self.tick_count +=1
d = self.vel*self.tick_count + 1.5*self.tick_count**2
if d >= 16:
d = 16
if d < 0:
d -= 2
self.y=self.y+d
if d < 0 or self.y < self.height + 50:
if self.tilt < sel.MAX_ROTATION:
self.tilt=self.MAX_ROTATION
else:
if self.tilt>-90:
self.tilt-=self.ROT_VEL
def draw(self,win):
self.img_count+=1
if self.img_count<self.ANIMATION_TIME:
self.img=self.IMGS[0]
elif self.img_count<self.ANIMATION_TIME*2:
self.img=self.IMGS[1]
elif self.img_count<self.ANIMATION_TIME*3:
self.img=self.IMGS[2]
elif self.img_count<self.ANIMATION_TIME*4:
self.img=self.IMGS[1]
elif self.img_count==self.ANIMATION_TIME*4+1:
self.img=self.IMGS[0]
self.img_count=0
if self.tilt <= -80:
self.img=self.IMGS[1]
self.img_count=self.ANIMATION_TIME*2
rotated_image = pygame.transform.rotate(self.img, self.tilt)
new_rect = rotated_image.get_rect(center=self.img.get_rect(topleft=(self.x, self.y)).center)
win.blit(rotated_image, new_rect.topleft)
def get_mask(self):
return pygame.mask.from_surface(self.img)
((((THIS IS WHERE I GET MY ERROR CODE))))))
def draw_window(win, bird):
HERE--> win.blit(BG_IMG, (0,0))
bird.draw(win)
pygame.display.update()
((((ABOVE HERE IS WHERE I GET MY ERROR CODE))))))
def main():
bird=Bird(200,200)
win=pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
run=True
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run=False
draw_window(win,bird)
pygame.quit()
quit()
main()
导入pygame
输入整洁
导入时间
随机输入
导入操作系统
WIN_宽度=600
WIN_高度=800
BIRD_IMGS=[pygame.transform.scale2x(pygame.image.load(“IMGS/bird1.png”)),pygame.transform.scale2x(pygame.image.load(“IMGS/bird2.png”)),pygame.transform.scale2x(pygame.image.load(“IMGS/bird3.png”))]
PIPE_IMG=[pygame.transform.scale2x(pygame.image.load(“imgs/PIPE.png”)]
BASE_IMG=[pygame.transform.scale2x(pygame.image.load(“imgs/BASE.png”)]
BG_IMG=[pygame.transform.scale2x(pygame.image.load(“imgs/BG.png”)]
类鸟:
IMGS=鸟
最大旋转=25
ROT_等级=20
动画时间=5
定义初始化(self,x,y):
self.x=x
self.y=y
self.tilt=0
self.tick_count=0
自速度=0
self.height=self.y
self.img\u计数=0
self.img=self.IMGS[0]
def跳转(自):
self.vel=-10.5
self.tick_count=0
self.height=self.y
def移动(自我):
self.tick_count+=1
d=自我水平*self.tick\u计数+1.5*self.tick\u计数**2
如果d>=16:
d=16
如果d<0:
d-=2
self.y=self.y+d
如果d<0或self.y-90:
自倾斜-=自旋转水平
def抽签(自我,赢):
自身img_计数+=1
如果self.img_count则会导致错误,因为
win.blit(BG_IMG,(0,0))
BG\u IMG
是一个包含单个元素的列表:
BG_IMG=[pygame.transform.scale2x(pygame.image.load(“imgs/BG.png”)]
获取列表的第一个元素以解决问题(BG\u IMG[0]
):
win.blit(BG_IMG[0],(0,0))
当然,您可以将BG\u IMG
设置为单个曲面,而不是曲面列表:
BG_IMG=pygame.transform.scale2x(pygame.image.load(“imgs/BG.png”))
此外,类的构造函数Bird
中缺少self.vel=0
:
类鸟:
# [...]
定义初始化(self,x,y):
self.vel=0
# [...]
我没有运行代码,但是查看文档,您可能想使用win=pygame.display.get\u surface()
。我认为set\u mode()
方法不会返回曲面。因此,在设置模式后,使用get_surface()
获得曲面,让您非常满意!这解决了问题:)现在我又遇到了一个错误。。当我在这个for循环中写入“bird.move()”时,“run=True while run:for pygame.event.get():if event.type==pygame.QUIT:run=False bird.move()draw_window(win,bird)”我得到一个错误“AttributeError:“bird”对象没有属性“vel”@Morenoself.vel=0
在构造函数中丢失(\uu init\uu
)类的Bird
。过了一会儿,出现了一个拼写错误self.MAX_ROTATION
而不是sel.MAX_ROTATION
谢谢你花时间来了解我并耐心等待,现在我真的不明白你说我缺少构造函数的int是什么意思。我应该在哪里写init?@Moreno我已经扩展了答案。请看最后一节。非常感谢!!现在我明白了:)谢谢你花时间和精力在这件事上,只是为了帮助我。祝你白天/晚上愉快!