Python制作飞机大战
注:代码中的常量建议在代码开头定义出来,默认规则定义为大写字母表示
屏幕尺寸大小常量
SCREEN_BACK = pygame.Rect(0,0,550,700)
#敌机定时器常量
CHANGE_TIME_EVENT = pygame.USEREVENT
#子弹定时器常量
HERO_TIME_EVENT = pygame.USEREVENT + 1
一、绘制屏幕与创建时钟对象(只显示关键代码)
def __init__(self):
#设置屏幕大小
self.screen = pygame.display.set_mode(SCREEN_BACK.size)
#创建时钟对象
self.clock = pygame.time.Clock()
#创建精灵,精灵族
self.__creat_sprits()
#设置定时器,1秒
pygame.time.set_timer(CHANGE_TIME_EVENT,500)
pygame.time.set_timer(HERO_TIME_EVENT,300)
ps设置敌机产生的时间
二、进行游戏循环
1.设置帧率(创建的时钟对象)
self.clock.tick(60)
这里帧率设置成60即可
2.事件监听
for p in pygame.event.get():
if p.type == pygame.QUIT:
self.__game_over()
elif p.type == CHANGE_TIME_EVENT:
enay= Enamy()
self.enamy_group.add(enay)
elif p.type ==HERO_TIME_EVENT:
self.hero.fire()
key_passdown = pygame.key.get_pressed()
if key_passdown[pygame.K_RIGHT]:
self.hero.speed = 7
elif key_passdown[pygame.K_LEFT]:
self.hero.speed = -7
elif key_passdown[pygame.K_UP]:
self.hero.speed = -5
elif key_passdown[pygame.K_DOWN]:
self.hero.speed = 5
else:
self.hero.speed = 0
3.碰撞检测
#1.子弹摧毁敌机、
pygame.sprite.groupcollide(self.hero.bullt_group,self.enamy_group,True,True)
#2.敌机摧毁英雄
list = pygame.sprite.spritecollide(self.hero,self.enamy_group,True)
if len(list)>0:
self.hero.kill()
self.__game_over()
4.精组的创建与刷新
def __update_draw(self):
'''绘制刷新'''
self.sprite_group.update()
self.sprite_group.draw(self.screen)
#敌机
self.enamy_group.update()
self.enamy_group.draw(self.screen)
#英雄
self.hero_group.update()
self.hero_group.draw(self.screen)
#子弹
self.hero.bullt_group.update()
self.hero.bullt_group.draw(self.screen)
5.屏幕刷新
pygame.display.update()
6.精灵基类的建立
class Gamesprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed = 1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
pygame.display.update()
ps:在定义精灵基类的时候需要已Pygame默认的精灵类为父类,需要用super()继承基类的初始化。
精灵类在进行初始化是需要定义三个变量,image:将图片加载到内存中(load方法),
rect:get_ract方法得到图片的大小,得到的是rect对象
speed:图片移动的速度。
7.背景类的创建(实现图片滑动的感觉)
#创建背景精灵,组
bg1 = Backgroud()
bg2 = Backgroud(True)
self.sprite_group = pygame.sprite.Group(bg1,bg2)
class Backgroud(Gamesprite):
def __init__(self,is_alt=False):
super().__init__("E:\椋炴満澶ф垬\images\\background.png")
self.speed = 3
if is_alt == True:
self.rect.y = -self.rect.height
def update(self):
#璋冪敤鐖剁被鐨勬柟娉�
super().update()
#鍒ゆ柇鍥剧墖鏄惁婊戝嚭骞冲睆骞�
if self.rect.y > SCREEN_BACK.height:
self.rect.y = -SCREEN_BACK.height
8.创建敌机类
class Enamy(Gamesprite):
def __init__(self):
super().__init__("E:\椋炴満澶ф垬\images\enemy1.png")
#璁剧疆闅忔満閫熷害
self.speed = random.randint(1,3)
self.rect.bottom = 0
self.rect.x = random.randint(0,(SCREEN_BACK.width-self.rect.width))
def update(self):
super().update()
if self.rect.y > SCREEN_BACK.height:
self.kill()
9.创建英雄类
class Hero(Gamesprite):
"""鑻遍泟"""
def __init__(self):
super().__init__("E:\椋炴満澶ф垬\images\me1.png")
self.speed = 0
self.rect.centerx = SCREEN_BACK.centerx
self.rect.bottom = 570
self.bullt_group = pygame.sprite.Group()
def update(self):
if self.speed == SPEED_X or self.speed == -SPEED_X:
self.rect.x += self.speed
if self.speed ==SPEED_Y or self.speed == -SPEED_Y:
self.rect.y += self.speed
if self.rect.x <0:
self.rect.x=0
elif self.rect.right >SCREEN_BACK.right:
self.rect.right = SCREEN_BACK.right
elif self.rect.y < 0:
self.rect.y = 0
elif self.rect.y > SCREEN_BACK.height - self.rect.height:
self.rect.y = SCREEN_BACK.height - self.rect.height
def fire(self):
#璁剧疆瀛愬脊鐨勪綅缃�
for i in (0,1,2):
bullt = Bullet()
bullt.rect.bottom = self.rect.y -i*30
bullt.rect.centerx = self.rect.centerx
self.bullt_group.add(bullt)
10.创建子弹类
class Bullet(Gamesprite):
def __init__(self):
super().__init__("E:\椋炴満澶ф垬\images\\bullet1.png",-4)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
11.在主函数中创建不同的精灵,和精灵组实例化
def __creat_sprits(self):
"""创建精灵和精灵组"""
#创建背景精灵,组
bg1 = Backgroud()
bg2 = Backgroud(True)
self.sprite_group = pygame.sprite.Group(bg1,bg2)
#创建敌机精灵组
self.enamy_group = pygame.sprite.Group()
#创建英雄
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
附上源码:
import pygame
import random
#灞忓箷灏哄澶у皬甯搁噺
SCREEN_BACK = pygame.Rect(0,0,550,700)
#瀛愬脊閫熷害甯搁噺
SPEED_X = 7
SPEED_Y = 5
class Gamesprite(pygame.sprite.Sprite):
def __init__(self,image_name,speed = 1):
super().__init__()
self.image = pygame.image.load(image_name)
self.rect = self.image.get_rect()
self.speed = speed
def update(self):
self.rect.y += self.speed
pygame.display.update()
#鑳屾櫙绫�
class Backgroud(Gamesprite):
def __init__(self,is_alt=False):
super().__init__("E:\椋炴満澶ф垬\images\\background.png")
self.speed = 3
if is_alt == True:
self.rect.y = -self.rect.height
def update(self):
#璋冪敤鐖剁被鐨勬柟娉�
super().update()
#鍒ゆ柇鍥剧墖鏄惁婊戝嚭骞冲睆骞�
if self.rect.y > SCREEN_BACK.height:
self.rect.y = -SCREEN_BACK.height
class Enamy(Gamesprite):
def __init__(self):
super().__init__("E:\椋炴満澶ф垬\images\enemy1.png")
#璁剧疆闅忔満閫熷害
self.speed = random.randint(1,3)
self.rect.bottom = 0
self.rect.x = random.randint(0,(SCREEN_BACK.width-self.rect.width))
def update(self):
super().update()
if self.rect.y > SCREEN_BACK.height:
self.kill()
class Hero(Gamesprite):
"""鑻遍泟"""
def __init__(self):
super().__init__("E:\椋炴満澶ф垬\images\me1.png")
self.speed = 0
self.rect.centerx = SCREEN_BACK.centerx
self.rect.bottom = 570
self.bullt_group = pygame.sprite.Group()
def update(self):
if self.speed == SPEED_X or self.speed == -SPEED_X:
self.rect.x += self.speed
if self.speed ==SPEED_Y or self.speed == -SPEED_Y:
self.rect.y += self.speed
if self.rect.x <0:
self.rect.x=0
elif self.rect.right >SCREEN_BACK.right:
self.rect.right = SCREEN_BACK.right
elif self.rect.y < 0:
self.rect.y = 0
elif self.rect.y > SCREEN_BACK.height - self.rect.height:
self.rect.y = SCREEN_BACK.height - self.rect.height
def fire(self):
#璁剧疆瀛愬脊鐨勪綅缃�
for i in (0,1,2):
bullt = Bullet()
bullt.rect.bottom = self.rect.y -i*30
bullt.rect.centerx = self.rect.centerx
self.bullt_group.add(bullt)
class Bullet(Gamesprite):
def __init__(self):
super().__init__("E:\椋炴満澶ф垬\images\\bullet1.png",-4)
def update(self):
super().update()
if self.rect.bottom < 0:
self.kill()
import pygame
from plane_sprite import *
pygame.init()
#屏幕尺寸大小常量
SCREEN_BACK = pygame.Rect(0,0,550,700)
#敌机定时器常量
CHANGE_TIME_EVENT = pygame.USEREVENT
#子弹定时器常量
HERO_TIME_EVENT = pygame.USEREVENT + 1
class Plangame:
#游戏初始化
def __init__(self):
#设置屏幕大小
self.screen = pygame.display.set_mode(SCREEN_BACK.size)
#创建时钟对象
self.clock = pygame.time.Clock()
#创建精灵,精灵族
self.__creat_sprits()
#设置定时器,1秒
pygame.time.set_timer(CHANGE_TIME_EVENT,500)
pygame.time.set_timer(HERO_TIME_EVENT,300)
def __creat_sprits(self):
"""创建精灵和精灵组"""
#创建背景精灵,组
bg1 = Backgroud()
bg2 = Backgroud(True)
self.sprite_group = pygame.sprite.Group(bg1,bg2)
#创建敌机精灵组
self.enamy_group = pygame.sprite.Group()
#创建英雄
self.hero = Hero()
self.hero_group = pygame.sprite.Group(self.hero)
def start_game(self):
while True:
#游戏循环的
#帧率
self.clock.tick(60)
#事件监听
self.__event_check()
#碰撞检测
self.__check_peng()
#精灵组刷新和绘制
self.__update_draw()
#屏幕刷新
pygame.display.update()
pass
def __event_check(self):
'''事件监听'''
for p in pygame.event.get():
if p.type == pygame.QUIT:
self.__game_over()
elif p.type == CHANGE_TIME_EVENT:
enay= Enamy()
self.enamy_group.add(enay)
elif p.type ==HERO_TIME_EVENT:
self.hero.fire()
key_passdown = pygame.key.get_pressed()
if key_passdown[pygame.K_RIGHT]:
self.hero.speed = 7
elif key_passdown[pygame.K_LEFT]:
self.hero.speed = -7
elif key_passdown[pygame.K_UP]:
self.hero.speed = -5
elif key_passdown[pygame.K_DOWN]:
self.hero.speed = 5
else:
self.hero.speed = 0
def __check_peng(self):
'''碰撞检测'''
#1.子弹摧毁敌机、
pygame.sprite.groupcollide(self.hero.bullt_group,self.enamy_group,True,True)
#2.敌机摧毁英雄
list = pygame.sprite.spritecollide(self.hero,self.enamy_group,True)
if len(list)>0:
self.hero.kill()
self.__game_over()
def __update_draw(self):
'''绘制刷新'''
self.sprite_group.update()
self.sprite_group.draw(self.screen)
#敌机
self.enamy_group.update()
self.enamy_group.draw(self.screen)
#英雄
self.hero_group.update()
self.hero_group.draw(self.screen)
#子弹
self.hero.bullt_group.update()
self.hero.bullt_group.draw(self.screen)
@staticmethod
def __game_over():
print("游戏结束")
pygame.quit()
exit()
if __name__ == '__main__':
plane = Plangame()
plane.start_game()