Skip to main content
 首页 » 编程设计

python之参数 1 必须是 pygame.surface,而不是列表

2024年11月01日8wayfarer

我正在编写一个小型 Python 游戏,但我的一些代码似乎无法运行。看看:

import pygame 
import sys 
import pygame.sprite as sprite 
import time 
pygame.init() 
 
pygame.display.set_caption("Uni Mario") 
 
 
m_x = 100 
m_y = 350 
width = 40 
height = 60 
vel = 5 
 
left=False 
right=False 
walk_count = 0 
walkRight = [] 
walkLeft = [] 
 
 
myfont = pygame.font.SysFont("monospace", 25) 
screen_over = pygame.font.SysFont("monospace", 100) 
hitcount = 0 
score=0 
 
clock = pygame.time.Clock() 
FPS = 30 
 
background_image = pygame.image.load('Sprites/bg2.png') 
background_size = background_image.get_size() 
background_rect = background_image.get_rect() 
sw = 837 
sh = 464 
win = pygame.display.set_mode((sw, sh)) 
w,h = background_size 
 
t_x = sw 
t_y = 350 
g_x = sw-100 
 
bg_x1 = 0 
bg_y1 = 0 
 
bg_x2 = w 
bg_y2 = 0 
 
d2 = 9999 
 
def load_img(file_name): # loads the image, makes the pure white background transparent 
    img = pygame.image.load(file_name).convert() 
    img.set_colorkey((255,255,255)) 
 
    return img 
 
for i in range(1,7): 
    walkLeft.append( load_img("Sprites/L" + str(i) + ".png" ) ) #loads in lists of images 
    walkRight.append( load_img("Sprites/R" + str(i) + ".png") )  
 
player_image = walkRight[0] 
 
turtle_image = [pygame.image.load('Sprites/Goomba1.png'), pygame.image.load('Sprites/Goomba2.png'), pygame.image.load('Sprites/Goomba3.png')] 
#turtle_small = pygame.transform.scale(turtle_image, (100, 60)) 
 
pygame.mixer.music.load('Music/Bros.mp3') 
pygame.mixer.music.play(-1) 
 
isJump = False 
jumpCount = 10 
 
left_idx=0 
right_idx=0 
 
#images = [] 
#images.append(pygame.image.load('Sprites/Goomba1.png')) 
#images.append(pygame.image.load('Sprites/Goomba2.png')) 
#images.append(pygame.image.load('Sprites/Goomba3.png')) 
 
#index = 0 
#  
#image = images[index] 
 
#def update(images): 
 #   index += 1 
 
#if index >= len(images): 
#    index = 0 
 
#image = images[self.index] 
 
run = True #main loop 
while run: 
    clock.tick(FPS) 
 
    pygame.time.delay(50) 
 
    win.blit(background_image,(sw, sh)) #makes a scrolling background  
    pygame.display.update() 
 
    bg_x2 -= 5 
    bg_x1 -= 5 
    if bg_x1 < sw - 2*w: 
       bg_x1 = sw 
    if bg_x2 < sw - 2*w: 
          bg_x2 = sw 
 
    t_x -=20 
    if t_x < 0: 
        t_x = sw 
 
    #g_x -= 20 
    #if g_x < 0: 
     #   g_x = sw+500 
 
 
 
    for event in pygame.event.get(): 
        if event.type == pygame.QUIT: 
            run = False 
 
    keys = pygame.key.get_pressed() 
    if keys[pygame.K_LEFT] and m_x > vel:  
        m_x -= vel 
        if not isJump: 
            player_image = walkLeft[left_idx] 
            left_idx += 1 
            if left_idx >= len(walkLeft): 
                left_idx=0 
 
    if keys[pygame.K_RIGHT] and m_x < sw - width - vel: 
        m_x += vel 
        if not isJump: 
            player_image = walkRight[right_idx] 
            right_idx += 1 
            if right_idx >= len(walkRight): 
                right_idx=0 
 
    if not(isJump):       
        if keys[pygame.K_UP]: 
            isJump = True 
        if keys[pygame.K_SPACE]: 
            isJump = True 
 
    else: 
        if jumpCount >= -10: 
            neg = 1 
            if jumpCount < 0: 
                neg = -1 
            m_y -= (jumpCount ** 2)* 0.25 * neg 
            jumpCount -= 1 
 
        else: 
            isJump = False 
            jumpCount = 10 
 
    if bg_x1 > -w:         
        win.blit(background_image,(bg_x1,bg_y1)) 
    if bg_x2 > -w: 
        win.blit(background_image,(bg_x2,bg_y2)) 
    win.blit(player_image, (m_x,m_y)) 
 
    win.blit(turtle_image, (t_x, t_y)) #22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 
 
 
 
    label = myfont.render("Hit Count = "+ str(hitcount), 1, (0, 0, 0)) 
    win.blit(label, ((sw-200), 420)) 
 
    label3 = myfont.render("If your hit count gets",1, (0, 0, 0)) 
    win.blit(label3, (270, 10)) 
 
    label4= myfont.render("to 3, you lose.", 1, (0 , 0, 0)) 
    win.blit(label4, (270, 60)) 
 
    label5 = myfont.render("Score: "+ str(score), 1, (0, 0, 0)) 
    win.blit(label5, ((sw-180), 50)) 
 
 
    pygame.display.update()  
 
 
    d2 = (t_x - m_x)**2 + (t_y - m_y)**2 #represents the distance between the two character 
    if d2 < 142: #keep it at 142, it seems to be a good distance for the hitcount 
        hitcount += 1 
    else: 
        score += 1/10 
    if hitcount >= 3: 
        label2 = screen_over.render("Game Over", 1, (255, 0, 0)) 
        win.blit(label2, (230, 200)) 
        pygame.display.update() 
        time.sleep(2) 
        run= False 
    if score >= 100: 
        label6 = screen_over.render("YOU WIN", 1, (255, 0, 0)) 
        win.blit(label6, (230,200)) 
        pygame.display.update() 
        time.sleep(5) 
        run=False 
 
pygame.quit() 

是的....所以错误是这样的:

Traceback (most recent call last): 
  File "C:\Users\aryaat\Desktop\SPython Gaming Project\Uni Mario(A).py", line 163, in <module> 
    win.blit(turtle_image, (t_x, t_y)) #22222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222 
TypeError: argument 1 must be pygame.Surface, not list 

请您参考如下方法:

turtle_image 不是 pygame.Surface 对象,而是 pygame.Surface 对象的列表:

turtle_image = [pygame.image.load('Sprites/Goomba1.png'), pygame.image.load('Sprites/Goomba2.png'), pygame.image.load('Sprites/Goomba3.png')]  

您必须使用索引运算符(例如 turtle_image[0])从列表中选择一个您想要“blit”到窗口表面的对象:

win.blit(turtle_image[0], (t_x, t_y))