5.敌机子弹部分
5.1创建敌机子弹
class EnemyBullet:
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.pic = pygame.image.load("img/bomb.png")
5.1.1图像绘制到屏幕
def draw(self):
self.screen.blit(self.pic, (self.x, self.y))
self.move()
5.1.2更新子弹的位置
def move(self):
self.y += 5
5.2创建敌机子弹列表
enemy_list = []
5.3创建敌机子弹和爆炸索引
enemy_bomb = False
enemy_index_bomb = 0
5.4循环用于绘制所有敌机发射的子弹
for enemy_bullet in enemy_list:
enemy_bullet.draw()
5.5设置子弹位置
enemy_Bulle_rect = pygame.rect.Rect(enemy_bullet.x, enemy_bullet.y, 5, 11)
5.6碰撞检测
flag2 = enemy_Bulle_rect.colliderect(hero_rect)
5.6.1设置敌机爆炸标志并移除子弹
if flag2:
print('战机爆炸')
hero_bomb = True
enemy_list.remove(enemy_bullet)
5.6.2导入爆炸图片
enemy_bomb_imgs = ['img/enemy2_down1.png',
'img/enemy2_down2.png',
'img/enemy2_down3.png',
'img/enemy2_down4.png']
5.6.3绘制敌机爆炸图片并设置时间
if not enemy_bomb:
if enemy_index_bomb == 0:
screan.blit(enemy_img1, (enmyPlaneX, enmyPlaneY))
enemy_index_bomb += 1
else:
screan.blit(enemy_img2, (enmyPlaneX, enmyPlaneY))
enemy_index_bomb = 0
else:
if enemy_index_bomb == len(enemy_bomb_imgs):
time.sleep(0.1)
exit(0)
enemy_bomb_img = pygame.image.load(enemy_bomb_imgs[enemy_index_bomb])
screan.blit(enemy_bomb_img,(enmyPlaneX,enmyPlaneY))
enemy_index_bomb +=1
time.sleep(0.2)
6.完整代码如下
import random
import time
import pygame
pygame.init()
screan = pygame.display.set_mode((480, 500))
shitindex = 0
bg_img = pygame.image.load("img/background.png")
hero_img1 = pygame.image.load("img/me1.png")
hero_img2 = pygame.image.load("img/me2.png")
hero_bomb_imgs = ["img/me_destroy_1.png",
"img/me_destroy_2.png",
"img/me_destroy_3.png",
"img/me_destroy_4.png"]
enemy_bomb_imgs = ['img/enemy2_down1.png',
'img/enemy2_down2.png',
'img/enemy2_down3.png',
'img/enemy2_down4.png']
hero_rect = pygame.rect.Rect(189, 374, 102, 126)
hero_PlaneX = hero_rect.x
hero_PlaneY = hero_rect.y
enemy_img1 = pygame.image.load('img/enemy2.png')
enemy_img2 = pygame.image.load("img/enemy2_hit.png")
enemy_rect = pygame.rect.Rect(198, 0, 69, 99)
enmyPlaneX = enemy_rect.x
enmyPlaneY = enemy_rect.y
pygame.key.set_repeat(20, 30)
clock = pygame.time.Clock()
direct = "left"
class HeroBullet:
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.pic = pygame.image.load("img/bullet1.png")
def draw(self):
self.screen.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y -= 5
class EnemyBullet:
def __init__(self, x, y, screen):
self.x = x
self.y = y
self.screen = screen
self.pic = pygame.image.load("img/bomb.png")
def draw(self):
self.screen.blit(self.pic, (self.x, self.y))
self.move()
def move(self):
self.y += 5
hero_Bulletlist = []
enemy_list = []
hero_bomb = False
hero_index_bomb = 0
enemy_bomb = False
enemy_index_bomb = 0
while True:
clock.tick(60)
pygame.display.set_caption("飞机大战")
icon = pygame.image.load("img/icon.png")
pygame.display.set_icon(icon)
screan.blit(bg_img, (0, 0))
if shitindex == 0:
screan.blit(hero_img1, (hero_PlaneX, hero_PlaneY))
shitindex += 1
else:
screan.blit(hero_img2, (hero_PlaneX, hero_PlaneY))
shitindex = 0
if direct == "left":
enmyPlaneX = enmyPlaneX - 5
if enmyPlaneX <= 0:
direct = "right"
elif direct == "right":
enmyPlaneX = enmyPlaneX + 5
if enmyPlaneX > 480 - 80:
direct = "left"
for bullet in hero_Bulletlist:
bullet.draw()
if bullet.y < 0:
hero_Bulletlist.remove(bullet)
continue
hero_bulle_rect = pygame.rect.Rect(bullet.x, bullet.y, 22, 22)
flag = hero_bulle_rect.colliderect(enemy_rect)
if flag:
hero_Bulletlist.remove(bullet)
enemy_bomb = True
if not enemy_bomb:
if enemy_index_bomb == 0:
screan.blit(enemy_img1, (enmyPlaneX, enmyPlaneY))
enemy_index_bomb += 1
else:
screan.blit(enemy_img2, (enmyPlaneX, enmyPlaneY))
enemy_index_bomb = 0
else:
if enemy_index_bomb == len(enemy_bomb_imgs):
time.sleep(0.1)
exit(0)
enemy_bomb_img = pygame.image.load(enemy_bomb_imgs[enemy_index_bomb])
screan.blit(enemy_bomb_img,(enmyPlaneX,enmyPlaneY))
enemy_index_bomb +=1
time.sleep(0.2)
x = random.randint(1, 100)
if x == 6 or x == 60:
enemy_Bullet = EnemyBullet(enmyPlaneX + 34 - 2, enmyPlaneY + 99, screan)
enemy_list.append(enemy_Bullet)
for enemy_bullet in enemy_list:
enemy_bullet.draw()
enemy_Bulle_rect = pygame.rect.Rect(enemy_bullet.x, enemy_bullet.y, 5, 11)
flag2 = enemy_Bulle_rect.colliderect(hero_rect)
if flag2:
print('战机爆炸')
hero_bomb = True
enemy_list.remove(enemy_bullet)
if hero_bomb:
if hero_index_bomb == len(hero_bomb_imgs):
time.sleep(0.3)
exit(0)
hero_bomb_img = pygame.image.load(hero_bomb_imgs[hero_index_bomb])
screan.blit(hero_bomb_img, (hero_PlaneX, hero_PlaneY))
hero_index_bomb += 1
time.sleep(0.2)
hero_PlaneY -= 1
if hero_PlaneY < 0:
hero_PlaneY = 650
screan.blit(enemy_img1, (enmyPlaneX, enmyPlaneY))
event_list = pygame.event.get()
for event in event_list:
if event.type == pygame.QUIT:
print("游戏结束了......")
pygame.quit()
exit(0)
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
hero_PlaneX = hero_PlaneX - 5 if hero_PlaneX >= 5 else 0
elif event.key == pygame.K_RIGHT:
hero_PlaneX = hero_PlaneX + 5 if hero_PlaneX <= 375 else 380
elif event.key == pygame.K_UP:
hero_PlaneY = hero_PlaneY - 5 if hero_PlaneY >= 5 else 0
elif event.key == pygame.K_DOWN:
hero_PlaneY = hero_PlaneY + 5 if hero_PlaneY <= 521 else 526
elif event.key == pygame.K_SPACE:
hero_Bullet = HeroBullet(hero_PlaneX + 50 - 2, hero_PlaneY - 11, screan)
hero_Bulletlist.append(hero_Bullet)
pygame.display.update()