python3.7.4游戏代码,Python3+Pygame实现射击游戏完整代码

之前看到过很多人写的飞机大战,当然了之前我也写过多个版本,总体来说功能是实现了,但总感觉不够“炫”今天浏览Python资料的时候,意外发现了这个很好的“射击”类游戏,看上去类似飞机大战,但更好玩一、游戏特点1. 运行非常流畅2. 默认有3条命,每条命的HP可以增加(吃补品)也可以减少(被击中)3. 有碰撞时的音效4. 有碰撞时的爆炸效果二、运行效果展示三、完整代码from __future__ i...
摘要由CSDN通过智能技术生成

之前看到过很多人写的飞机大战,当然了之前我也写过多个版本,总体来说功能是实现了,但总感觉不够“炫”

今天浏览Python资料的时候,意外发现了这个很好的“射击”类游戏,看上去类似飞机大战,但更好玩

一、游戏特点

1. 运行非常流畅

2. 默认有3条命,每条命的HP可以增加(吃补品)也可以减少(被击中)

3. 有碰撞时的音效

4. 有碰撞时的爆炸效果

二、运行效果展示

c2275b839f4a8f7cfdd96add39360d76.gif

三、完整代码

from __future__ import division

import pygame

import random

from os import path

## assets folder

img_dir = path.join(path.dirname(__file__), 'assets')

sound_folder = path.join(path.dirname(__file__), 'sounds')

###############################

## to be placed in "constant.py" later

WIDTH = 480

HEIGHT = 600

FPS = 60

POWERUP_TIME = 5000

BAR_LENGTH = 100

BAR_HEIGHT = 10

# Define Colors

WHITE = (255, 255, 255)

BLACK = (0, 0, 0)

RED = (255, 0, 0)

GREEN = (0, 255, 0)

BLUE = (0, 0, 255)

YELLOW = (255, 255, 0)

###############################

###############################

## to placed in "__init__.py" later

## initialize pygame and create window

pygame.init()

pygame.mixer.init() ## For sound

screen = pygame.display.set_mode((WIDTH, HEIGHT))

pygame.display.set_caption("Space Shooter")

clock = pygame.time.Clock() ## For syncing the FPS

###############################

font_name = pygame.font.match_font('arial')

def main_menu():

global screen

menu_song = pygame.mixer.music.load(path.join(sound_folder, "menu.ogg"))

pygame.mixer.music.play(-1)

title = pygame.image.load(path.join(img_dir, "main.png")).convert()

title = pygame.transform.scale(title, (WIDTH, HEIGHT), screen)

screen.blit(title, (0,0))

pygame.display.update()

while True:

ev = pygame.event.poll()

if ev.type == pygame.KEYDOWN:

if ev.key == pygame.K_RETURN:

break

elif ev.key == pygame.K_q:

pygame.quit()

quit()

else:

draw_text(screen, "Press [ENTER] To Begin", 30, WIDTH/2, HEIGHT/2)

draw_text(screen, "or [Q] To Quit", 30, WIDTH/2, (HEIGHT/2)+40)

pygame.display.update()

#pygame.mixer.music.stop()

ready = pygame.mixer.Sound(path.join(sound_folder,'getready.ogg'))

ready.play()

screen.fill(BLACK)

draw_text(screen, "GET READY!", 40, WIDTH/2, HEIGHT/2)

pygame.display.update()

def draw_text(surf, text, size, x, y):

## selecting a cross platform font to display the score

font = pygame.font.Font(font_name, size)

text_surface = font.render(text, True, WHITE) ## True denotes the font to be anti-aliased

text_rect = text_surface.get_rect()

text_rect.midtop = (x, y)

surf.blit(text_surface, text_rect)

def draw_shield_bar(surf, x, y, pct):

# if pct < 0:

# pct = 0

pct = max(pct, 0)

## moving them to top

# BAR_LENGTH = 100

# BAR_HEIGHT = 10

fill = (pct / 100) * BAR_LENGTH

outline_rect = pygame.Rect(x, y, BAR_LENGTH, BAR_HEIGHT)

fill_rect = pygame.Rect(x, y, fill, BAR_HEIGHT)

pygame.draw.rect(surf, GREEN, fill_rect)

pygame.draw.rect(surf, WHITE, outline_rect, 2)

def draw_lives(surf, x, y, lives, img):

for i in range(lives):

img_rect= img.get_rect()

img_rect.x = x + 30 * i

img_rect.y = y

surf.blit(img, img_rect)

def newmob():

mob_element = Mob()

all_sprites.add(mob_element)

mobs.add(mob_element)

class Explosion(pygame.sprite.Sprite):

def __init__(self, center, size):

pygame.sprite.Sprite.__init_

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值