精灵和图片问题。精灵方法.draw()由于某些原因无法正常工作

艾迪生

我收到以下错误。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图像并将其命名为“ blue-car-hi.png”或其他名称,以在其中显示图像地点。

r

方法self.image中将汽车的设置为,但这是一个字符串,而不是pygame.Surface。有同样的问题。CAR_IMAGE_RIGHTmove_car_rightmove_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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

由于某些神秘问题,htaccess无法正常工作

glDrawArrays由于某些原因无法正常工作?

CSS由于某些原因无法正常工作

由于某些原因,SortProperties无法正常工作

无法使用简化的 Draw 方法翻转精灵

由于背光问题,挂起无法正常工作

AngularJS ng-include由于某些原因无法正常工作

由于某些未知原因,PHP INSERT无法正常工作

WebSocket服务器由于某些原因无法正常工作

JavaScript smoothscroll由于某些原因无法正常工作

HashMap.containsKey由于某些原因无法正常工作

该代码由于某些原因无法正常工作

MatPlotLib的ion()和draw()无法正常工作

链接由于背景图片而无法正常工作

pygame.draw.rect 无法正常工作,只能绘制精灵组的一部分

由于 Playstore 缓存问题,应用内更新无法正常工作

精灵动画:在 for 循环中无法识别函数和分号

由于错误的编码,oninput和onchange无法正常工作

使精灵无法移动

QueryStringConverter由于未知原因无法正常工作

:hover由于背景原因无法正常工作

做游戏时,我无法导入背景图片或精灵

我正在尝试制作此“ fps”游戏,由于某些原因,mouseClicked函数无法正常工作

由于某些原因,我的拉普拉斯过滤器程序无法正常工作

由于某些原因,点击无法正常工作..我哪里出错了?

我正在尝试使用Java重命名文件,但是由于某些原因它无法正常工作

我有 Python PyQt5 问题,由于某些原因我无法运行代码

JavaFX和精灵动画,如何制作动画循环来更改播放器精灵的图片?

无法阻止精灵移出屏幕