Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我的游戏一直在为错误的精灵调用方法_Python_Pygame_Sprite - Fatal编程技术网

Python 我的游戏一直在为错误的精灵调用方法

Python 我的游戏一直在为错误的精灵调用方法,python,pygame,sprite,Python,Pygame,Sprite,当我试图运行游戏时,代码试图为错误的精灵运行一个方法。我认为行“player.handle\u keys()”是个问题,因为当我运行它时,它说它找不到“meteor”类的“handle\u keys()”方法。我没有一行代码来运行“meteor.handle_keys()”,因为这个类不应该有这个方法 代码如下: import pygame import random # Define some colors BLACK = ( 0, 0, 0) WHITE = (255, 255,

当我试图运行游戏时,代码试图为错误的精灵运行一个方法。我认为行“player.handle\u keys()”是个问题,因为当我运行它时,它说它找不到“meteor”类的“handle\u keys()”方法。我没有一行代码来运行“meteor.handle_keys()”,因为这个类不应该有这个方法

代码如下:

import pygame
import random

# Define some colors
BLACK = (  0,   0,   0)
WHITE = (255, 255, 255)
RED   = (255,   0,   0)


bg = pygame.image.load("bg1.png")




class space_ship(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
    # Create an image of the space_ship1, and fill it with a color.
    # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
    #draw image
        self.image = pygame.image.load("player1.gif").convert()

    # Draw the ellipse
        #pygame.draw.ellipse(self.image, color, [0, 0, width, height])

    # x and y coordinates
        self.x = 500
        self.y = 450



    def handle_keys(self):
            """ Handles Keys """
            key = pygame.key.get_pressed()
            dist = 5 # distance moved in 1 frame
            if key[pygame.K_RIGHT]: # right key
                self.x += dist # move right
            elif key[pygame.K_LEFT]: # left key
                self.x -= dist # move left

    def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, (self.x, self.y))




class asteroid(pygame.sprite.Sprite):
    def __init__(self, color, width, height):
        super().__init__()
    # Create an image of the space_ship1, and fill it with a color.
    # This could also be an image loaded from the disk.
        self.image = pygame.Surface([width, height])
        self.image.fill(WHITE)
        self.image.set_colorkey(WHITE)
        self.rect = self.image.get_rect()
    # Draw the ellipse
        #pygame.draw.ellipse(self.image, color, [0, 0, width, height])

        self.image = pygame.image.load("ast1.gif").convert()

    # x and y coordinates
        self.x = random.randint(50,950)
        self.y = 10

    def draw(self, surface):
        """ Draw on surface """
        # blit yourself at your current position
        surface.blit(self.image, (self.x, self.y))

    def fall(self):
        dist = 5
        self.y +=dist
        if self.y > 600:
            self.x = random.randint(50,950)
            self.y = random.randint(-2000, -10)



    def respawn(self):
        self.y = -10






# Initialize Pygame
pygame.init()

# Set the height and width of the screen
screen_width = 1000
screen_height = 600
screen = pygame.display.set_mode([screen_width, screen_height])

# This is a list of 'sprites.' Each sprite in the program is
# added to this list.
# The list is managed by a class called 'Group.'
asteroid_list = pygame.sprite.Group()

# This is a list of every sprite.
# All asteroids and the player as well.
all_sprites_list = pygame.sprite.Group()




player = space_ship(RED, 20, 15)
all_sprites_list.add(player)
asteroid_1 = asteroid(BLACK, 40, 40)
asteroid_list.add(asteroid_1)
all_sprites_list.add(asteroid_1)
asteroid_2 = asteroid(BLACK, 40, 40)
asteroid_list.add(asteroid_2)
all_sprites_list.add(asteroid_2)
asteroid_3 = asteroid(BLACK,40, 40)
asteroid_list.add(asteroid_3)
all_sprites_list.add(asteroid_3)
asteroid_4 = asteroid(BLACK,40, 40)
asteroid_list.add(asteroid_4)
all_sprites_list.add(asteroid_4)
asteroid_5 = asteroid(BLACK,40, 40)
asteroid_list.add(asteroid_5)
all_sprites_list.add(asteroid_5)
asteroid_6 = asteroid(BLACK,40, 40)
asteroid_list.add(asteroid_6)
all_sprites_list.add(asteroid_6)
asteroid_7 = asteroid(BLACK,40, 40)
asteroid_list.add(asteroid_7)
all_sprites_list.add(asteroid_7)
asteroid_8 = asteroid(BLACK,40, 40)
asteroid_list.add(asteroid_8)
all_sprites_list.add(asteroid_list)


# Loop until the user clicks the close button.
done = False

# Used to manage how fast the screen updates
clock = pygame.time.Clock()

score = 0

# ----------------- Main Program Loop --------------------
while not done:
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

    #Call upon function
    player.handle_keys()

    # Clear the screen
    screen.fill(WHITE)

    #INSIDE OF THE GAME LOOP
    screen.blit(bg, (0, 0))

    # See if the player space_ship1 has collided with anything.
    blocks_hit_list = pygame.sprite.spritecollide(player, asteroid_list, True)

    # Check the list of collisions.
    for player in blocks_hit_list:
        score +=1
        print(score)

    # Draw all the spites
    player.draw(screen)
    asteroid_1.draw(screen)
    asteroid_1.fall()
    asteroid_2.draw(screen)
    asteroid_2.fall()
    asteroid_3.draw(screen)
    asteroid_3.fall()
    asteroid_4.draw(screen)
    asteroid_4.fall()
    asteroid_5.draw(screen)
    asteroid_5.fall()
    asteroid_6.draw(screen)
    asteroid_6.fall()
    asteroid_7.draw(screen)
    asteroid_7.fall()
    asteroid_8.draw(screen)
    asteroid_8.fall()


    #all_sprites_list.draw(screen)

# Limit to 60 frames per second
    clock.tick(60)

    # Go ahead and update the screen with what we've drawn.
    pygame.display.flip()

pygame.quit()

在for循环中,您是最重要的玩家

# Check the list of collisions.
for player in blocks_hit_list:
    score +=1
    print(score)
把它换成别的东西,一切都会好起来的

# Check the list of collisions.
for something_else in blocks_hit_list:
    score +=1
    print(score)

享受吧

你在for循环中超越了玩家

# Check the list of collisions.
for player in blocks_hit_list:
    score +=1
    print(score)
把它换成别的东西,一切都会好起来的

# Check the list of collisions.
for something_else in blocks_hit_list:
    score +=1
    print(score)

享受

也许你可以使用class
meteor
创建对象
player
始终添加有问题的完整错误消息。如果你加载image
self.image=pygame.Surface(…)
,你不必创建Surface
self.image=pygame.image.load(…)
。加载的图像不在此表面上绘制-它会删除此表面并创建新的表面可能您使用class
meteor
创建对象
player
始终添加有问题的完整错误消息。如果加载图像
self.image=pygame.surface(…)
,则不必创建表面
self.image=pygame.image.load(…)
。加载的图像不会在此曲面上绘制-它会删除此曲面并创建新曲面