开发工具:pycharm
游戏介绍:游戏开始时,一群外星人出现,他们在屏幕中向下移动,玩家的任务就是发射子弹消灭敌人,每消灭一个敌人,玩家积累一分。当敌人落地前未被玩家消灭,则游戏结束。
程序设计步骤:
1.游戏初始化
2.定义子弹类
3.定义玩家类
4.定义敌人类
5.设置游戏机制
6.游戏结束
一,游戏初始化
SCREEN_WIDTH=480 SCREEN_HEIGHT = 800 TYPE_SMALL=1 TYPE_MIDDLE=2 TYPE_BIG=3
二,定义子弹类
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
三,定义玩家类
1.设置玩家基本参数
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.is_hit = False
2.发射子弹
def shoot(self, bullet_img): bullet = Bullet(bullet_img, self.rect.midtop) self.bullets.add(bullet)
3.移动,需要判断的边界
上:
def moveUp(self): if self.rect.top <= 0: self.rect.top = 0 else: self.rect.top -= self.speed
下:
def moveDown(self): if self.rect.top >= SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height else: self.rect.top += self.speed
左:
def moveLeft(self): if self.rect.left <= 0: self.rect.left = 0 else: self.rect.left -= self.speed
右:
def moveRight(self): if self.rect.left >= SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left += self.speed
四,定义敌人类
class Enemy(pygame.sprite.Sprite): def __init__(self, enemy_img, enemy_down_imgs, init_pos): pygame.sprite.Sprite.__init__(self) self.image = enemy_img self.rect = self.image.get_rect() self.rect.topleft = init_pos self.down_imgs = enemy_down_imgs self.speed = 2 def move(self): self.rect.top += self.speed
五,设置游戏机制
while running: clock.tick(60) if not player.is_hit: if shoot_frequency % 15 == 0: player.shoot(bullet_img) shoot_frequency += 1 if shoot_frequency >= 15: shoot_frequency = 0 if enemy_frequency % 50 == 0: enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) enemies1.add(enemy1) enemy_frequency += 1 if enemy_frequency >= 100: enemy_frequency = 0 for bullet in player.bullets: # 以固定速度移动子弹 bullet.move() # 移动出屏幕后删除子弹 if bullet.rect.bottom < 0: player.bullets.remove(bullet) for enemy in enemies1: #2. 移动敌机 enemy.move() #3. 敌机与玩家飞机碰撞效果处理 if pygame.sprite.collide_circle(enemy, player): enemies_down.add(enemy) enemies1.remove(enemy) player.is_hit = True break #4. 移动出屏幕后删除敌人 if enemy.rect.top < 0: enemies1.remove(enemy) #敌机被子弹击中效果处理 #将被击中的敌机对象添加到击毁敌机 Group 中 enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1) for enemy_down in enemies1_down: enemies_down.add(enemy_down) # 绘制背景 screen.fill(0) screen.blit(background, (0, 0)) # 绘制玩家飞机 if not player.is_hit: screen.blit(player.image[0], player.rect) #将正常飞机画出来 else: # 玩家飞机被击中后的效果处理 screen.blit(player.image[1], player.rect) #将爆炸的飞机画出来 running = False # 敌机被子弹击中效果显示 for enemy_down in enemies_down: enemies_down.remove(enemy_down) score += 1 screen.blit(enemy_down.down_imgs, enemy_down.rect) #将爆炸的敌机画出来 # 显示子弹 player.bullets.draw(screen) # 显示敌机 enemies1.draw(screen) # 绘制得分 score_font = pygame.font.Font(None, 36) score_text = score_font.render('score: '+str(score), True, (128, 128, 128)) text_rect = score_text.get_rect() text_rect.topleft = [10, 10] screen.blit(score_text, text_rect) pygame.display.update() # 处理游戏退出 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() # 获取键盘事件(上下左右按键) key_pressed = pygame.key.get_pressed() # 处理键盘事件(移动飞机的位置) if key_pressed[K_w] or key_pressed[K_UP]: player.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: player.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: player.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: player.moveRight()
六,游戏结束
while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() pygame.display.update()
参考代码:
import pygame from sys import exit from pygame.locals import * import random SCREEN_WIDTH=480 SCREEN_HEIGHT = 800 TYPE_SMALL=1 TYPE_MIDDLE=2 TYPE_BIG=3 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.is_hit = False # 玩家是否被击中 # 发射子弹 def shoot(self, bullet_img): bullet = Bullet(bullet_img, self.rect.midtop) self.bullets.add(bullet) # 向上移动,需要判断边界 def moveUp(self): if self.rect.top <= 0: self.rect.top = 0 else: self.rect.top -= self.speed # 向下移动,需要判断边界 def moveDown(self): if self.rect.top >= SCREEN_HEIGHT - self.rect.height: self.rect.top = SCREEN_HEIGHT - self.rect.height else: self.rect.top += self.speed # 向左移动,需要判断边界 def moveLeft(self): if self.rect.left <= 0: self.rect.left = 0 else: self.rect.left -= self.speed # 向右移动,需要判断边界 def moveRight(self): if self.rect.left >= SCREEN_WIDTH - self.rect.width: self.rect.left = SCREEN_WIDTH - self.rect.width else: self.rect.left += self.speed class Enemy(pygame.sprite.Sprite): def __init__(self, enemy_img, enemy_down_imgs, init_pos): pygame.sprite.Sprite.__init__(self) self.image = enemy_img self.rect = self.image.get_rect() self.rect.topleft = init_pos self.down_imgs = enemy_down_imgs self.speed = 2 def move(self): self.rect.top += self.speed pygame.init() screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT)) pygame.display.set_caption('Python打飞机大战') background = pygame.image.load('resources/image/background.png').convert() game_over = pygame.image.load('resources/image/gameover.png') plane_img = pygame.image.load('resources/image/shoot.png') player_rect = [] player_rect.append(pygame.Rect(0, 99, 102, 126)) # 玩家飞机图片 player_rect.append(pygame.Rect(165, 234, 102, 126)) # 玩家爆炸图片 player_pos = [200, 600] player = Player(plane_img, player_rect, player_pos) bullet_rect = pygame.Rect(1004, 987, 9, 21) bullet_img = plane_img.subsurface(bullet_rect) enemy1_rect = pygame.Rect(534, 612, 57, 43) enemy1_img = plane_img.subsurface(enemy1_rect) enemy1_down_imgs = plane_img.subsurface(pygame.Rect(267, 347, 57, 43)) enemies1 = pygame.sprite.Group() enemies_down = pygame.sprite.Group() shoot_frequency = 0 enemy_frequency = 0 score = 0 clock = pygame.time.Clock() running = True while running: clock.tick(60) if not player.is_hit: if shoot_frequency % 15 == 0: player.shoot(bullet_img) shoot_frequency += 1 if shoot_frequency >= 15: shoot_frequency = 0 if enemy_frequency % 50 == 0: enemy1_pos = [random.randint(0, SCREEN_WIDTH - enemy1_rect.width), 0] enemy1 = Enemy(enemy1_img, enemy1_down_imgs, enemy1_pos) enemies1.add(enemy1) enemy_frequency += 1 if enemy_frequency >= 100: enemy_frequency = 0 for bullet in player.bullets: # 以固定速度移动子弹 bullet.move() # 移动出屏幕后删除子弹 if bullet.rect.bottom < 0: player.bullets.remove(bullet) for enemy in enemies1: #2. 移动敌机 enemy.move() #3. 敌机与玩家飞机碰撞效果处理 if pygame.sprite.collide_circle(enemy, player): enemies_down.add(enemy) enemies1.remove(enemy) player.is_hit = True break #4. 移动出屏幕后删除敌人 if enemy.rect.top < 0: enemies1.remove(enemy) #敌机被子弹击中效果处理 #将被击中的敌机对象添加到击毁敌机 Group 中 enemies1_down = pygame.sprite.groupcollide(enemies1, player.bullets, 1, 1) for enemy_down in enemies1_down: enemies_down.add(enemy_down) # 绘制背景 screen.fill(0) screen.blit(background, (0, 0)) # 绘制玩家飞机 if not player.is_hit: screen.blit(player.image[0], player.rect) #将正常飞机画出来 else: # 玩家飞机被击中后的效果处理 screen.blit(player.image[1], player.rect) #将爆炸的飞机画出来 running = False # 敌机被子弹击中效果显示 for enemy_down in enemies_down: enemies_down.remove(enemy_down) score += 1 screen.blit(enemy_down.down_imgs, enemy_down.rect) #将爆炸的敌机画出来 # 显示子弹 player.bullets.draw(screen) # 显示敌机 enemies1.draw(screen) # 绘制得分 score_font = pygame.font.Font(None, 36) score_text = score_font.render('score: '+str(score), True, (128, 128, 128)) text_rect = score_text.get_rect() text_rect.topleft = [10, 10] screen.blit(score_text, text_rect) pygame.display.update() # 处理游戏退出 for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() # 获取键盘事件(上下左右按键) key_pressed = pygame.key.get_pressed() # 处理键盘事件(移动飞机的位置) if key_pressed[K_w] or key_pressed[K_UP]: player.moveUp() if key_pressed[K_s] or key_pressed[K_DOWN]: player.moveDown() if key_pressed[K_a] or key_pressed[K_LEFT]: player.moveLeft() if key_pressed[K_d] or key_pressed[K_RIGHT]: player.moveRight() # 游戏 Game Over 后显示最终得分 font = pygame.font.Font(None, 64) text = font.render('Final Score: '+ str(score), True, (255, 0, 0)) text_rect = text.get_rect() text_rect.centerx = screen.get_rect().centerx text_rect.centery = screen.get_rect().centery + 24 screen.blit(game_over, (0, 0)) screen.blit(text, text_rect) # 显示得分并处理游戏退出 while 1: for event in pygame.event.get(): if event.type == pygame.QUIT: pygame.quit() exit() pygame.display.update()
运行结果:
来源:oschina
链接:https://my.oschina.net/u/4463382/blog/3192409