python 3.3 list error-SPRITES -
this game right reason keep getting
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'
error. have no idea why , ive tried alot of stuff work.
this stupid thing keeps saying have code wrote sentance take room
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 # class represents bar @ bottom player controls class player(pygame.sprite.sprite): """ class represents bar @ bottom player controls. """ # set speed vector change_x = 0 change_y = 0 walls = none # constructor function def __init__(self, x, y): # call 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 passed-in location. self.rect = self.image.get_rect() self.rect.y = y self.rect.x = x def changespeed(self, x, y): """ change speed of player. """ self.change_x += x self.change_y += y def update(self): """ update player position. """ # move left/right self.rect.x += self.change_x # did update cause hit wall? block_hit_list = pygame.sprite.spritecollide(self, self.walls, false) block in block_hit_list: # if moving right, set our right side left side of item hit if self.change_x > 0: self.rect.right = block.rect.left else: # otherwise if moving left, opposite. self.rect.left = block.rect.right # move up/down self.rect.y += self.change_y # check , see if hit block_hit_list = pygame.sprite.spritecollide(self, self.walls, false) block in block_hit_list: # reset our position based on top/bottom of 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 player can run into. """ def __init__(self, x, y, width, height): """ constructor wall player can run into. """ # call parent's constructor pygame.sprite.sprite.__init__(self) # make black wall, of size specified in parameters self.image = pygame.surface([width, height]) self.image.fill(black) # make our top-left corner 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 function pygame library can initialize pygame.init() # create 800x600 sized screen screen = pygame.display.set_mode([screen_width, screen_height]) # set title of window pygame.display.set_caption('rockets everywhere!!') all_sprite_list = pygame.sprite.group() # create empty array rocket_list = [] # loop 50 times , add rocket in random x,y position 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 , direction of rectangle rect_change_x = 5 rect_change_y = 5 # font use draw text on screen (size 36) font = pygame.font.font(none, 36) # list hold sprites all_sprite_list = pygame.sprite.group() # make 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 player object player = player(500, 600) player.walls = wall_list 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 , display_instructions: event in pygame.event.get(): # user did if event.type == pygame.quit: # if user clicked close done=true # flag done exit loop if event.type == pygame.mousebuttondown: instruction_page += 1 if instruction_page == 3: display_instructions = false # set screen background screen.fill(black) if instruction_page == 1: # draw instructions, page 1 # load image created in program. # both easier , 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 see instructions", true, white) screen.blit(text, [10, 40]) if instruction_page == 2: # draw instructions, page 2 text=font.render("the objective of game dodge rockets", true, white) screen.blit(text, [10, 10]) text=font.render("use arrow keys move around screen", true, white) screen.blit(text, [10, 40]) # limit 20 frames per second clock.tick(20) # go ahead , update screen we've drawn. pygame.display.flip() #------------------------ #main program loop #------------------------ done = false while not done: event in pygame.event.get(): if event.type == pygame.quit: done = true # set speed based on 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 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 list of collisions. block in blocks_hit_list: score+=99999999999 if score>x: x+=100 in range(len(rocket_list)): # draw rocket pygame.draw.circle(screen, white, rocket_list[i], 2) # move rocket down 1 pixel rocket_list[i][1] += 10 # if rocket has moved off bottom of screen if rocket_list[i][1] > 700: # reset above top y = random.randrange(-50, -10) rocket_list[i][1] = y # give new x position x = random.randrange(0, 1000) rocket_list[i][0] = x score+=1 # go ahead , update screen we've drawn. pygame.display.flip() clock.tick(40) pygame.quit ()
as error message suggests, trying access attribute rect
of rocket
. looking @ rocket
class, rocket
objects indeed have no such attribute.
Comments
Post a Comment