精灵和图像的问题。 sprite方法.draw()由于某种原因不起作用

时间:2018-01-28 21:14:48

标签: python-3.x sprite

我收到以下错误。 TypeError:参数1必须是pygame.Surface,而不是str

我一直在研究如何格式化图像,似乎没有任何错误。无论如何,这是我的代码:

import pygame
from random import randrange
pygame.init()



WHITE=(255, 255, 255)
BLACK=(0, 0, 0,)
RED=(255, 0, 0)
BLUE=(0, 0, 255)
GREEN=(0,255,0)
GRAY=(180,180,180)
MAROON=(128,0,0)
NAVY=(0,0,128)
YELLOW=(255,255,0)
LIGHT_GRAY=(250,250,250)
LIGHT_BLUE=(144, 195, 212)
CAR_IMAGE_RIGHT = "blue-car-hi.png"
CAR_IMAGE_LEFT = "blue-car-hi copy.png"


DISPLAY_WIDTH=800
DISPLAY_HEIGHT=600


all_sprites=pygame.sprite.Group()
meteor_sprites=pygame.sprite.Group()
car_sprite=pygame.sprite.Group()
FPS=60

screen=pygame.display.set_mode((DISPLAY_WIDTH, DISPLAY_HEIGHT))
screen.fill(WHITE)

clock=pygame.time.Clock()

finished=False




class car(pygame.sprite.Sprite):
        #This will create a car and allow the user to move it
        def __init__(self, image, x, y):

            pygame.sprite.Sprite.__init__(self)
            self.image = pygame.image.load(image)
            #self.image.set_colorkey(BLACK)
            self.rect = self.image.get_rect()
            self.x=self.rect.x
            self.y=self.rect.y

        def draw_car(self):
            self.x=self.rect.x
            self.y=self.rect.x




        def move_car_right(self):
            self.rect.x+=4
            self.image = CAR_IMAGE_RIGHT


        def move_car_left(self):
            self.rect.x-=4
            self.image = CAR_IMAGE_LEFT

        def move_car_up(self):
            self.rect.y-=4

        def move_car_down(self):
            self.rect.y+=4

        def check_boundary(self, mode):
            #constraining the area in which the player can move while playing
            if mode=='play':
                if self.rect.y >= DISPLAY_HEIGHT:
                    self.rect.y = DISPLAY_HEIGHT-30
                if self.rect.y <= DISPLAY_HEIGHT-100:
                    self.rect.y = DISPLAY_HEIGHT-100
                if self.rect.x <= 0:
                    self.rect.x=0
                if self.rect.x >= DISPLAY_WIDTH-60:
                    self.rect.x=DISPLAY_WIDTH-60

            #constraining the area in which the player can move while roaming
            if mode=='roam':
                if self.rect.y >= DISPLAY_HEIGHT-30:
                    self.rect.y = DISPLAY_HEIGHT-30
                if self.rect.y <= DISPLAY_HEIGHT-300:
                    self.rect.y = DISPLAY_HEIGHT-300
                if self.rect.x <= 0:
                    self.rect.x=0
                if self.rect.x >= DISPLAY_WIDTH-60:
                    self.rect.x=DISPLAY_WIDTH-60

class meteors(pygame.sprite.Sprite):
    def __init__(self):
        pygame.sprite.Sprite.__init__(self)

        self.image = pygame.image.load("meteor.png")
        self.image.set_colorkey(BLACK)
        self.rect = self.image.get_rect()

    def update(self):
        self.rect.y+=1
        if self.rect.y > DISPLAY_HEIGHT:
            self.rect.y = randrange(-100, -10)
            self.rect.x = randrange(0, DISPLAY_WIDTH)














 # Allowing the Keys to be used to move the car

def keys(mode):
    if event.type == pygame.KEYDOWN:

        if event.key == pygame.K_RIGHT:
            car1.move_car_right()
            if pressed != True:
                if mode=='play':
                    car1.check_boundary('play')
                elif mode=='roam':
                    car1.check_boundary('roam')
            car1.draw_car()

        elif event.key == pygame.K_LEFT:
            car1.move_car_left()
            if pressed != True:
                if mode=='play':
                    car1.check_boundary('play')
                elif mode=='roam':
                    car1.check_boundary('roam')
            car1.draw_car()

        elif event.key == pygame.K_UP:

            if pressed != True:
                if mode=='play':
                    car1.check_boundary('play')
                elif mode=='roam':
                car1.check_boundary('roam')
            car1.move_car_up()
            car1.draw_car()

        elif event.key == pygame.K_DOWN:
            car1.move_car_down()
            if pressed != True:
                if mode=='play':
                    car1.check_boundary('play')
                elif mode=='roam':
                    car1.check_boundary('roam')
            car1.draw_car()









car1=car(CAR_IMAGE_RIGHT, 100, 200)
all_sprites.add(car1)
pressed = False
mode='roam'
meteor_counter=0

while not finished:
    for event in pygame.event.get():
        if event.type==pygame.QUIT:
            finished=True

    pygame.draw.rect(screen, BLUE, [0, 0, DISPLAY_WIDTH, DISPLAY_WIDTH-500],0)


    pygame.draw.rect(screen,GREEN, [0,DISPLAY_HEIGHT-300,DISPLAY_WIDTH,200],0)
    pygame.draw.rect(screen, GRAY, [0,DISPLAY_HEIGHT-100,DISPLAY_WIDTH,DISPLAY_HEIGHT-500],0)
                                #0    #500                #800            #100

    car1.draw_car()
    keys(mode)



    pos = pygame.mouse.get_pos()

    if pos[0] >= (DISPLAY_WIDTH / 2) - 20 and pos[0] <= (((DISPLAY_WIDTH / 2) - 20) + (DISPLAY_WIDTH - 720)) and pos[
    1] >= (DISPLAY_HEIGHT - 335) and pos[1] <= ((DISPLAY_HEIGHT - 335) + (DISPLAY_HEIGHT - 575)):
        if pygame.mouse.get_pressed()[0] == 1:
            print('clicked!')
            mode='play'
            car1.draw_car()
            car_sprite.add(car1)
    if pos[0] >= (DISPLAY_WIDTH / 2) - 20 and pos[0] <= (((DISPLAY_WIDTH / 2) - 20) + (DISPLAY_WIDTH - 720)) and pos[1] >= (DISPLAY_HEIGHT - 290) and pos[1] <= ((DISPLAY_HEIGHT - 290) + (DISPLAY_HEIGHT - 575)):
        if pygame.mouse.get_pressed()[0] == 1:
            print('clicked!2')
            mode='roam'
            car1.draw_car()


    # Making buttons to play the game
    pygame.draw.rect(screen, GREEN,[(DISPLAY_WIDTH / 2) - 20, DISPLAY_HEIGHT - 335, DISPLAY_WIDTH - 720, DISPLAY_HEIGHT - 575], 0)
    pygame.draw.rect(screen, BLUE,[(DISPLAY_WIDTH / 2) - 20, DISPLAY_HEIGHT - 290, DISPLAY_WIDTH - 720, DISPLAY_HEIGHT - 575], 0)

    font = pygame.font.SysFont('Helvetica', 25, True, True)
    text = font.render("Play", True, BLACK)
    screen.blit(text, [((DISPLAY_WIDTH / 2) - 18), ((DISPLAY_HEIGHT - 332))])
    font2 = pygame.font.SysFont('Helvetica', 25, True, False)
    text2 = font.render("Roam", True, BLACK)
    screen.blit(text2, [((DISPLAY_WIDTH / 2) - 18), ((DISPLAY_HEIGHT - 290))])


    if mode=='play':
        meteor_counter += 1
        if meteor_counter<2:

            for val in range(20):
                meteor = meteors()
                meteor.rect.x = randrange(DISPLAY_WIDTH)
                meteor.rect.y = randrange(DISPLAY_HEIGHT)
                meteor_sprites.add(meteor)
                all_sprites.add(meteor)
                meteors()

    collision=pygame.sprite.spritecollide(car1, meteor_sprites, False)
    if collision:
        #mode='lost'
        print("HIT")




    all_sprites.update()
    all_sprites.draw(screen)

    clock.tick(FPS)
    pygame.display.flip()


pygame.quit()

顺便说一句,这些图片显然不会为你加载,所以如果你想运行它,只需获取任何jpeg图像并命名为&#34; blue-car-hi.png&#34;或其他名称,以显示图像。

1 个答案:

答案 0 :(得分:1)

您在self.image方法中将汽车的CAR_IMAGE_RIGHT设置为move_car_right,但它不是pygame.Surface的字符串。 move_car_left有同样的问题。

CAR_IMAGE_RIGHT = "blue-car-hi.png"

def move_car_right(self):
    self.rect.x+=4
    self.image = CAR_IMAGE_RIGHT  # Now self.image is a string instead of a surface.

在游戏开始时加载您的图片并在游戏中重复使用。

# Load the images in the global scope and use the convert
# or convert_alpha methods afterwards to improve the performance.
CAR_IMAGE_RIGHT = pygame.image.load("blue-car-hi.png").convert_alpha()
CAR_IMAGE_LEFT = pygame.image.load("blue-car-hi copy.png").convert_alpha()