Python制作太空射击小游戏!

写在最前面

你想成为Python高手吗?你想使用Python编写一个炫酷的游戏吗?

那么今天这篇文章就能带着你从零开始编写一个Python小游戏。希望你能喜欢。

话不多说,我们先来看一副动图

《Python制作太空射击小游戏!》 零基础用Python开发的第一个小游戏——太空射击

这个图片中就是我们最终的效果。是不是很炫酷?有木有?而且代码全部都是由Python编写的,是不是很神奇?不清楚你们是什么感觉,我一次看到这个项目是就觉得很厉害。

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>需要项目源码与素材的小伙伴关注并转发文章,私信小编“太空射击”即可获取项目的源码
</pre>

项目部分

这种小项目看起来是很炫酷,其实做起来也不难,但是稍微有点麻烦。麻烦就在于找素材

《Python制作太空射击小游戏!》 零基础用Python开发的第一个小游戏——太空射击

上诉就是需要使用到的素材。除了上面需要用到的图片,还有音乐等文件。我就不一一发出来了。素材是次要的,最主要的还是代码。

代码我们只需要用到pygame这个模块,没有安装的小伙伴可以自己安装一下。

安装之后我们就来看一下代码。首先我们需要导入一个用到的库。有了这些库之后我们才能进行使用

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>from future import division
import pygame
import random
from os import path
</pre>

然后要导入当前文件下的素材,没有这些素材页面就会是丑丑的

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”># 导入图片素材
img_dir = path.join(path.dirname(file), ‘assets’)

导入音频文件

sound_folder = path.join(path.dirname(file), ‘sounds’)
</pre>

然后在定义一些默认的参数、颜色

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>## to be placed in “constant.py” later
WIDTH = 480
HEIGHT = 600
FPS = 60
POWERUP_TIME = 5000
BAR_LENGTH = 100
BAR_HEIGHT = 10

默认的颜色

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
</pre>

初始化游戏

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>pygame.init()
pygame.mixer.init() ## For sound
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption(“Space Shooter”)
clock = pygame.time.Clock()
font_name = pygame.font.match_font(‘arial’)
</pre>

定义玩游戏的事件与逻辑

<pre style=”-webkit-tap-highlight-color: transparent; box-sizing: border-box; font-family: Consolas, Menlo, Courier, monospace; font-size: 16px; white-space: pre-wrap; position: relative; line-height: 1.5; color: rgb(153, 153, 153); margin: 1em 0px; padding: 12px 10px; background: rgb(244, 245, 246); border: 1px solid rgb(232, 232, 232); font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;”>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()
elif ev.type == pygame.QUIT:
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()
</pre>

中间的代码有点多,我就不一一展现出来了,想要的话源码可以,进群:700341555。如果你将这个游戏学完了的话,你的能力将会提升一大截。如果你只是一个小白的话,要到源码也可以直接运行。

写在最后

这个项目虽然不大,算上空格与注释,代码量是600多行。虽然代码不多,但是如果你全部掌握的话,能力提升还是杠杠的。

真心希望大家能够在Python的方向上走的更远!!

毕竟现在Python的火热程度及实用性,很快能够在编程语言占据很大的地位!只要坚持学下去,终有一天是能够全部掌握的。

    原文作者:浪里小白龙q
    原文地址: https://www.jianshu.com/p/76c2cc6d25b1
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞