I’ve been following the python flappy bird ai tutorial from techwithtim, and I get this error;
JavaScript
x
11
11
1
Traceback (most recent call last):
2
File "c:UserskahraDesktopprojectsflappy bird aiflappy_bird_ai.py", line 109, in <module>
3
main()
4
File "c:UserskahraDesktopprojectsflappy bird aiflappy_bird_ai.py", line 103, in main
5
draw_window(win, bird)
6
File "c:UserskahraDesktopprojectsflappy bird aiflappy_bird_ai.py", line 90, in draw_window
7
bird.draw(win)
8
File "c:UserskahraDesktopprojectsflappy bird aiflappy_bird_ai.py", line 78, in draw
9
rotated_image = pygame.transform.rotate(self.img, self.tilt)
10
TypeError: argument 1 must be pygame.Surface, not list
11
My code is like this
JavaScript
1
109
109
1
import pygame
2
import neat
3
import time
4
import os
5
import random
6
7
WIN_WIDTH = 600
8
WIN_HEIGHT = 800
9
BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png")))], [pygame.transform.scale2x(
10
pygame.image.load(os.path.join("imgs", "bird2.png")))], [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
11
PIPE_IMG = pygame.transform.scale2x(
12
pygame.image.load(os.path.join("imgs", "pipe.png")))
13
BASE_IMG = pygame.transform.scale2x(
14
pygame.image.load(os.path.join("imgs", "base.png")))
15
BG_IMG = pygame.transform.scale2x(
16
pygame.image.load(os.path.join("imgs", "bg.png")))
17
18
19
class Bird:
20
IMGS = BIRD_IMGS
21
MAX_ROTATION = 25
22
ROT_VEL = 20
23
ANIMATION_TIME = 5
24
25
def __init__(self, x, y):
26
self.x = x
27
self.y = y
28
self.tilt = 0
29
self.tick_count = 0
30
self.vel = 0
31
self.height = self.y
32
self.img_count = 0
33
self.img = self.IMGS[0]
34
35
def jump(self):
36
self.vel = -10.5
37
self.tick_count = 0
38
self.height = self.y
39
40
def move(self):
41
self.tick_count += 1
42
43
d = self.vel*self.tick_count + 1.5*self.tick_count**2
44
if d >= 16:
45
d = 16
46
47
if d < 0:
48
d -= 2
49
50
self.y = self.y + d
51
52
if d < 0 or self.y > self.height + 50:
53
if self.tilt < self.MAX_ROTATION:
54
self.tilt = self.MAX_ROTATION
55
else:
56
if self.tilt > -90:
57
self.tilt -= self.ROT_VEL
58
59
def draw(self, win):
60
self.img_count += 1
61
62
if self.img_count < self.ANIMATION_TIME:
63
self.img = self.IMGS[0]
64
elif self.img_count < self.ANIMATION_TIME*2:
65
self.img = self.IMGS[1]
66
elif self.img_count < self.ANIMATION_TIME*3:
67
self.img = self.IMGS[2]
68
elif self.img_count < self.ANIMATION_TIME*4:
69
self.img = self.IMGS[1]
70
elif self.img_count == self.ANIMATION_TIME*4+1:
71
self.img = self.IMGS[0]
72
self.img_count = 0
73
74
if self.tilt <= -80:
75
self.img = self.IMGS[1]
76
self.img_count = self.ANIMATION_TIME*2
77
78
rotated_image = pygame.transform.rotate(self.img, self.tilt)
79
new_rect = rotated_image.get_rect(
80
center=self.img.get_rect(topleft=(self.x, self.y)).center)
81
win.blit(rotated_image, new_rect.topleft)
82
83
def get_mask(self):
84
return pygame.mask.from_surface(self.img)
85
86
87
def draw_window(win, bird):
88
win.blit(BG_IMG, (0, 0))
89
bird.draw(win)
90
pygame.display.update()
91
92
93
def main():
94
bird = Bird(200, 200)
95
win = pygame.display.set_mode((WIN_WIDTH, WIN_HEIGHT))
96
97
run = True
98
while run:
99
for event in pygame.event.get():
100
if event.type == pygame.QUIT:
101
run = False
102
draw_window(win, bird)
103
104
pygame.quit()
105
quit()
106
107
108
main()
109
I read the other posts with the same problem and I understand it, but BG_IMG
is not a list like BIRD_IMGS
. I don’t know what to do.
I tried doing BG_IMGS[0]
to get the first image of the “list” but it says the object is not a list. Why?
Advertisement
Answer
[i1], [i2], [i3]
is not the same as [i1, i2, i3]
The following is not a list, but a tuple with 3 components, where each component is a list with 1 element:
JavaScript141BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png")))],
2[pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png")))],
3[pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
4
Change it:
JavaScript
1
4
1
BIRD_IMGS = [pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird1.png"))),
2
pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird2.png"))),
3
pygame.transform.scale2x(pygame.image.load(os.path.join("imgs", "bird3.png")))]
4