本文目录一览:
- 1、如何为pygame的飞机大战增加一个分数排行榜?python
- 2、如何用用python写飞机大战?
- 3、python开发飞机大战外星人游戏怎么弄双人模式
- 4、怎么样用Python写飞机大战游戏
- 5、怎么用python学飞机大战?
- 6、python怎么写飞机大战?
如何为pygame的飞机大战增加一个分数排行榜?python
import pygame
from plane_sprites import*
from Background import BackGround
from Enemy import Enemy
from Hero import Hero
from time import sleep
from Bullet import Bullet
class Plane_main(object):
def __init__(self):
print('游戏初始化')
self.screen = pygame.display.set_mode(SCREEN_RECT.size)
self.clock = pygame.time.Clock()
self.__create_sprites()
pygame.time.set_timer(CREATE_ENEMY_EVENT,1000)
pygame.time.set_timer(HERO_FIRE_EVENT, 500)
self.count = 0
self.score = 0
def __create_sprites(self):
# 背景组
self.back_group = pygame.sprite.Group()
bg1 = BackGround()
bg2 = BackGround(True)
pygame.mixer.init()
pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
pygame.mixer.music.play(-1)
self.back_group.add(bg1,bg2)
# 敌机组
self.enemy_group = pygame.sprite.Group()
#敌级销毁组
self.enemy1_down_group=pygame.sprite.Group()
# 英雄组
self.hero_group = pygame.sprite.Group()
self.hero = Hero()
self.hero_group.add(self.hero)
def start_game(self):
pygame.init()
print("开始游戏...")
# pygame.mixer.init()
# pygame.mixer.music.load("./Capo Productions - Journey 00_00_00-00_00_59.ogg")
# pygame.mixer.music.play(-1)
# gameIcon = pygame.image.load("./name.png")
# pygame.display.set_icon(gameIcon)
while True:
self.count+=1
# 1. 设置刷新帧率
self.clock.tick(10)
# 2. 事件监听
self.__event_handler()
# 3. 碰撞检测
self.__check_collide()
# 4. 更新精灵组
self.__update_sprites()
# 5. 更新屏幕显示
pygame.display.update()
def __event_handler(self):
"""事件监听"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
Plane_main.__game_over()
elif event.type == CREATE_ENEMY_EVENT:
self.enemy_group.add(Enemy())
elif event.type == HERO_FIRE_EVENT:
self.hero.fire()
keys = pygame.key.get_pressed()
if keys[pygame.K_RIGHT]:
print('向右移动')
self.hero.speed = 5
self.hero.speed1 = 0
elif keys[pygame.K_LEFT]:
print('向左移动')
self.hero.speed = -5
self.hero.speed1 = 0
elif keys[pygame.K_UP]:
print('向上移动')
self.hero.speed1 = -5
self.hero.speed = 0
elif keys[pygame.K_DOWN]:
print('向下移动')
self.hero.speed1= 5
self.hero.speed = 0
else:
self.hero.speed = 0
self.hero.speed1 = 0
if event.type == HERO_FIRE_EVENT:
self.hero.fire()
print('发射子弹!!!!!')
def __check_collide(self):
"""碰撞检测"""
# 1. 子弹摧毁敌机
enemy_down = pygame.sprite.groupcollide(self.enemy_group, self.hero.bullets,True, True)
enemy1_down_group.add(enemy_down)
# 2. 敌机撞毁英雄
enemies = pygame.sprite.spritecollide(self.hero, self.enemy_group, True)
# 判断列表时候有内容`
if len(enemies) 0:
sleep(3)
# # 让英雄牺牲
self.hero.kill()
# 结束游戏
Plane_main.__game_over()
def __update_sprites(self):
self.back_group.update()
self.back_group.draw(self.screen)
self.enemy_group.update()
self.enemy_group.draw(self.screen)
self.hero_group.update()
self.hero_group.draw(self.screen)
self.hero.bullets.update()
self.hero.bullets.draw(self.screen)
self.drawText(str(self.score),SCREEN_RECT.width - 50,50)
for enemy1_down in enemy1_down_group:
self.screen.blit(enemy1_down_surface[enemy1_down.down_index],enemy1_down.rect)
if self.count % 5 ==0:
enemy1_down.down_index += 1
if enemy1_down.down_index ==3:
# pygame.mixer.music.load("./baozha.ogg")
# pygame.mixer.music.play()
self.score+=5
enemy1_down_group.remove(enemy1_down)
print(self.score)
#更新精灵组
# for group in [self.back_group, self.enemy_group, self.hero_group,self.hero.bullets]:
# group.update()
# group.draw(self.screen)
@staticmethod
def __game_over():
"""游戏结束"""
print("游戏结束")
pygame.quit()
exit()
def drawText(self,text,posx,posy,textHeight=48,fontColor=(0,0,0),backgroundColor=(111,225,1)):
fontObj = pygame.font.Font('freesansbold.ttf',textHeight)
textSurfaceObj = fontObj.render(text,13,fontColor,backgroundColor)
textRectObj = textSurfaceObj.get_rect()
textRectObj.center = (posx,posy)
self.screen.blit(textSurfaceObj,textRectObj)
#判断当前执行的文件是否是主文件
#如果不是 则if下的代码不会执行
if __name__ == '__main__':
game = Plane_main()
game.start_game()
如何用用python写飞机大战?
import pygame
from 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)
# 创建一个用于存储子弹的编组
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()
python开发飞机大战外星人游戏怎么弄双人模式
主函数
import pygamefrom settings import Settingsfrom ship import Shipimport game_function as gffrom pygame.sprite import Groupfrom alien import Alienfrom game_stats import GameStatsfrom button import Buttonfrom scoreboard import Scoreboarddef 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")# 创建Play按钮play_button = Button(ai_settings, screen, "Play")# 创建一个用于存储游戏统计信息的实例, 并创建记分牌stats = GameStats(ai_settings)sb = Scoreboard(ai_settings, screen, stats)# 创建一艘飞船ship = Ship(ai_settings, screen)# 创建一个用于存储子弹的数组bullets = Group()aliens = Group()# 创建外星人群gf.create_fleet(ai_settings, screen, ship, aliens)# 开始游戏主循环while True:gf.check_events(ai_settings, screen, stats, play_button, ship, aliens, bullets)if stats.game_active:ship.update()gf.update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets)gf.update_aliens(ai_settings, stats, screen, ship, aliens, bullets)gf.update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button)run_game()
怎么样用Python写飞机大战游戏
飞机大战的游戏用python写的很多,百度文库中也有相应的源码,自己参考一下吧.
怎么用python学飞机大战?
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
python怎么写飞机大战?
这个有专门的这个编程代码复制过去就可以了。在百度里面搜索。