用python做一个飞机大战(一)

您所在的位置:网站首页 编程猫源码编辑器制作飞机大战 用python做一个飞机大战(一)

用python做一个飞机大战(一)

2024-07-03 04:15| 来源: 网络整理| 查看: 265

1.用python做一个飞机大战(一) 2.添加自己飞机的生命,击落敌机的分数,背景音乐(二) 用到的一个python插件,pygame,想要深入学习python游戏开发可访问官网:https://www.pygame.org/news 实现后的飞机大战: 在这里插入图片描述

1.创建一个飞机的精灵

import pygame import random SCREEN_SIZE = None CLOCK_TICK = 60 EMENY_FLY_MAX_SPEED = 12 #背景类 class BackGround(pygame.sprite.Sprite): def __init__(self,bg_pathname,speed = 1,is_first = True): global SCREEN_SIZE # print(SCREEN_SIZE) super().__init__() self.image = pygame.image.load(bg_pathname) self.rect = self.image.get_rect() # 0 0 width height self.speed = speed if not is_first: self.rect.y = -SCREEN_SIZE.height def update(self, *args): self.rect.y += self.speed if self.rect.y >SCREEN_SIZE.height: self.rect.y = -SCREEN_SIZE.height #敌机类 class Enemy(pygame.sprite.Sprite): def __init__(self,image_path,plane_size = 1): super().__init__() self.image = pygame.image.load(image_path) self.rect = self.image.get_rect() self.rect.bottom = 0 # self.hp = 1 self.rect.x = random.randint(0,SCREEN_SIZE.width-self.rect.width) if plane_size == 1: self.speed = random.randint(1,EMENY_FLY_MAX_SPEED) # self.hp = 1 elif plane_size == 2: self.speed = random.randint(1,EMENY_FLY_MAX_SPEED/2) # self.hp = EMENY_FLY_MAX_SPEED/2 else: self.speed = random.randint(1, EMENY_FLY_MAX_SPEED / 3) # self.hp = EMENY_FLY_MAX_SPEED * 2 def update(self, *args): self.rect.y += self.speed if self.rect.y >= SCREEN_SIZE.height: self.kill() def __del__(self): #print('over...') pass #英雄类 class Hero(pygame.sprite.Sprite): def __init__(self,pathname1,pathname2): super().__init__() self.image = None self.image_flag = False self.speed = {'x':0,'y':0} self.image_all = [pygame.image.load(pathname1),pygame.image.load(pathname2)] self.rect = self.image_all[0].get_rect() self.rect.centerx = SCREEN_SIZE.centerx self.rect.bottom = SCREEN_SIZE.height - 100 self.bullets = pygame.sprite.Group() def update(self): self.image = self.image_all[int(self.image_flag)] self.image_flag = not self.image_flag self.rect.x += self.speed['x'] self.rect.y += self.speed['y'] if self.rect.centerx SCREEN_SIZE.width: self.rect.centerx = SCREEN_SIZE.width if self.rect.top > SCREEN_SIZE.height-self.rect.height: self.rect.bottom = SCREEN_SIZE.height if self.rect.bottom


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3