Python 实例 – Day2 – Aircraft Fight 飞机大战 (进行中ing)

首先感谢@1_bit 大佬的教程, 源代码请移步下面链接. 本文是自己为了理解代码所做的注释.
https://blog.csdn.net/A757291228/article/details/116305295

功能: 可以用子弹消灭敌机, 无法碰撞敌机销毁.
还有代码没有读懂, 注释持续添加中.

# 2021.5.8
# refer to https://blog.csdn.net/A757291228/article/details/116305295import pygame
# reset the path for your own file path!!!
bg = pygame.image.load(r'C:\aircraft_fight\img\bg.png')  
hero = pygame.image.load(r'C:\aircraft_fight\img\hero1.png')
enemy = pygame.image.load(r'C:\aircraft_fight\img\enemy1.png')
enemy_boom = pygame.image.load(r'C:\aircraft_fight\img\enemy1_down1.png')
bullet = pygame.image.load(r'C:\aircraft_fight\img\bullet1.png')pygame.init()   # initialize all modules of pygame
screen = pygame.display.set_mode((600, 800))   # set the size of game window
pygame.display.set_caption("This is a aircraft fight game!") # set the title of the game windowheroX = 250  # the origination of hero on X axis
heroY = 680  # the origination of hero on Y axis
stepX = 0    # the extent of movement on X axis
stepY = 0    # the extent of movement on Y axisenemy_speed = 2
enemy_objs = []
enemy_objs1 = []
enemy_objs2 = []
enemy_objs3 = []
bullets_pos =[]
bullet_speed =[]def keydown_event(event, stepX, stepY, hero_pos):  # the movement of keyboard controlbullet_pos = []if event.key == pygame.K_RIGHT:stepX =5elif event.key == pygame.K_LEFT:stepX =-5elif event.key == pygame.K_UP:stepY =-5elif event.key == pygame.K_DOWN:stepY =5elif event.key == pygame.K_SPACE:bullet_pos=[hero_pos[0],hero_pos[1]+10]return stepX, stepY, bullet_posdef enemy_show(enemy_objs,startY =-40):if len(enemy_objs) < 5:while len(enemy_objs) < 5:enemy_X = random.randint(0,500)enemy_pos = [enemy_X, startY]screen.blit(enemy,enemy_pos)enemy_objs.append(enemy_pos)else:i = 0for pos in enemy_objs:screen.blit(enemy, pos)enemy_objs[i] = [pos[0],pos[1]+enemy_speed]i = i +1return enemy_objsdef screen_border(x,y):if x<0:x = 0elif x > 500:x = 500if y<0:y = 0elif y>700:y = 700return x, ydef distance(bx,by,ex,ey):a=bx-exb=by-eyreturn math.sqrt(a*a+b*b)while True:bullet_pos_ = []heroX = heroX + stepXheroY = heroY + stepYheroX, heroY = screen_border(heroX, heroY)#blit function: draw one image onto anotherscreen.blit(bg,(0, 0))  #draw the background image onto screen windowscreen.blit(hero, (heroX,heroY))  #draw the aircraft image onto screen windowenemy_objs = enemy_show(enemy_objs)print(bullets_pos)i = 0for v in bullets_pos:bullets_pos[i] =[v[0], v[1]-10]screen.blit(bullet, (bullets_pos[i][0]+45, bullets_pos[i][1]))distance_b = [bullets_pos[i][0]+45, bullets_pos[i][1]]ei = 0for ep in enemy_objs:if distance(distance_b[0], distance_b[1],ep[0],ep[1])<60:print('\n\n\n\n\n\n\n\n\n\n\n boom')screen.blit(enemy_boom, (ep[0], ep[1]))enemy_objs[ei] = [random.randint(0,500),-50]ei = ei + 1i = i + 1for event in pygame.event.get():if event.type == pygame.QUIT:exit()if event.type == pygame.KEYDOWN:stepX, stepY, bullet_pos_= keydown_event(event, stepX, stepY, [heroX, heroY])if len(bullet_pos_)>0:bullets_pos.append(bullet_pos_)pygame.display.update()  #need update to refresh game window

Published by

风君子

独自遨游何稽首 揭天掀地慰生平

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注