gpt4 book ai didi

python - 制作简单的 flappy bird 游戏时如何修复错误 "TypeError: argument 1 must be pygame.Surface, not list"?

转载 作者:行者123 更新时间:2023-11-28 16:56:25 25 4
gpt4 key购买 nike

我正在关注 youtube 上的一个教程,该教程使用 python 和 pygame 制作了一个 flappy bird 游戏。到目前为止,我已经完成了他所做的工作,但是当我尝试运行代码时出现错误:

类型错误:参数 1 必须是 pygame.Surface,而不是列表

我真的不知道是什么导致了这个问题。

我已经尝试使用谷歌搜索并找到了一些可能回答我的问题的线程,但是因为我在 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()

最佳答案

错误是由于在

win.blit(BG_IMG, (0,0))

BG_IMG 是一个只有一个元素的列表:

BG_IMG = [pygame.transform.scale2x(pygame.image.load("imgs/bg.png"))]

获取列表的第一个元素来解决问题(BG_IMG[0]):

win.blit(BG_IMG[0], (0,0))

当然,您可以将 BG_IMG 设为单个表面而不是表面列表:

BG_IMG = pygame.transform.scale2x(pygame.image.load("imgs/bg.png"))

此外 self.vel = 0Bird 的构造函数中缺少:

class Bird:
# [...]

def __init__(self,x,y):
self.vel = 0
# [...]

关于python - 制作简单的 flappy bird 游戏时如何修复错误 "TypeError: argument 1 must be pygame.Surface, not list"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57913412/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com