学习完Python后,为巩固与练习,因而编写了《飞机大战》小游戏。(共六个章节)
需求:
1. 玩家飞机(hero)
2. 敌人飞机(enemy0、enemy1、enemy2)
3. 补给品(blood_supply血量补给、bullet_supply弹药补给)
4. 子弹
类
1. Base(所有类的基类)
2. BasePlane(飞机类的基类)
3. HeroPlane(hero飞机类)
4. Enemy0Plane、Enemy1Plane、Enemy2Plane(三种敌机类)
5. BaseBullet(所有子弹的基类)
6. Bullet(hero子弹)EnemyBullet(enemy子弹)
6. supply_2_hero(补给品类)
以及众多的全局变量和函数。
1 - 窗口和hero的显示
# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import time
def main():
#1. 创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2. 创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3. 创建一个飞机图片
hero = pygame.image.load("./feiji/hero1.png")
while True:
screen.blit(background, (0,0))
screen.blit(hero, (195, 700))
pygame.display.update()
time.sleep(0.01)
if __name__ == "__main__":
main()
def key_control(hero_temp):
#获取事件,比如按键等
for event in pygame.event.get():
#判断是否是点击了退出按钮
if event.type == QUIT:
print("exit")
exit()
#判断是否是按下了键
elif event.type == KEYDOWN:
#检测按键是否是a或者left
if event.key == K_a or event.key == K_LEFT:
print('left')
hero_temp.move_left()
#检测按键是否是d或者right
elif event.key == K_d or event.key == K_RIGHT:
print('right')
hero_temp.move_right()
#检测按键是否是空格键
elif event.key == K_SPACE:
print('space')
3 - 子弹基类
class Bullet(object):
def __init__(self, screen_temp, x, y):
self.x = x+40
self.y = y-20
self.screen = screen_temp
self.image = pygame.image.load("./feiji/bullet.png")
def display(self):
self.screen.blit(self.image, (self.x, self.y))
def move(self):
self.y -= 10
4 - hero左右移动及发射子弹
class HeroPlane(object):
def __init__(self, screen_temp):
self.x = 195
self.y = 700
self.screen = screen_temp
self.image = pygame.image.load("./feiji/hero1.png")
self.bullet_list = [] #存储发射出去的子弹的引用
def display(self):
self.screen.blit(self.image, (self.x, self.y))
for bullet in self.bullet_list:
bullet.display()
bullet.move()
#当一直按下键盘时调用移动函数
def press_move(self):
if len(self.key_down_list) != 0:
if self.key_down_list[0] == K_LEFT:
self.move_left()
elif self.key_down_list[0] == K_RIGHT:
self.move_right()
def move_left(self):
self.x -= 5
def move_right(self):
self.x += 5
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
5 - Enemy0类
class EnemyPlane(object):
"""敌机的类"""
def __init__(self, screen_temp):
self.x = 0
self.y = 0
self.screen = screen_temp
self.image = pygame.image.load("./feiji/enemy0.png")
#self.bullet_list = [] #存储发射出去的子弹的引用
self.direction = "right" #飞机默认移动方向
def display(self):
self.screen.blit(self.image, (self.x, self.y))
#for bullet in self.bullet_list:
# bullet.display()
# bullet.move()
def move(self):
if self.direction == "right":
self.x += 5
elif self.direction == "left":
self.x -= 5
if self.x > 430:
self.direction = "left"
elif self.x < 0:
self.direction = "right"
def fire(self):
self.bullet_list.append(Bullet(self.screen, self.x, self.y))
def main():
#1. 创建窗口
screen = pygame.display.set_mode((480,852),0,32)
#2. 创建一个背景图片
background = pygame.image.load("./feiji/background.png")
#3. 创建一个飞机对象
hero = HeroPlane(screen)
#4. 创建一个敌机对象
enemy = EnemyPlane(screen)
while True:
screen.blit(background, (0,0))
hero.display()
enemy.display()
enemy.move()#调用敌机移动
pygame.display.update()
key_control(hero)
time.sleep(0.01)
《飞机大战》(一)的代码编写就先到这里结束,本次教程主要实现hero飞机的移动、子弹发射,Enemy0的移动。对本次游戏有一个初步的编写及认识。以后的教程或对其进一步的改进。
[pygame之《飞机大战》(二)]