这个函数的第一个参数就是单个精灵,第二个参数是精灵组,第三个参数是一个bool值,最后这个参数起了很大的作用。当为True的时候,会删除组中所有冲突的精灵,False的时候不会删除冲突的精灵。因此我们这里将第三个参数设置为True,这样就会删除掉和精灵冲突的对象了,看起来就好像是玩家吃掉了这些果实一样。
e in group_fruit: e.X -=5 collide_list = pygame.sprite.spritecollide(player,group_fruit,False) score +=len(collide_list)
最后还是看一下全部的代码:
sys, time, random, math, pygame,locale 3 from pygame.locals import * 4 from MyLibrary import * reset_arrow(): 8 y = random.randint(270,350) 9 arrow.position = 800,y 10 bullent_sound.play_sound() MyMap(pygame.sprite.Sprite): (self,x,y): 16 self.x = x 17 self.y = y ).convert_alpha() 19 def map_rolling(self): 20 if self.x < -600: 21 self.x = 600 22 else: 23 self.x -=5 24 def map_update(self): 25 screen.blit(self.bg, (self.x,self.y)) 26 def set_pos(x,y): 27 self.x =x 28 self.y =y Button(object): (self, upimage, downimage,position): 32 self.imageUp = pygame.image.load(upimage).convert_alpha() 33 self.imageDown = pygame.image.load(downimage).convert_alpha() 34 self.position = position 35 self.game_start = False isOver(self): 38 point_x,point_y = pygame.mouse.get_pos() 39 x, y = self. position 40 w, h = self.imageUp.get_size() 41 42 in_x = x - w/2 < point_x < x + w/2 43 in_y = y - h/2 < point_y < y + h/2 44 return in_x and in_y render(self): 47 w, h = self.imageUp.get_size() 48 x, y = self.position self.isOver(): 51 screen.blit(self.imageDown, (x-w/2,y-h/2)) 52 else: 53 screen.blit(self.imageUp, (x-w/2, y-h/2)) 54 def is_start(self): 55 if self.isOver(): 56 b1,b2,b3 = pygame.mouse.get_pressed() 57 if b1 == 1: 58 self.game_start = True 59 bg_sound.play_pause() 60 btn_sound.play_sound() 61 bg_sound.play_sound() replay_music(): 64 bg_sound.play_pause() 65 bg_sound.play_sound() data_read(): ,) 70 best_score = fd_1.read() 71 fd_1.close() 72 return best_score audio_init(): 77 global hit_au,btn_au,bg_au,bullent_au 78 pygame.mixer.init() ) ) ) ) 83 class Music(): (self,sound): 85 self.channel = None 86 self.sound = sound 87 def play_sound(self): 88 self.channel = pygame.mixer.find_channel(True) 89 self.channel.set_volume(0.5) 90 self.channel.play(self.sound) 91 def play_pause(self): 92 self.channel.set_volume(0.0) 93 self.channel.play(self.sound) pygame.init() 97 audio_init() 98 screen = pygame.display.set_mode((800,600),0,32) ) 100 font = pygame.font.Font(None, 22) 101 font1 = pygame.font.Font(None, 40) 102 framerate = pygame.time.Clock() downImageFilename = button = Button(upImageFilename,downImageFilename, (400,500)) ) bg1 = MyMap(0,0) 111 bg2 = MyMap(600,0) group = pygame.sprite.Group() 114 group_exp = pygame.sprite.Group() 115 group_fruit = pygame.sprite.Group() dragon = MySprite() , 260, 150, 3) 119 dragon.position = 100, 230 120 group.add(dragon) explosion = MySprite() ,128,128,6) player = MySprite() , 100, 100, 4) 128 player.position = 400, 270 129 group.add(player) arrow = MySprite() , 40, 16, 1) 134 arrow.position = 800,320 135 group.add(arrow) arrow_vel = 10.0 141 game_over = False 142 you_win = False 143 player_jumping = False 144 jump_vel = 0.0 145 player_start_y = player.Y 146 player_hit = False 147 monster_hit = False 148 p_first = True 149 m_first = True 150 best_score = 0 151 global bg_sound,hit_sound,btn_sound,bullent_sound 152 bg_sound=Music(bg_au) 153 hit_sound=Music(hit_au) 154 btn_sound=Music(btn_au) 155 bullent_sound =Music(bullent_au) ,2:,3:,4:,5:} 157 game_pause = True 158 index =0 159 current_time = 0 160 start_time = 0 161 music_time = 0 162 score =0 163 replay_flag = True bg_sound.play_sound() 166 best_score = data_read() 167 while True: 168 framerate.tick(60) 169 ticks = pygame.time.get_ticks() 170 for event in pygame.event.get(): 171 if event.type == pygame.QUIT: 172 pygame.quit() 173 sys.exit() 174 keys = pygame.key.get_pressed() 175 if keys[K_ESCAPE]: 176 pygame.quit() 177 sys.exit() keys[K_SPACE]: player_jumping: 181 player_jumping = True 182 jump_vel = -12.0 183 184 screen.blit(interface,(0,0)) 185 button.render() 186 button.is_start() 187 if button.game_start == True: 188 if game_pause : 189 index +=1 190 tmp_x =0 191 if score >int (best_score): 192 best_score = score ,) 194 fd_2.write(str(best_score)) 195 fd_2.close() index == 6: 198 you_win = True 199 if you_win: 200 start_time = time.clock() 201 current_time =time.clock()-start_time 202 while current_time<5: 203 screen.fill((200, 200, 200)) ,(240,20,20)) 205 current_time =time.clock()-start_time ,(120,224,22)) 207 print_text(font1, 370, 290, str(best_score),(255,0,0)) ,(120,224,22)) 209 print_text(font1, 385, 380, str(score),(255,0,0)) 210 pygame.display.update() 211 pygame.quit() 212 sys.exit() i in range(0,100): 215 element = MySprite() , 75, 20, 1) 217 tmp_x +=random.randint(50,120) 218 element.X = tmp_x+300 219 element.Y = random.randint(80,200) 220 group_fruit.add(element) 221 start_time = time.clock() 222 current_time =time.clock()-start_time 223 while current_time<3: 224 screen.fill((200, 200, 200)) 225 print_text(font1, 320, 250,game_round[index],(240,20,20)) 226 pygame.display.update() 227 game_pause = False 228 current_time =time.clock()-start_time : game_over: 233 arrow.X -= arrow_vel 234 if arrow.X < -40: reset_arrow() pygame.sprite.collide_rect(arrow, player): 237 reset_arrow() 238 explosion.position =player.X,player.Y 239 player_hit = True 240 hit_sound.play_sound() 241 if p_first: 242 group_exp.add(explosion) 243 p_first = False 244 player.X -= 10 pygame.sprite.collide_rect(arrow, dragon): 248 reset_arrow() 249 explosion.position =dragon.X+50,dragon.Y+50 250 monster_hit = True 251 hit_sound.play_sound() 252 if m_first: 253 group_exp.add(explosion) 254 m_first = False 255 dragon.X -= 10 pygame.sprite.collide_rect(player, dragon): 259 game_over = True e in group_fruit: 262 e.X -=5 263 collide_list = pygame.sprite.spritecollide(player,group_fruit,False) 264 score +=len(collide_list) dragon.X < -100: 267 game_pause = True 268 reset_arrow() 269 player.X = 400 270 dragon.X = 100 player_jumping: 276 if jump_vel <0: 277 jump_vel += 0.6 278 elif jump_vel >= 0: 279 jump_vel += 0.8 280 player.Y += jump_vel 281 if player.Y > player_start_y: 282 player_jumping = False 283 player.Y = player_start_y 284 jump_vel = 0.0 bg1.map_update() 289 bg2.map_update() 290 bg1.map_rolling() 291 bg2.map_rolling() game_over: 295 group.update(ticks, 60) 296 group_exp.update(ticks,60) 297 group_fruit.update(ticks,60) music_time = time.clock() 300 if music_time > 150 and replay_flag: 301 replay_music() 302 replay_flag =False group.draw(screen) 305 group_fruit.draw(screen) 306 if player_hit or monster_hit: 307 group_exp.draw(screen) ) ,(219,224,22)) 310 print_text(font1, 380, 10, str(score),(255,0,0)) 311 if game_over: 312 start_time = time.clock() 313 current_time =time.clock()-start_time 314 while current_time<5: 315 screen.fill((200, 200, 200)) ,(240,20,20)) 317 current_time =time.clock()-start_time ,(120,224,22)) 319 if score >int (best_score): 320 best_score = score 321 print_text(font1, 370, 290, str(best_score),(255,0,0)) ,(120,224,22)) 323 print_text(font1, 370, 380, str(score),(255,0,0)) 324 pygame.display.update() ,) 326 fd_2.write(str(best_score)) 327 fd_2.close() 328 pygame.quit() 329 sys.exit() 330 pygame.display.update()