python 3.3列表错误-SPRITES

时间:2014-06-08 20:07:40

标签: python

现在这是我的游戏,但由于某些原因,我一直得到一个

PYTHON GAME.py", line 319, in <module>
    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
  File "C:\Python33\lib\site-packages\pygame\sprite.py", line 1515, in spritecollide
    if spritecollide(s.rect):
AttributeError: 'Rocket' object has no attribute 'rect'

错误。我不知道为什么和香港专业教育学院尝试了很多东西让它工作。

这个愚蠢的事情一直说我需要很多代码,所以我写了这个篇幅来占用空间


import pygame
import random

"""
Global constants
"""

# Colors
BLACK    = (   0,   0,   0)
WHITE    = ( 255, 255, 255)
BLUE     = (   0,   0, 255)
BLACK = [  0,   0,   0]
WHITE = [255, 255, 255]
black = [ 0,0,0]
white=[255,255,255]
green    = (   0, 255,   0)
red      = ( 255,   0,   0)
BLUE     = (   0,    0,  255)
lightblue= (   0, 255, 255)
brown    =( 97,  66,   12)
yellow  =(232,232,74)
grey=(148,148,148)
purple=(124,102,196)
yellow2 =(252,252,0)
yellow3 =(252,252,0)
red2=(255,0,0)
brown2 =(51,32,5)
orange = (255,119,0)
a=random.randrange(0,255,1)
b=random.randrange(0,255,1)
c=random.randrange(0,255,1)
#color=(a,b,c)
score=0
x=50

#game_sound = pygame.mixer.Sound("nunu_nights.wav")

# Screen dimensions
SCREEN_WIDTH  = 1000
SCREEN_HEIGHT = 700

# This class represents the bar at the bottom that the player controls
class Player(pygame.sprite.Sprite):
    """ This class represents the bar at the bottom that the player controls. """

    # Set speed vector
    change_x = 0
    change_y = 0
    walls = None

    # Constructor function
    def __init__(self, x, y):
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        # Set height, width
        self.image = pygame.Surface([15, 15])
        self.image.fill(WHITE)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

    def changespeed(self, x, y):
        """ Change the speed of the player. """
        self.change_x += x
        self.change_y += y

    def update(self):
        """ Update the player position. """
        # Move left/right
        self.rect.x += self.change_x

        # Did this update cause us to hit a wall?
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:
            # If we are moving right, set our right side to the left side of the item we hit
            if self.change_x > 0:
                self.rect.right = block.rect.left
            else:
                # Otherwise if we are moving left, do the opposite.
                self.rect.left = block.rect.right

        # Move up/down
        self.rect.y += self.change_y

        # Check and see if we hit anything
        block_hit_list = pygame.sprite.spritecollide(self, self.walls, False)
        for block in block_hit_list:

            # Reset our position based on the top/bottom of the object.
            if self.change_y > 0:
                self.rect.bottom = block.rect.top
            else:
                self.rect.top = block.rect.bottom

class Wall(pygame.sprite.Sprite):
    """ Wall the player can run into. """
    def __init__(self, x, y, width, height):
        """ Constructor for the wall that the player can run into. """
        # Call the parent's constructor
        pygame.sprite.Sprite.__init__(self)

        # Make a black wall, of the size specified in the parameters
        self.image = pygame.Surface([width, height])
        self.image.fill(BLACK)

        # Make our top-left corner the passed-in location.
        self.rect = self.image.get_rect()
        self.rect.y = y
        self.rect.x = x

class Rocket(pygame.sprite.Sprite):
    """ Rocket Class. """
    x=0
    y=0
    change_x=0
    change_y=0
    size=10
    color=[255,   0,   0]

    def move(self):
        self.x+=self.change_x
        self.y+=self.change_y

    def draw(self,screen):
        pygame.draw.circle(screen,self.color,[self.x,self.y], self.size)



# Call this function so the Pygame library can initialize itself
pygame.init()

# Create an 800x600 sized screen
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])

# Set the title of the window
pygame.display.set_caption('ROCKETS EVERYWHERE!!')

all_sprite_list = pygame.sprite.Group()

# Create an empty array
rocket_list = []

# Loop 50 times and add a rocket in a random x,y position
for i in range(100):
    x = random.randrange(0, 1000)
    y = random.randrange(0, 700)
    rocket_list.append([x, y])


rect_x = 50
rect_y = 50

# Speed and direction of rectangle
rect_change_x = 5
rect_change_y = 5

# This is a font we use to draw text on the screen (size 36)
font = pygame.font.Font(None, 36)


# List to hold all the sprites
all_sprite_list = pygame.sprite.Group()

# Make the walls. (x_pos, y_pos, width, height)
wall_list = pygame.sprite.Group()

wall = Wall(0, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)

wall = Wall(10, 0, 1000, 10)
wall_list.add(wall)
all_sprite_list.add(wall)

wall = Wall(990, 0, 10, 800)
wall_list.add(wall)
all_sprite_list.add(wall)

wall = Wall(0, 690, 1000, 10)
wall_list.add(wall)
all_sprite_list.add(wall)

block_list = pygame.sprite.Group()
all_sprites_list = pygame.sprite.Group()

# Create the player object
player = Player(500, 600)
player.walls = wall_list

for i in range(50):
    enemyRocket1 = Rocket()                         
    enemyRocket1.change_x=2
    enemyRocket1.change_y=2
    enemyRocket1.color=[a,b,c]
    enemyRocket1.x=random.randrange(0,1000,1)
    enemyRocket1.y=random.randrange(0,700,1)
    block_list.add(enemyRocket1)
    all_sprites_list.add(enemyRocket1)

'''
enemyRocket2 = Rocket()
enemyRocket2.x=50
enemyRocket2.y=0                           
enemyRocket2.change_x=0
enemyRocket2.change_y=10
enemyRocket2.color=[a,b,c]
'''

all_sprite_list.add(player)

clock = pygame.time.Clock()

display_instructions = True
instruction_page = 1
done= False
# -------- Instruction Page Loop -----------
while done==False and display_instructions:
    for event in pygame.event.get(): # User did something
        if event.type == pygame.QUIT: # If user clicked close
            done=True # Flag that we are done so we exit this loop
        if event.type == pygame.MOUSEBUTTONDOWN:
            instruction_page += 1
            if instruction_page == 3:
                display_instructions = False

    # Set the screen background
    screen.fill(black)

    if instruction_page == 1:
        # Draw instructions, page 1
        # This could also load an image created in another program.
        # That could be both easier and more flexible.

        #random.play()

        #screen.blit(title_intro, [0,0])

        text=font.render("ROCKETS EVERYWHERE!!", True, white)
        screen.blit(text, [10, 10])

        text=font.render("Click mouse to see the instructions", True, white)
        screen.blit(text, [10, 40])

    if instruction_page == 2:
        # Draw instructions, page 2
        text=font.render("The objective of the game is to dodge rockets", True, white)
        screen.blit(text, [10, 10])

        text=font.render("Use the Arrow keys to move around the screen", True, white)
        screen.blit(text, [10, 40])

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

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


#------------------------
#MAIN PROGRAM LOOP
#------------------------


done = False

while not done:

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            done = True

        # Set the speed based on the key pressed
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                player.changespeed(-14, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(14, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, -7)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, 7)

        # Reset speed when key goes up
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT:
                player.changespeed(14, 0)
            elif event.key == pygame.K_RIGHT:
                player.changespeed(-14, 0)
            elif event.key == pygame.K_UP:
                player.changespeed(0, 7)
            elif event.key == pygame.K_DOWN:
                player.changespeed(0, -7)

    all_sprite_list.update()
    player.update()
    screen.fill(BLACK)

    all_sprite_list.draw(screen)

    #The score
    text=font.render("Score="+str(score), True, white)
    screen.blit(text, [10, 10])


    a=random.randrange(0,255,1)
    b=random.randrange(0,255,1)
    c=random.randrange(0,255,1)
    color=(a,b,c)



    enemyRocket1.move()
    enemyRocket1.draw(screen)

    blocks_hit_list = pygame.sprite.spritecollide(player, block_list, True)
    # Check the list of collisions.
    for block in blocks_hit_list:
        score+=99999999999

    if score>x:

        x+=100



    for i in range(len(rocket_list)):

        # Draw the rocket
        pygame.draw.circle(screen, WHITE, rocket_list[i], 2)



        # Move the rocket down one pixel
        rocket_list[i][1] += 10

        # If the rocket has moved off the bottom of the screen
        if rocket_list[i][1] > 700:
            # Reset it just above the top
            y = random.randrange(-50, -10)
            rocket_list[i][1] = y
            # Give it a new x position
            x = random.randrange(0, 1000)
            rocket_list[i][0] = x


    score+=1







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

pygame.quit ()

1 个答案:

答案 0 :(得分:0)

如错误消息所示,您正在尝试访问rect的属性Rocket。 查看您的Rocket类,Rocket对象确实没有这样的属性。