• 售前

  • 售后

热门帖子
入门百科

Python3+Pygame实现射击游戏完备代码

[复制链接]
方的豆豆 显示全部楼层 发表于 2021-10-26 12:13:50 |阅读模式 打印 上一主题 下一主题
目录


  • 一、游戏特点
  • 二、运行效果展示
  • 三、完整代码
  • 四、运行方式
之前看到过很多人写的飞机大战,当然了之前我也写过多个版本,总体来说功能是实现了,但总感觉不敷“炫”
本日浏览Python资料的时候,意外发现了这个很好的“射击”类游戏,看上去类似飞机大战,但更好玩

一、游戏特点

1. 运行非常流畅
2. 默认有3条命,每条命的HP可以增长(吃补品)也可以淘汰(被击中)
3. 有碰撞时的音效
4. 有碰撞时的爆炸效果

二、运行效果展示



三、完整代码
  1. from __future__ import division
  2. import pygame
  3. import random
  4. from os import path
  5. ## assets folder
  6. img_dir = path.join(path.dirname(__file__), 'assets')
  7. sound_folder = path.join(path.dirname(__file__), 'sounds')
  8. ###############################
  9. ## to be placed in "constant.py" later
  10. WIDTH = 480
  11. HEIGHT = 600
  12. FPS = 60
  13. POWERUP_TIME = 5000
  14. BAR_LENGTH = 100
  15. BAR_HEIGHT = 10
  16. # Define Colors
  17. WHITE = (255, 255, 255)
  18. BLACK = (0, 0, 0)
  19. RED = (255, 0, 0)
  20. GREEN = (0, 255, 0)
  21. BLUE = (0, 0, 255)
  22. YELLOW = (255, 255, 0)
  23. ###############################
  24. ###############################
  25. ## to placed in "__init__.py" later
  26. ## initialize pygame and create window
  27. pygame.init()
  28. pygame.mixer.init() ## For sound
  29. screen = pygame.display.set_mode((WIDTH, HEIGHT))
  30. pygame.display.set_caption("Space Shooter")
  31. clock = pygame.time.Clock()   ## For syncing the FPS
  32. ###############################
  33. font_name = pygame.font.match_font('arial')
  34. def main_menu():
  35.   global screen
  36.   menu_song = pygame.mixer.music.load(path.join(sound_folder, "menu.ogg"))
  37.   pygame.mixer.music.play(-1)
  38.   title = pygame.image.load(path.join(img_dir, "main.png")).convert()
  39.   title = pygame.transform.scale(title, (WIDTH, HEIGHT), screen)
  40.   screen.blit(title, (0,0))
  41.   pygame.display.update()
  42.   while True:
  43.     ev = pygame.event.poll()
  44.     if ev.type == pygame.KEYDOWN:
  45.       if ev.key == pygame.K_RETURN:
  46.         break
  47.       elif ev.key == pygame.K_q:
  48.         pygame.quit()
  49.         quit()
  50.     else:
  51.       draw_text(screen, "Press [ENTER] To Begin", 30, WIDTH/2, HEIGHT/2)
  52.       draw_text(screen, "or [Q] To Quit", 30, WIDTH/2, (HEIGHT/2)+40)
  53.       pygame.display.update()
  54.   #pygame.mixer.music.stop()
  55.   ready = pygame.mixer.Sound(path.join(sound_folder,'getready.ogg'))
  56.   ready.play()
  57.   screen.fill(BLACK)
  58.   draw_text(screen, "GET READY!", 40, WIDTH/2, HEIGHT/2)
  59.   pygame.display.update()
  60. def draw_text(surf, text, size, x, y):
  61.   ## selecting a cross platform font to display the score
  62.   font = pygame.font.Font(font_name, size)
  63.   text_surface = font.render(text, True, WHITE)    ## True denotes the font to be anti-aliased
  64.   text_rect = text_surface.get_rect()
  65.   text_rect.midtop = (x, y)
  66.   surf.blit(text_surface, text_rect)
  67. def draw_shield_bar(surf, x, y, pct):
  68.   # if pct < 0:
  69.   #   pct = 0
  70.   pct = max(pct, 0)
  71.   ## moving them to top
  72.   # BAR_LENGTH = 100
  73.   # BAR_HEIGHT = 10
  74.   fill = (pct / 100) * BAR_LENGTH
  75.   outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)
  76.   fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)
  77.   pygame.draw.rect(surf, GREEN, fill_rect)
  78.   pygame.draw.rect(surf, WHITE, outline_rect, 2)
  79. def draw_lives(surf, x, y, lives, img):
  80.   for i in range(lives):
  81.     img_rect= img.get_rect()
  82.     img_rect.x = x + 30 * i
  83.     img_rect.y = y
  84.     surf.blit(img, img_rect)
  85. def newmob():
  86.   mob_element = Mob()
  87.   all_sprites.add(mob_element)
  88.   mobs.add(mob_element)
  89. class Explosion(pygame.sprite.Sprite):
  90.   def __init__(self, center, size):
  91.     pygame.sprite.Sprite.__init__(self)
  92.     self.size = size
  93.     self.image = explosion_anim[self.size][0]
  94.     self.rect = self.image.get_rect()
  95.     self.rect.center = center
  96.     self.frame = 0
  97.     self.last_update = pygame.time.get_ticks()
  98.     self.frame_rate = 75
  99.   def update(self):
  100.     now = pygame.time.get_ticks()
  101.     if now - self.last_update > self.frame_rate:
  102.       self.last_update = now
  103.       self.frame += 1
  104.       if self.frame == len(explosion_anim[self.size]):
  105.         self.kill()
  106.       else:
  107.         center = self.rect.center
  108.         self.image = explosion_anim[self.size][self.frame]
  109.         self.rect = self.image.get_rect()
  110.         self.rect.center = center
  111. class Player(pygame.sprite.Sprite):
  112.   def __init__(self):
  113.     pygame.sprite.Sprite.__init__(self)
  114.     ## scale the player img down
  115.     self.image = pygame.transform.scale(player_img, (50, 38))
  116.     self.image.set_colorkey(BLACK)
  117.     self.rect = self.image.get_rect()
  118.     self.radius = 20
  119.     self.rect.centerx = WIDTH / 2
  120.     self.rect.bottom = HEIGHT - 10
  121.     self.speedx = 0
  122.     self.shield = 100
  123.     self.shoot_delay = 250
  124.     self.last_shot = pygame.time.get_ticks()
  125.     self.lives = 3
  126.     self.hidden = False
  127.     self.hide_timer = pygame.time.get_ticks()
  128.     self.power = 1
  129.     self.power_timer = pygame.time.get_ticks()
  130.   def update(self):
  131.     ## time out for powerups
  132.     if self.power >=2 and pygame.time.get_ticks() - self.power_time > POWERUP_TIME:
  133.       self.power -= 1
  134.       self.power_time = pygame.time.get_ticks()
  135.     ## unhide
  136.     if self.hidden and pygame.time.get_ticks() - self.hide_timer > 1000:
  137.       self.hidden = False
  138.       self.rect.centerx = WIDTH / 2
  139.       self.rect.bottom = HEIGHT - 30
  140.     self.speedx = 0   ## makes the player static in the screen by default.
  141.     # then we have to check whether there is an event hanlding being done for the arrow keys being
  142.     ## pressed
  143.     ## will give back a list of the keys which happen to be pressed down at that moment
  144.     keystate = pygame.key.get_pressed()
  145.     if keystate[pygame.K_LEFT]:
  146.       self.speedx = -5
  147.     elif keystate[pygame.K_RIGHT]:
  148.       self.speedx = 5
  149.     #Fire weapons by holding spacebar
  150.     if keystate[pygame.K_SPACE]:
  151.       self.shoot()
  152.     ## check for the borders at the left and right
  153.     if self.rect.right > WIDTH:
  154.       self.rect.right = WIDTH
  155.     if self.rect.left < 0:
  156.       self.rect.left = 0
  157.     self.rect.x += self.speedx
  158.   def shoot(self):
  159.     ## to tell the bullet where to spawn
  160.     now = pygame.time.get_ticks()
  161.     if now - self.last_shot > self.shoot_delay:
  162.       self.last_shot = now
  163.       if self.power == 1:
  164.         bullet = Bullet(self.rect.centerx, self.rect.top)
  165.         all_sprites.add(bullet)
  166.         bullets.add(bullet)
  167.         shooting_sound.play()
  168.       if self.power == 2:
  169.         bullet1 = Bullet(self.rect.left, self.rect.centery)
  170.         bullet2 = Bullet(self.rect.right, self.rect.centery)
  171.         all_sprites.add(bullet1)
  172.         all_sprites.add(bullet2)
  173.         bullets.add(bullet1)
  174.         bullets.add(bullet2)
  175.         shooting_sound.play()
  176.       """ MOAR POWAH """
  177.       if self.power >= 3:
  178.         bullet1 = Bullet(self.rect.left, self.rect.centery)
  179.         bullet2 = Bullet(self.rect.right, self.rect.centery)
  180.         missile1 = Missile(self.rect.centerx, self.rect.top) # Missile shoots from center of ship
  181.         all_sprites.add(bullet1)
  182.         all_sprites.add(bullet2)
  183.         all_sprites.add(missile1)
  184.         bullets.add(bullet1)
  185.         bullets.add(bullet2)
  186.         bullets.add(missile1)
  187.         shooting_sound.play()
  188.         missile_sound.play()
  189.   def powerup(self):
  190.     self.power += 1
  191.     self.power_time = pygame.time.get_ticks()
  192.   def hide(self):
  193.     self.hidden = True
  194.     self.hide_timer = pygame.time.get_ticks()
  195.     self.rect.center = (WIDTH / 2, HEIGHT + 200)
  196. # defines the enemies
  197. class Mob(pygame.sprite.Sprite):
  198.   def __init__(self):
  199.     pygame.sprite.Sprite.__init__(self)
  200.     self.image_orig = random.choice(meteor_images)
  201.     self.image_orig.set_colorkey(BLACK)
  202.     self.image = self.image_orig.copy()
  203.     self.rect = self.image.get_rect()
  204.     self.radius = int(self.rect.width *.90 / 2)
  205.     self.rect.x = random.randrange(0, WIDTH - self.rect.width)
  206.     self.rect.y = random.randrange(-150, -100)
  207.     self.speedy = random.randrange(5, 20)    ## for randomizing the speed of the Mob
  208.     ## randomize the movements a little more
  209.     self.speedx = random.randrange(-3, 3)
  210.     ## adding rotation to the mob element
  211.     self.rotation = 0
  212.     self.rotation_speed = random.randrange(-8, 8)
  213.     self.last_update = pygame.time.get_ticks() ## time when the rotation has to happen
  214.   def rotate(self):
  215.     time_now = pygame.time.get_ticks()
  216.     if time_now - self.last_update > 50: # in milliseconds
  217.       self.last_update = time_now
  218.       self.rotation = (self.rotation + self.rotation_speed) % 360
  219.       new_image = pygame.transform.rotate(self.image_orig, self.rotation)
  220.       old_center = self.rect.center
  221.       self.image = new_image
  222.       self.rect = self.image.get_rect()
  223.       self.rect.center = old_center
  224.   def update(self):
  225.     self.rotate()
  226.     self.rect.x += self.speedx
  227.     self.rect.y += self.speedy
  228.     ## now what if the mob element goes out of the screen
  229.     if (self.rect.top > HEIGHT + 10) or (self.rect.left < -25) or (self.rect.right > WIDTH + 20):
  230.       self.rect.x = random.randrange(0, WIDTH - self.rect.width)
  231.       self.rect.y = random.randrange(-100, -40)
  232.       self.speedy = random.randrange(1, 8)    ## for randomizing the speed of the Mob
  233. ## defines the sprite for Powerups
  234. class Pow(pygame.sprite.Sprite):
  235.   def __init__(self, center):
  236.     pygame.sprite.Sprite.__init__(self)
  237.     self.type = random.choice(['shield', 'gun'])
  238.     self.image = powerup_images[self.type]
  239.     self.image.set_colorkey(BLACK)
  240.     self.rect = self.image.get_rect()
  241.     ## place the bullet according to the current position of the player
  242.     self.rect.center = center
  243.     self.speedy = 2
  244.   def update(self):
  245.     """should spawn right in front of the player"""
  246.     self.rect.y += self.speedy
  247.     ## kill the sprite after it moves over the top border
  248.     if self.rect.top > HEIGHT:
  249.       self.kill()
  250. ## defines the sprite for bullets
  251. class Bullet(pygame.sprite.Sprite):
  252.   def __init__(self, x, y):
  253.     pygame.sprite.Sprite.__init__(self)
  254.     self.image = bullet_img
  255.     self.image.set_colorkey(BLACK)
  256.     self.rect = self.image.get_rect()
  257.     ## place the bullet according to the current position of the player
  258.     self.rect.bottom = y
  259.     self.rect.centerx = x
  260.     self.speedy = -10
  261.   def update(self):
  262.     """should spawn right in front of the player"""
  263.     self.rect.y += self.speedy
  264.     ## kill the sprite after it moves over the top border
  265.     if self.rect.bottom < 0:
  266.       self.kill()
  267.     ## now we need a way to shoot
  268.     ## lets bind it to "spacebar".
  269.     ## adding an event for it in Game loop
  270. ## FIRE ZE MISSILES
  271. class Missile(pygame.sprite.Sprite):
  272.   def __init__(self, x, y):
  273.     pygame.sprite.Sprite.__init__(self)
  274.     self.image = missile_img
  275.     self.image.set_colorkey(BLACK)
  276.     self.rect = self.image.get_rect()
  277.     self.rect.bottom = y
  278.     self.rect.centerx = x
  279.     self.speedy = -10
  280.   def update(self):
  281.     """should spawn right in front of the player"""
  282.     self.rect.y += self.speedy
  283.     if self.rect.bottom < 0:
  284.       self.kill()
  285. ###################################################
  286. ## Load all game images
  287. background = pygame.image.load(path.join(img_dir, 'starfield.png')).convert()
  288. background_rect = background.get_rect()
  289. ## ^^ draw this rect first
  290. player_img = pygame.image.load(path.join(img_dir, 'playerShip1_orange.png')).convert()
  291. player_mini_img = pygame.transform.scale(player_img, (25, 19))
  292. player_mini_img.set_colorkey(BLACK)
  293. bullet_img = pygame.image.load(path.join(img_dir, 'laserRed16.png')).convert()
  294. missile_img = pygame.image.load(path.join(img_dir, 'missile.png')).convert_alpha()
  295. # meteor_img = pygame.image.load(path.join(img_dir, 'meteorBrown_med1.png')).convert()
  296. meteor_images = []
  297. meteor_list = [
  298.   'meteorBrown_big1.png',
  299.   'meteorBrown_big2.png',
  300.   'meteorBrown_med1.png',
  301.   'meteorBrown_med3.png',
  302.   'meteorBrown_small1.png',
  303.   'meteorBrown_small2.png',
  304.   'meteorBrown_tiny1.png'
  305. ]
  306. for image in meteor_list:
  307.   meteor_images.append(pygame.image.load(path.join(img_dir, image)).convert())
  308. ## meteor explosion
  309. explosion_anim = {}
  310. explosion_anim['lg'] = []
  311. explosion_anim['sm'] = []
  312. explosion_anim['player'] = []
  313. for i in range(9):
  314.   filename = 'regularExplosion0{}.png'.format(i)
  315.   img = pygame.image.load(path.join(img_dir, filename)).convert()
  316.   img.set_colorkey(BLACK)
  317.   ## resize the explosion
  318.   img_lg = pygame.transform.scale(img, (75, 75))
  319.   explosion_anim['lg'].append(img_lg)
  320.   img_sm = pygame.transform.scale(img, (32, 32))
  321.   explosion_anim['sm'].append(img_sm)
  322.   ## player explosion
  323.   filename = 'sonicExplosion0{}.png'.format(i)
  324.   img = pygame.image.load(path.join(img_dir, filename)).convert()
  325.   img.set_colorkey(BLACK)
  326.   explosion_anim['player'].append(img)
  327. ## load power ups
  328. powerup_images = {}
  329. powerup_images['shield'] = pygame.image.load(path.join(img_dir, 'shield_gold.png')).convert()
  330. powerup_images['gun'] = pygame.image.load(path.join(img_dir, 'bolt_gold.png')).convert()
  331. ###################################################
  332. ###################################################
  333. ### Load all game sounds
  334. shooting_sound = pygame.mixer.Sound(path.join(sound_folder, 'pew.wav'))
  335. missile_sound = pygame.mixer.Sound(path.join(sound_folder, 'rocket.ogg'))
  336. expl_sounds = []
  337. for sound in ['expl3.wav', 'expl6.wav']:
  338.   expl_sounds.append(pygame.mixer.Sound(path.join(sound_folder, sound)))
  339. ## main background music
  340. #pygame.mixer.music.load(path.join(sound_folder, 'tgfcoder-FrozenJam-SeamlessLoop.ogg'))
  341. pygame.mixer.music.set_volume(0.2)   ## simmered the sound down a little
  342. player_die_sound = pygame.mixer.Sound(path.join(sound_folder, 'rumble1.ogg'))
  343. ###################################################
  344. ## group all the sprites together for ease of update
  345. all_sprites = pygame.sprite.Group()
  346. player = Player()
  347. all_sprites.add(player)
  348. ## spawn a group of mob
  349. mobs = pygame.sprite.Group()
  350. for i in range(8):   ## 8 mobs
  351.   # mob_element = Mob()
  352.   # all_sprites.add(mob_element)
  353.   # mobs.add(mob_element)
  354.   newmob()
  355. ## group for bullets
  356. bullets = pygame.sprite.Group()
  357. powerups = pygame.sprite.Group()
  358. #### Score board variable
  359. score = 0
  360. ## TODO: make the game music loop over again and again. play(loops=-1) is not working
  361. # Error :
  362. # TypeError: play() takes no keyword arguments
  363. #pygame.mixer.music.play()
  364. #############################
  365. ## Game loop
  366. running = True
  367. menu_display = True
  368. while running:
  369.   if menu_display:
  370.     main_menu()
  371.     pygame.time.wait(3000)
  372.     #Stop menu music
  373.     pygame.mixer.music.stop()
  374.     #Play the gameplay music
  375.     pygame.mixer.music.load(path.join(sound_folder, 'tgfcoder-FrozenJam-SeamlessLoop.ogg'))
  376.     pygame.mixer.music.play(-1)   ## makes the gameplay sound in an endless loop
  377.     menu_display = False
  378.   #1 Process input/events
  379.   clock.tick(FPS)   ## will make the loop run at the same speed all the time
  380.   for event in pygame.event.get():    # gets all the events which have occured till now and keeps tab of them.
  381.     ## listening for the the X button at the top
  382.     if event.type == pygame.QUIT:
  383.       running = False
  384.     ## Press ESC to exit game
  385.     if event.type == pygame.KEYDOWN:
  386.       if event.key == pygame.K_ESCAPE:
  387.         running = False
  388.     # ## event for shooting the bullets
  389.     # elif event.type == pygame.KEYDOWN:
  390.     #   if event.key == pygame.K_SPACE:
  391.     #     player.shoot()   ## we have to define the shoot() function
  392.   #2 Update
  393.   all_sprites.update()
  394.   ## check if a bullet hit a mob
  395.   ## now we have a group of bullets and a group of mob
  396.   hits = pygame.sprite.groupcollide(mobs, bullets, True, True)
  397.   ## now as we delete the mob element when we hit one with a bullet, we need to respawn them again
  398.   ## as there will be no mob_elements left out
  399.   for hit in hits:
  400.     score += 50 - hit.radius     ## give different scores for hitting big and small metoers
  401.     random.choice(expl_sounds).play()
  402.     # m = Mob()
  403.     # all_sprites.add(m)
  404.     # mobs.add(m)
  405.     expl = Explosion(hit.rect.center, 'lg')
  406.     all_sprites.add(expl)
  407.     if random.random() > 0.9:
  408.       pow = Pow(hit.rect.center)
  409.       all_sprites.add(pow)
  410.       powerups.add(pow)
  411.     newmob()    ## spawn a new mob
  412.   ## ^^ the above loop will create the amount of mob objects which were killed spawn again
  413.   #########################
  414.   ## check if the player collides with the mob
  415.   hits = pygame.sprite.spritecollide(player, mobs, True, pygame.sprite.collide_circle)    ## gives back a list, True makes the mob element disappear
  416.   for hit in hits:
  417.     player.shield -= hit.radius * 2
  418.     expl = Explosion(hit.rect.center, 'sm')
  419.     all_sprites.add(expl)
  420.     newmob()
  421.     if player.shield <= 0:
  422.       player_die_sound.play()
  423.       death_explosion = Explosion(player.rect.center, 'player')
  424.       all_sprites.add(death_explosion)
  425.       # running = False   ## GAME OVER 3:D
  426.       player.hide()
  427.       player.lives -= 1
  428.       player.shield = 100
  429.   ## if the player hit a power up
  430.   hits = pygame.sprite.spritecollide(player, powerups, True)
  431.   for hit in hits:
  432.     if hit.type == 'shield':
  433.       player.shield += random.randrange(10, 30)
  434.       if player.shield >= 100:
  435.         player.shield = 100
  436.     if hit.type == 'gun':
  437.       player.powerup()
  438.   ## if player died and the explosion has finished, end game
  439.   if player.lives == 0 and not death_explosion.alive():
  440.     running = False
  441.     # menu_display = True
  442.     # pygame.display.update()
  443.   #3 Draw/render
  444.   screen.fill(BLACK)
  445.   ## draw the stargaze.png image
  446.   screen.blit(background, background_rect)
  447.   all_sprites.draw(screen)
  448.   draw_text(screen, str(score), 18, WIDTH / 2, 10)   ## 10px down from the screen
  449.   draw_shield_bar(screen, 5, 5, player.shield)
  450.   # Draw lives
  451.   draw_lives(screen, WIDTH - 100, 5, player.lives, player_mini_img)
  452.   ## Done after drawing everything to the screen
  453.   pygame.display.flip()
  454. pygame.quit()
复制代码
四、运行方式

假如想要运行本程序,流程如下
1. 下载上述代码 比方存储为xxxx.py
2. 下载素材(图片、声音等)https://www.itprojects.cn/197.html
3. 切换到安装有pygame模块的python捏造环境(假如没有pygame可以pip install pygame安装)
4. 使用命令运行 python3 xxxx.py
到此这篇关于Python3+Pygame实现射击游戏完整代码的文章就介绍到这了,更多相关Python3+Pygame实现射击游戏内容请搜索脚本之家从前的文章或继承浏览下面的相关文章希望大家以后多多支持脚本之家!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x

帖子地址: 

回复

使用道具 举报

分享
推广
火星云矿 | 预约S19Pro,享500抵1000!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

草根技术分享(草根吧)是全球知名中文IT技术交流平台,创建于2021年,包含原创博客、精品问答、职业培训、技术社区、资源下载等产品服务,提供原创、优质、完整内容的专业IT技术开发社区。
  • 官方手机版

  • 微信公众号

  • 商务合作