python3实现飞机大战实现飞机大战
本文实例为大家分享了python3实现飞机大战的具体代码,供大家参考,具体内容如下
以下是亲测Python飞机大战全部代码,在保证有pygame环境支持并且有Python3解释器的话完全没问题!
如果大家喜欢的话麻烦点个赞!
运行效果如下图:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# 导入需要使用的模块
import pygame
from pygame.locals import *
from sys import exit
import random
# 设置屏幕大小的变量
SCREEN_WIDTH = 480
SCREEN_HEIGHT = 800
import codecs
# 子弹类
class Bullet(pygame.sprite.Sprite):
def __init__(self,bullet_img,init_pos):
# 实现父类的初始化方法
pygame.sprite.Sprite.__init__(self)
self.image = bullet_img
self.rect = self.image.get_rect()
self.rect.midbottom = init_pos
self.speed = 10
def move(self):
self.rect.top -= self.speed
# 玩家飞机类
class Player(pygame.sprite.Sprite):
def __init__(self,plane_img,player_rect,init_pos):
pygame.sprite.Sprite.__init__(self)
self.image=[] for i in range(len(player_rect)):
self.image.append(plane_img.subsurface(player_rect[i]).convert_alpha())
self.rect = player_rect[0] self.rect.topleft = init_pos
self.speed = 8
self.bullets = pygame.sprite.Group() #玩家飞机发射子弹的集合
self.img_index = 0
self.is_hit = False