简体   繁体   中英

Getting pygame.Surface Error. How do I fix?

I use Python 3.7.4 and Pygame 1.9.6. I'm trying to create just a test game so I can save data like a score per say using pickle this is my first time. But I'll get down that path once I'm done with the test game.

I'm getting an error two separate occasions in ball(ballX[i], ballY[i], i) and in screen.blit(ballImg, (x, y), i) . It's a TypeError: arugument 1 must be pygame.Surface, not list.

Full Trace Back:

Traceback (most recent call last):
  File "C:Test/main.py", line 128, in <module>
ball(ballX[i], ballY[i], i)
  File "C:/Test/main.py", line 66, in ball
screen.blit(ballImg, (x, y), i)

The code:

import pygame
import random

# Ball
ballImg = []
ballX = []
ballY = []
num_of_balls = 4
for i in range(num_of_balls):
    ballImg.append(pygame.image.load('ball.png'))
    ballX.append(random.randint(0, 736))
    ballY.append(random.randint(50, 535))

'''This code is above the while running loop'''
def ball(x, y, i):
    screen.blit(ballImg, (x, y), i)

running = True
while running:
'''The key stuff and what not'''
'''I have the screen color load before the image appears'''
        for i in range(num_of_balls):
            ball(ballX[i], ballY[i], i)
    pygame.display.update()

I'm not understanding what I'm doing wrong in my function/list or evening bliting the image. I feel like I'm doing everything right filling out the parameters, etc. I appreciate the advice. So thank you.

A few things:

  • As @Ewong mentioned, you are passing ballImg to blit instead of ballImg[i]
  • You're not processing OS messages, so the program will freeze

Try this code:

import pygame
import random

scr_width = 800
scr_height = 600
screen = pygame.display.set_mode((scr_width, scr_height))


# Ball
ballImg = []
ballX = []
ballY = []
num_of_balls = 4
for i in range(num_of_balls):
    ballImg.append(pygame.image.load('ball.png'))
    ballX.append(random.randint(0, 736))
    ballY.append(random.randint(50, 535))

#'''This code is above the while running loop'''
def ball(x, y, i):
    screen.blit(ballImg[i], (x, y))

running = True
while running:
#'''The key stuff and what not'''
#'''I have the screen color load before the image appears'''
    for i in range(num_of_balls):
         ball(ballX[i], ballY[i], i)
    for event in pygame.event.get():   # process OS messages
       if event.type == pygame.QUIT:
           pygame.quit()
           break

    pygame.display.update()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM