5.8消灭飞碟
这一节中我们将在射出子弹的同时,消灭飞碟。消灭飞碟后再生成新的飞碟。下面是我们要做的事情。
1、射出子弹,当子弹碰撞到飞碟后,飞碟与子弹消失。
2、当一波飞碟全部被消灭后,新的一波飞碟又出现在太空中。
首先,我们修改射出子弹的代码,如下:
#射出子弹
for bulletx in bullets:
velx=math.cos(bulletx[0])*20
vely=math.sin(bulletx[0])*20
#print(velx)
#print(vely)
bulletx[1]+=velx
bulletx[2]+=vely
index=0
if bulletx[1]<0 or bulletx[1]>480 or bulletx[2]<0 or bulletx[2]>700:
bullets.pop(index)
index+=1
bulletrot = pygame.transform.rotate(bullet,360-bulletx[0]*57.29)
screen.blit(bulletrot,(bulletx[1],bulletx[2]))
bulletrect=pygame.Rect(bulletrot.get_rect())
bulletrect.left=bulletx[1]
bulletrect.top=bulletx[2]
击落飞碟的代码如下:
#击落飞碟
index=0
for flyingsaucer in flyingsaucers:
if flyingsaucer[1]<610:
flyingsaucer[1]+=0.5
screen.blit(enemy,flyingsaucer)
enemyrect=pygame.Rect(enemy.get_rect())
enemyrect.left=flyingsaucer[0]
enemyrect.top=flyingsaucer[1]
indexbull=0
for bulletx in bullets:
#print(len(bullets))
bulletrot = pygame.transform.rotate(bullet,360-bulletx[0]*57.29)
bulletrect=pygame.Rect(bulletrot.get_rect())
bulletrect.left=bulletx[1]
bulletrect.top=bulletx[2]
if enemyrect.colliderect(bulletrect):
print('打中飞碟')
print(len(bullets))
print(len(flyingsaucers))
print(index)
print(indexbull)
bullets.pop(indexbull)
flyingsaucers.pop(index)
print(index)
print(indexbull)
if(len(flyingsaucers)==0):
b=0#重新生成飞碟
indexbull+=1
index+=1
将代码整合起来,整合后完整代码如下:
import pygame
from pygame.locals import *
from sys import exit
import math
import random
#消灭飞碟
screen_width, screen_height = 480, 700
screen=pygame.display.set_mode((screen_width, screen_height))
background=pygame.image.load(“resources/images/background2.png”)
pygame.display.set_caption(‘雷雷的太空大战’)
pygame.init()
running=1
flyingsaucers=[]
enemy = pygame.image.load(“resources/images/flyingsaucer.png”)
b=0
bullet = pygame.image.load(“resources/images/bullet1.png”)
player = pygame.image.load(“resources/images/hero1.png”)
positionPlayer=[100,400]
positionBullet=[positionPlayer[0]+50,positionPlayer[1]+40]
bullets=[]
keys = [False, False, False, False]
position=[]
while running:
screen.blit(background,(0,0))
#screen.blit(player,positionPlayer)
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key==K_w:
keys[0]=True
elif event.key==K_a:
keys[1]=True
elif event.key==K_s:
keys[2]=True
elif event.key==K_d:
keys[3]=True
if event.type == pygame.KEYUP:
if event.key==pygame.K_w:
keys[0]=False
elif event.key==pygame.K_a:
keys[1]=False
elif event.key==pygame.K_s:
keys[2]=False
elif event.key==pygame.K_d:
keys[3]=False
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type==pygame.MOUSEBUTTONDOWN:
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(positionPlayer[1]+50),\
position[0]-(positionPlayer[0]+40))
bullets.append([angle,positionPlayer[0]+50,positionPlayer[1]+40])
if keys[0]:
positionPlayer[1]-=10
if positionPlayer[1]<=10:
positionPlayer[1]=10
elif keys[2]:
positionPlayer[1]+=10
if positionPlayer[1]>=600:
positionPlayer[1]=600
if keys[1]:
positionPlayer[0]-=10
if positionPlayer[0]<=10:
positionPlayer[0]=10
elif keys[3]:
positionPlayer[0]+=10
if positionPlayer[0]>=380:
positionPlayer[0]=380
#screen.blit(player,positionPlayer)
#旋转飞机
position = pygame.mouse.get_pos()
angle = math.atan2(position[1]-(positionPlayer[1]+50),position[0]-(positionPlayer[0]+40))
#print(angle*57.29)
playerrot = pygame.transform.rotate(player,360-angle*57.29)
screen.blit(playerrot, positionPlayer)
#射出子弹
for bulletx in bullets:
velx=math.cos(bulletx[0])*20
vely=math.sin(bulletx[0])*20
#print(velx)
#print(vely)
bulletx[1]+=velx
bulletx[2]+=vely
index=0
if bulletx[1]<0 or bulletx[1]>480 or bulletx[2]<0 or bulletx[2]>700:
bullets.pop(index)
index+=1
bulletrot = pygame.transform.rotate(bullet,360-bulletx[0]*57.29)
screen.blit(bulletrot,(bulletx[1],bulletx[2]))
bulletrect=pygame.Rect(bulletrot.get_rect())
bulletrect.left=bulletx[1]
bulletrect.top=bulletx[2]
#if enemyrect.colliderect(bulletrect):
#print('打中飞碟')
if b<5:
flyingsaucers.append([random.randint(0,400),random.randint(20,100)])
b+=1
#击落飞碟
index=0
for flyingsaucer in flyingsaucers:
if flyingsaucer[1]<610:
flyingsaucer[1]+=0.5
screen.blit(enemy,flyingsaucer)
enemyrect=pygame.Rect(enemy.get_rect())
enemyrect.left=flyingsaucer[0]
enemyrect.top=flyingsaucer[1]
indexbull=0
for bulletx in bullets:
#print(len(bullets))
bulletrot = pygame.transform.rotate(bullet,360-bulletx[0]*57.29)
bulletrect=pygame.Rect(bulletrot.get_rect())
bulletrect.left=bulletx[1]
bulletrect.top=bulletx[2]
if enemyrect.colliderect(bulletrect):
print('打中飞碟')
print(len(bullets))
print(len(flyingsaucers))
print(index)
print(indexbull)
bullets.pop(indexbull)
flyingsaucers.pop(index)
print(index)
print(indexbull)
if(len(flyingsaucers)==0):
b=0#重新生成飞碟
indexbull+=1
index+=1
pygame.display.update()
#pygame.display.flip()
运行这段代码,在画布上,移动鼠标,战机旋转。单击鼠标,子弹沿着鼠标点击的方向射出,命中飞碟后,子弹与飞碟同时消失。见下图5-13、5-14。消灭第一波飞碟后,第二波飞碟又出现在天空。见下图5-15、5-16。没有消灭的飞碟,落到地球,见图5-17。
图5-17