赞
踩
我想大家都是有玩过类似飞机大战的射击类游戏,也享受目标被消除通过后带来的愉悦感。
那么如果用Python来实现飞机大战游戏?
下面就跟大家一起用Python来做一个小游戏,不仅可以从中感受到乐趣,还可以提升自己的能力。
游戏的基本设定:
敌方共有大中小3款飞机,分为高中低三种速度;
子弹的射程并非全屏,而大概是屏幕长度的80%;
消灭小飞机需要1发子弹,中飞机需要8发,大飞机需要20发子弹;
每消灭一架小飞机得1000分,中飞机6000分,大飞机10000分;
每隔30秒有一个随机的道具补给,分为两种道具,全屏炸弹和双倍子弹;
全屏炸弹最多只能存放3枚,双倍子弹可以维持18秒钟的效果;
游戏将根据分数来逐步提高难度,难度的提高表现为飞机数量的增多以及速度的加快。
首先创建一个myplane. py模块来定义我方飞机:
-
- import pygame
-
- class MyPlane(pygame.sprite.Sprite):
- def __init__(self, bg_size):
- pygame.sprite.Sprite.__init__(self)
-
- self.image1 = pygame.image.load("images/me1.png").convert_alpha()
- self.image2 = pygame.image.load("images/me2.png").convert_alpha()
- self.destroy_images = []
- self.destroy_images.extend([\
- pygame.image.load("images/me_destroy_1.png").convert_alpha(), \
- pygame.image.load("images/me_destroy_2.png").convert_alpha(), \
- pygame.image.load("images/me_destroy_3.png").convert_alpha(), \
- pygame.image.load("images/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)
-
- # 分别定义moveUp()、moveDown()、moveLeft()和moveRight()控制我方飞机上、下、左、右移动:
-
- 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 - 60:
- self.rect.top += self.speed
- else:
- self.rect.bottom = self.height - 60
-
- 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 - 60
- self.active = True
- self.invincible = True
敌机分为小、中、大三个尺寸,它们的速度依次是快、中、慢,在游戏界面的上方位置创造位置随机的敌机,可以让它们不在同一排出现。将敌机的定义写在enemy. py模块中:
-
- import pygame
- from random import *
- class SmallEnemy(pygame.sprite.Sprite):
- def __init__(self, bg_size):
- pygame.sprite.Sprite.__init__(self)
-
- self.image = pygame.image.load("images/enemy1.png").convert_alpha()
- self.destroy_images = []
- self.destroy_images.extend([ \
- pygame.image.load("images/enemy1_down1.png").convert_alpha(), \
- pygame.image.load("images/enemy1_down2.png").convert_alpha(), \
- pygame.image.load("images/enemy1_down3.png").convert_alpha(), \
- pygame.image.load("images/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)
-
- def move(self):
- if self.rect.top < self.height:
- self.rect.top += self.speed
- else:
- self.reset()
-
- def reset(self):
- self.active = True
- self.rect.left, self.rect.top = \
- randint(0, self.width - self.rect.width), \
- randint(-5 * self.height, 0)
-
-
- class MidEnemy(pygame.sprite.Sprite):
- energy = 8
-
- def __init__(self, bg_size):
- pygame.sprite.Sprite.__init__(self)
-
- self.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。