项目名如标题,在《python编程:从入门到实践》里有完整详细的说明,网上也能查到源码,这里只是在重复这种程序
不过学编程,借鉴模仿很重要,即使抄轮子也得抄个像模像样才行,抄到即是学到。
进入正题:
游戏很简单,就是一个打飞机类型的游戏,以下代码将实现游戏的主程序,飞船的基本 *** 作等。
注:这里提醒新上手的萌新,python的代码不要随便复制粘贴!
不要随便复制粘贴!
不要随便复制粘贴!
python的缩进很严格,复制粘贴后一定要注意格式是否正确,否则会报出异常:
IndentationError: unexpected indent
未知的缩进错误
当出现此提示时,要检查一下哪里的缩进出现明答了问题,小段的代码还好,大段的代码就呵呵激拿慧了
注意下载好的文件后缀名为.whl
然后新建一敏纯个文件夹,名字叫alien_invasion,将下载好的模块放在此文件夹下,
然后你要新建以下文件:
1. alien_invasion.py 游戏主程序
2. game_functions.py 存放游戏函数
3. bullet.py 子d模块
4. settings.py 游戏设置模块
5. ship.py 飞船模块
然后新建images文件夹存放飞船素材。
这些会让我们实现启动游戏,飞船的显示与移动,以及开火功能。
为避免文章冗~长,这里贴一段主程序代码,其它代码下周见(偷懒)。
alien_invasion.py
#!usr/bin/env python3
#_*_ coding:utf-8 _*_
#此模块是游戏的主程序
import sys
import game_functions as gf #导入game_functions文件,并指定了别名gf
import pygame
from settings import Settings
from ship import Ship
from pygame.sprite import Group
def run_game():
# 初始化游戏并创建一个屏幕对象
pygame.init() #pygame的init函数,用于创建一个游戏对象
#把Setting类实例化
ai_settings = Settings()
#显示游戏窗口并设置其大小
screen = pygame.display.set_mode(
(ai_settings.screen_width,ai_settings.screen_height))
#设置窗口标题
pygame.display.set_caption("Alien Invasion")
# 把Ship类实例化,用于创建飞船
ship = Ship(ai_settings,screen)
#存储子d的编组
bullets = Group()
#开始游戏的主循环
while True:
#用于响应鼠标,键盘事件
gf.check_events(ai_settings,screen,ship,bullets)
#更新飞船在屏幕的位置
ship.update()
#更新子d位置并删除消失的子d
gf.update_bullets(bullets)
#用于更新屏幕背景色,更新飞船,子d等
gf.update_screen(ai_settings,screen,ship,bullets)
run_game() #运行游戏
import pygamefrom settings import Settings
from ship import Ship
import game_functions as gf
from pygame.sprite import Group
def run_game():
# 初始迟源化游戏并创建一个屏冲缓幕对象
pygame.init()
ai_settings = Settings()
screen = pygame.display.set_mode((ai_settings.screen_width, ai_settings.screen_height))
pygame.display.set_caption("Alien Invasion")
# 创建一艘飞船
ship = Ship(ai_settings, screen)
# 创建一个用码判态于存储子d的编组
bullets = Group()
# 开始游戏主循环
while True:
gf.check_events(ai_settings, screen, ship, bullets)
ship.update()
bullets.update()
gf.update_bullets(ship, bullets)
gf.update_screen(ai_settings, screen, ship, bullets)
run_game()
1、我方飞型竖机模块import pygame
class MyPlane(pygame.sprite.Sprite):
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image1 = pygame.image.load(r'卜轮大E:\learn pygame\image\me1.png').convert_alpha()
self.image2 = pygame.image.load(r'E:\learn pygame\image\me2.png').convert_alpha()
self.image3 = pygame.image.load(r'E:\learn pygame\image\me_protect.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\me_destroy_1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\me_destroy_4.png').convert_alpha() \
])
self.rect = self.image1.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 60
self.speed = 10
self.active = True
self.invincible = False
self.mask = pygame.mask.from_surface(self.image1)
def moveUp(self):
if self.rect.top >0:
self.rect.top -= self.speed
else:
self.rect.top = 0
def moveDown(self):
if self.rect.bottom <self.height - 30:
self.rect.top += self.speed
else:
self.rect.bottom = self.height - 30
def moveLeft(self):
if self.rect.left >桐穗 0:
self.rect.left -= self.speed
else:
self.rect.left = 0
def moveRight(self):
if self.rect.right <self.width:
self.rect.left += self.speed
else:
self.rect.right = self.width
def reset(self):
self.rect.left,self.rect.top = (self.width - self.rect.width) // 2,self.height - self.rect.height - 30
self.active = True
self.invincible = True
2、敌机与背景动图模块
import pygame
from random import *
class SmallEnemy(pygame.sprite.Sprite):
energy = 1
def __init__(self,bg_size):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(r'E:\learn pygame\image\enemy1.png').convert_alpha()
self.destroy_images = []
self.destroy_images.extend([\
pygame.image.load(r'E:\learn pygame\image\enemy1_down1.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down2.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down3.png').convert_alpha(), \
pygame.image.load(r'E:\learn pygame\image\enemy1_down4.png').convert_alpha() \
])
self.rect = self.image.get_rect()
self.width,self.height = bg_size[0],bg_size[1]
self.speed = 2
self.active = True
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5 * self.height, 0)
self.mask = pygame.mask.from_surface(self.image)
self.energy = SmallEnemy.energy
self.hit = False
def move(self):
if self.rect.top <self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = SmallEnemy.energy
self.rect.left,self.rect.top = \
randint(0,self.width - self.rect.width), \
randint(-5 * self.height, 0)
class MidEnemy(pygame.sprite.Sprite):
energy = 4
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)