当前位置:   article > 正文

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

python射击小游戏代码

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

今天浏览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_

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/105400
推荐阅读
相关标签
  

闽ICP备14008679号