前言:不久前接触了Python的游戏制作的相关第三方库,于是学习了pygame的相关内容,想制作一款基于pygame的小游戏。因为还不太熟悉游戏制作和pygame,部分内容我参考了《Python-从入门到精通》这本书。那么好,话不多说,我们直接进入今天的Python学习之旅-制作基于Pygame的小游戏(天空之战)。
一:准备游戏相关图片
制作pygame小游戏天空之战时,我们需要事先准备好相关的图片(如自己的战机,敌机,子弹等),最好是png或jpg格式。我这里用的是Windows自带的"画图"功能来制作自己的战机,敌机,子弹等图片,大家可以自行制作(需要我的原图片的话私信我发也行)。(如下所示)
敌机图片
战机图片
子弹图片
制作好图片好后我们将图片放在python项目下。(如下圆圈标注所示)
二:完整代码展示
import pygame
import sys
import random
pygame.init()
screen_width = 800
screen_height = 600
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("飞机大战")
clock = pygame.time.Clock()
player_plane_image = pygame.image.load('airplane.png').convert_alpha()
enemy_plane_image = pygame.image.load('enemy.png').convert_alpha()
bullet_image = pygame.image.load('bullet.png').convert_alpha()
BLACK = (0, 0, 0)
class PlayerPlane(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = player_plane_image
self.rect = self.image.get_rect()
self.rect.centerx = screen_width // 2
self.rect.bottom = screen_height - 10
self.speed_x = 0
self.speed_y = 0
self.bullets = pygame.sprite.Group()
def update(self):
self.rect.x += self.speed_x
self.rect.y += self.speed_y
if self.rect.right > screen_width:
self.rect.right = screen_width
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > screen_height:
self.rect.bottom = screen_height
if self.rect.top < 0:
self.rect.top = 0
self.bullets.update()
def shoot(self):
bullet = Bullet(self.rect.centerx, self.rect.top)
self.bullets.add(bullet)
class EnemyPlane(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = enemy_plane_image
self.rect = self.image.get_rect()
self.rect.x = random.randint(0, screen_width - self.rect.width)
self.rect.y = random.randint(-100, -40)
self.speed_y = random.randint(1, 3)
def update(self):
self.rect.y += self.speed_y
if self.rect.top > screen_height:
self.rect.x = random.randint(0, screen_width - self.rect.width)
self.rect.y = random.randint(-100, -40)
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y):
pygame.sprite.Sprite.__init__(self)
self.image = bullet_image
self.rect = self.image.get_rect()
self.rect.centerx = x
self.rect.bottom = y
self.speed_y = -5
def update(self):
self.rect.y += self.speed_y
if self.rect.bottom < 0:
self.kill()
player_plane = PlayerPlane()
enemy_planes = pygame.sprite.Group()
for i in range(5):
enemy_plane = EnemyPlane()
enemy_planes.add(enemy_plane)
score = 0
font = pygame.font.Font(None, 36)
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
player_plane.speed_x = -3
elif event.key == pygame.K_RIGHT:
player_plane.speed_x = 3
elif event.key == pygame.K_UP:
player_plane.speed_y = -3
elif event.key == pygame.K_DOWN:
player_plane.speed_y = 3
elif event.key == pygame.K_SPACE:
player_plane.shoot()
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
player_plane.speed_x = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
player_plane.speed_y = 0
player_plane.update()
enemy_planes.update()
# 碰撞检测
hits = pygame.sprite.groupcollide(enemy_planes, player_plane.bullets, True, True)
for hit in hits:
score += 1
enemy_plane = EnemyPlane()
enemy_planes.add(enemy_plane)
screen.fill(BLACK)
screen.blit(player_plane.image, player_plane.rect)
enemy_planes.draw(screen)
player_plane.bullets.draw(screen)
# 显示得分
text = font.render("Score: " + str(score), 1, (255, 255, 255))
screen.blit(text, (10, 10))
pygame.display.update()
clock.tick(60)
三:运行效果展示
四:玩法介绍
上,下,左,右方向键控制移动方向,空格键控制发射。
(后续我还会更新哦)