alien_invasion.rar

  • chimerican
    了解作者
  • Python
    开发工具
  • 18KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • 1 积分
    下载积分
  • 1
    下载次数
  • 2020-11-19 09:21
    上传日期
python入门到实践这本书的大战外星人游戏编程。
alien_invasion.rar
  • alien_invasion
  • __pycache__
  • game_stats.cpython-37.pyc
    693B
  • ship.cpython-37.pyc
    1.1KB
  • settings.cpython-37.pyc
    1.3KB
  • button.cpython-37.pyc
    1.2KB
  • alien.cpython-37.pyc
    1.2KB
  • scoreboard.cpython-37.pyc
    2.3KB
  • bullet.cpython-37.pyc
    1.1KB
  • game_functions.cpython-37.pyc
    4.2KB
  • images
  • ship1.bmp
    5.8KB
  • alien.bmp
    9.8KB
  • ship.bmp
    5.8KB
  • button.py
    1000B
  • alien.py
    769B
  • alien_invasion.py
    1.5KB
  • ship.py
    1.8KB
  • settings.py
    839B
  • scoreboard.py
    2KB
  • game_stats.py
    362B
  • game_functions.py
    5.3KB
  • bullet.py
    733B
内容介绍
import sys import pygame from time import sleep from settings import Settings from bullet import Bullet from alien import Alien def check_events(ai_settings,screen,ship,bullets,aliens,stats,play_button,sb): key_pressed = pygame.key.get_pressed() if key_pressed[pygame.K_RIGHT] and ship.rect.right < ship.screen_rect.right: ship.center += ship.ai_settings.ship_speed_factor if key_pressed[pygame.K_LEFT] and ship.rect.left > 0: ship.center -= ship.ai_settings.ship_speed_factor # if key_pressed[pygame.K_SPACE]: # new_bullet = Bullet(ai_settings,screen,ship) # bullets.add(new_bullet) # print("d____________") for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() elif event.type == pygame.MOUSEBUTTONDOWN: mouse_x,mouse_y = pygame.mouse.get_pos() check_play_button(ai_settings,screen,ship,bullets,aliens,stats,play_button, mouse_x,mouse_y,sb) elif event.type == pygame.KEYDOWN and event.key == pygame.K_SPACE: if len(bullets) < ai_settings.bullets_allowed and stats.game_active: new_bullet = Bullet(ai_settings,screen,ship) bullets.add(new_bullet) # print("d_____________") elif event.type == pygame.KEYDOWN and event.key== pygame.K_q: sys.exit() def create_fleet(ai_settings,screen,ship,aliens): alien = Alien(ai_settings,screen) alien_width = alien.rect.width alien_height = alien.rect.height available_space_x = ai_settings.screen_width - 2*alien_width number_aliens_x = int(available_space_x/(2*alien_width)) available_space_y = (ai_settings.screen_height-(3 * alien_height) - ship.rect.height) number_rows = int(available_space_y/(2 * alien_height)) for row_number in range(number_rows): for alien_number in range(number_aliens_x): alien = Alien(ai_settings,screen) alien.x = alien_width + 2 * alien_width * alien_number alien.rect.x = alien.x alien.rect.y = alien_height + 2 * alien_height * row_number aliens.add(alien) def check_fleet_edges(ai_settings,aliens): for alien in aliens.sprites(): #print('________ddd_______') if alien.check_edges(): #print('______dir_______') ai_settings.fleet_direction *= -1 for alien in aliens.sprites(): alien.rect.y += ai_settings.fleet_drop_speed #print('_______aaa_______') break #print('_____fff_____') def ship_hit(ai_settings,screen,ship,bullets,aliens,stats,lose_ship,sb): if stats.ships_left > 0: stats.ships_left -= 1 sb.prep_ships() aliens.empty() bullets.empty() create_fleet(ai_settings,screen,ship,aliens) ship.center_ship() # lose_ship.draw_button() # pygame.display.flip() sleep(1) else: # print('you loser') stats.game_active = False pygame.mouse.set_visible(True) def check_aliens_bottom(ai_settings,screen,ship,bullets,aliens,stats,lose_ship,sb): screen_rect = screen.get_rect() for alien in aliens.sprites(): if alien.rect.bottom >= screen_rect.bottom: ship_hit(ai_settings,screen,ship,aliens,bullets,stats,lose_ship,sb) break def update_bullets(ai_settings,screen,ship,bullets,aliens,stats,sb): bullets.update() for bullet in bullets.copy(): if bullet.rect.bottom <= 0: bullets.remove(bullet) collisions = pygame.sprite.groupcollide(bullets,aliens,True,True) if collisions: for aliens in collisions.values(): stats.score += ai_settings.alien_points*len(aliens) sb.prep_score() #update score check_high_score(stats,sb) if len(aliens)== 0: #all aliens are killed bullets.empty() ai_settings.increase_speed() #increase game difficult stats.level += 1 sb.prep_level() create_fleet(ai_settings,screen,ship,aliens) sleep(1) def update_aliens(ai_settings,screen,ship,bullets,aliens,stats): check_fleet_edges(ai_settings,aliens) #change the direction of aliens fleet aliens.update() def check_play_button(ai_settings,screen,ship,bullets,aliens,stats,play_button, mouse_x,mouse_y,sb): button_clicked = play_button.rect.collidepoint(mouse_x,mouse_y) if button_clicked and not stats.game_active: ai_settings.initialize_dynamic_settings() stats.reset_stats() #initialize ship's number,score,level sb.prep_score() #update score=0 sb.prep_level() #update level=1 sb.prep_ships() stats.game_active = True pygame.mouse.set_visible(False) #mouse invisible aliens.empty() bullets.empty() create_fleet(ai_settings,screen,ship,aliens) ship.center_ship() def check_high_score(stats,sb): if stats.score > stats.high_score: stats.high_score = stats.score sb.prep_high_score() def update_screen(ai_settings,screen,ship,bullets,aliens,stats, play_button,lose_ship,sb): screen.fill(ai_settings.bg_color) ship.blitme() # print("b_________") for bullet in bullets.sprites(): # print('e___________') bullet.draw_bullet() # print('f___________') aliens.draw(screen) sb.show_score() if not stats.game_active: play_button.draw_button() pygame.display.flip() #print("c_________") if pygame.sprite.spritecollideany(ship,aliens): #aliens hit ship ship_hit(ai_settings,screen,ship,bullets,aliens,stats,lose_ship,sb) check_aliens_bottom(ai_settings,screen,ship,bullets,aliens,stats,lose_ship,sb)
评论
    相关推荐
    • python-banana_game
      这是一个简单的python 的小游戏,适用于学习的初步练习
    • python game demo temp
      python game demo temp python game demo temp python game demo temp
    • python snake game
      symbian S60v3平台python for s60 实现的游戏, greedy snake。要求手机安装了python。 这只是个脚本,还没来得及转换成sis格式,且需要一些改进。
    • snakegame-python-
      蛇游戏-python-
    • python Game Server
      Game Server Developed By Me!No Test No Runing Just Used for Me!
    • snake-game-python
      snake-game-python
    • Python_Game
      Python_Game
    • PythonGame
      PythonGame
    • pythongame教程
      Pygame 是一个利用SDL 库的写就的游戏库,SDL呢,全名 Simple DirectMedia Layer,是一位叫 做 SamLantinga 的大牛写的,据说他为了让 Loki(致力于向 Linux上移植 Windows 的游戏的一家 大好人公司,可惜已经倒闭,...
    • pythongame
      pythongame