each.reset()
绘制中型敌机
for each in mid_enemies:
if each.active:
each.move()
if each.hit:
screen.blit(each.image_hit, each.rect)
each.hit = False
else:
screen.blit(each.image1, each.rect)
绘制血槽
pygame.draw.line(screen, BLACK,
(each.rect.left, each.rect.top - 5),
(each.rect.right, each.rect.top - 5),
energy_remain = each.energy / enemy.MidEnemy.energy
if energy_remain > 0.2:
energy_color = GREEN
else:
energy_color = RED
pygame.draw.line(screen, energy_color,
(each.rect.left, each.rect.top - 5),
(each.rect.left + each.rect.width * energy_remain,
each.rect.top - 5),
else:
if not (delay % 3):
if e2_destroy_index == 0:
enemy2_down_sound.play()
screen.blit(each.destroy_images[e2_destroy_index], each.rect)
e2_destroy_index = (e2_destroy_index + 1) % 4
if e2_destroy_index == 0:
score += 5000
each.reset()
绘制小型敌机
for each in small_enemies:
if each.active:
each.move()
screen.blit(each.image1, each.rect)
else:
if not (delay % 3):
if e1_destroy_index == 0:
enemy1_down_sound.play()
screen.blit(each.destroy_images[e1_destroy_index], each.rect)
e1_destroy_index = (e1_destroy_index + 1) % 4
if e1_destroy_index == 0:
score += 1000
each.reset()
key_pressed = pygame.key.get_pressed()
if key_pressed[K_w] or key_pressed[K_UP]:
me.moveUp()
if key_pressed[K_s] or key_pressed[K_DOWN]:
me.moveDown()
if key_pressed[K_a] or key_pressed[K_LEFT]:
me.moveLeft()
if key_pressed[K_d] or key_pressed[K_RIGHT]:
me.moveRight()
检测我方飞机是否被撞
enemies_down = pygame.sprite.spritecollide(me, enemies, False, pygame.sprite.collide_mask)
if enemies_down and not me.invincible:
me.active = False
for e in enemies_down:
e.active = False
绘制我方飞机
if me.active:
if switch_image:
screen.blit(me.image1, me.rect)
else:
screen.blit(me.image2, me.rect)
else:
me_down_sound.play()
if not (delay % 3):
screen.blit(each.destroy_images[me_destroy_index], each.rect)
me_destroy_index = (me_destroy_index + 1) % 4
剩余生命数量
if me_destroy_index == 0:
life_num -= 1
me.reset()
pygame.time.set_timer(INVINCEBLE_TIME, 3 * 1000)
绘制剩余炸弹数量
bomb_text = bomb_font.render(“x %d” % bomb_num, True, WHITE)
text_rect = bomb_text.get_rect()
screen.blit(bomb_image, (10, height - 10 - bomb_rect.height))
screen.blit(bomb_text, (20 + bomb_rect.width, height - 5 - text_rect.height))
if life_num:
for i in range(life_num):
screen.blit(life_image,
(width - 10 - (i + 1) * life_rect.width,
height - 10 - life_rect.height))
score_text = score_font.render(str(“Score: %s” % score), True, WHITE)
screen.blit(score_text, (10, 5))
elif life_num == 0:
pygame.mixer.music.stop()
pygame.mixer.stop()
停止发放补给
pygame.time.set_timer(SUPPLY_TIME, 0)
if not recorded:
recorded = True
读取历史最高分
with open(“plane/record/record.txt”, “r”) as f:
record_score = int(f.read())
if score > record_score:
with open(“plane/record/record.txt”, “w”) as f:
f.write(str(score))
绘制结束画面
record_score_text = score_font.render(“Best: %d” % record_score, True, WHITE)
screen.blit(record_score_text, (50, 50))
gameover_text1 = gameover_font.render("Your Score: ", True, WHITE)
gameover_text1_rect = gameover_text1.get_rect()
gameover_text1_rect.left, gameover_text1_rect.top = \
(width - gameover_text1_rect.width) // 2, height // 2
screen.blit(gameover_text1, gameover_text1_rect)
gameover_text2 = gameover_font.render(str(score), True, WHITE)
gameover_text2_rect = gameover_text2.get_rect()
gameover_text2_rect.left, gameover_text2_rect.top = \
(width - gameover_text2_rect.width) // 2, \
gameover_text1_rect.bottom + 10
screen.blit(gameover_text2, gameover_text2_rect)
again_rect.left, again_rect.top = \
(width - again_rect.width) // 2, \
gameover_text2_rect.bottom + 50
screen.blit(again_image, again_rect)
gameover_rect.left, gameover_rect.top = \
(width - again_rect.width) // 2, \
again_rect.bottom + 10
screen.blit(gameover_image, gameover_rect)
检测用户的鼠标操作
如果用户按下鼠标左键
if pygame.mouse.get_pressed()[0]:
pos = pygame.mouse.get_pos()
if again_rect.left < pos[0] < again_rect.right and \
again_rect.top < pos[1] < again_rect.bottom:
main()
elif gameover_rect.left < pos[0] < gameover_rect.right and \
gameover_rect.top < pos[1] < gameover_rect.bottom:
pygame.quit()
sys.exit()
screen.blit(paused_image, paused_rect)
切换图片
if not (delay % 5):
switch_image = not switch_image
delay -= 1
if not delay:
delay = 100
pygame.display.flip()
clock.tick(60)
if name == “main”:
try:
main()
except SystemExit:
pass
except:
traceback.print_exc()
pygame.quit()
input()
4、子弹模块
import pygame
class Bullet1(pygame.sprite.Sprite): # 定义一个子弹类Bullet1继承pygame模块的sprite,Sprite类
def init(self, position): # 定义构造函数,并继承sprite.Sprite类中的构造函数
pygame.sprite.Sprite.init(self)
定义自己的属型
self.image = pygame.image.load(“plane/images/bullet1.png”).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 12
self.active = True
self.mask = pygame.mask.from_surface(self.image)
定义子弹的移动
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
定义第二个子弹类,并继承pygame.sprite.Sprite类
class Bullet2(pygame.sprite.Sprite):
def init(self, position): # 定义构造函数
pygame.sprite.Sprite.init(self) # 并继承sprite.Sprite类中的构造函数
实例化第二个子弹属型
self.image = pygame.image.load(“plane/images/bullet2.png”).convert_alpha()
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = position
self.speed = 14
self.active = True
self.mask = pygame.mask.from_surface(self.image)
#同上
def move(self):
self.rect.top -= self.speed
if self.rect.top < 0:
self.active = False
#同上
def reset(self, position):
self.rect.left, self.rect.top = position
self.active = True
5、敌机模块
import pygame
from random import * # 导入random库中所有模块
定义SmallEnemy也就是小敌机类,并继承pygame.sprite.Sprite类
class SmallEnemy(pygame.sprite.Sprite):
def init(self, bg_size): # 定义构造函数,传入bg_size参数
pygame.sprite.Sprite.init(self) #继承pygame.sprite.Sprite的构造函数
实例化属型,也就是赋值
self.image1 = pygame.image.load(“plane/images/enemy1.png”).convert_alpha()
self.destroy_images = []
敌机爆炸后的图片
self.destroy_images.extend([
pygame.image.load(“plane/images/enemy1_down1.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy1_down2.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy1_down3.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy1_down4.png”).convert_alpha(),
])
self.rect = self.image1.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.image1)
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.image1 = pygame.image.load(“plane/images/enemy2.png”).convert_alpha()
self.image_hit = pygame.image.load(“plane/images/enemy2_hit.png”).convert_alpha()
self.destroy_images = []
self.destroy_images.extend([
pygame.image.load(“plane/images/enemy2_down1.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy2_down2.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy2_down3.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy2_down4.png”).convert_alpha(),
])
self.rect = self.image1.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 1
self.active = True
self.hit = False
self.energy = MidEnemy.energy
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-5 * self.height, -self.height)
self.mask = pygame.mask.from_surface(self.image1)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
def reset(self):
self.active = True
self.energy = MidEnemy.energy
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-8 * self.height, -self.height)
同上
class BigEnemy(pygame.sprite.Sprite):
energy = 20
def init(self, bg_size):
pygame.sprite.Sprite.init(self)
self.image1 = pygame.image.load(“plane/images/enemy3_n1.png”).convert_alpha()
self.image2 = pygame.image.load(“plane/images/enemy3_n2.png”).convert_alpha()
self.image_hit = pygame.image.load(“plane/images/enemy3_hit.png”).convert_alpha()
self.destroy_images = []
self.destroy_images.extend([
pygame.image.load(“plane/images/enemy3_down1.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy3_down2.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy3_down3.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy3_down4.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy3_down5.png”).convert_alpha(),
pygame.image.load(“plane/images/enemy3_down6.png”).convert_alpha(),
])
self.rect = self.image1.get_rect()
self.width, self.height = bg_size[0], bg_size[1]
self.speed = 1
self.active = True
self.hit = False
self.energy = BigEnemy.energy
self.rect.left, self.rect.top = \
randint(0, self.width - self.rect.width), \
randint(-10 * self.height, -5 * self.height)
self.mask = pygame.mask.from_surface(self.image1)
def move(self):
if self.rect.top < self.height:
self.rect.top += self.speed
else:
self.reset()
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)
如果你也是看准了Python,想自学Python,在这里为大家准备了丰厚的免费学习大礼包,带大家一起学习,给大家剖析Python兼职、就业行情前景的这些事儿。
一、Python所有方向的学习路线
Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。
二、学习软件
工欲善其必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。
三、全套PDF电子书
书籍的好处就在于权威和体系健全,刚开始学习的时候你可以只看视频或者听某个人讲课,但等你学完之后,你觉得你掌握了,这时候建议还是得去看一下书籍,看权威技术书籍也是每个程序员必经之路。
四、入门学习视频
我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
成为一个Python程序员专家或许需要花费数年时间,但是打下坚实的基础只要几周就可以,如果你按照我提供的学习路线以及资料有意识地去实践,你就有很大可能成功!
最后祝你好运!!!
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!文章来源:https://www.toymoban.com/news/detail-852478.html
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算文章来源地址https://www.toymoban.com/news/detail-852478.html
d0a6a2.png)
四、实战案例
光学理论是没用的,要学会跟着一起敲,要动手实操,才能将自己的所学运用到实际当中去,这时候可以搞点实战案例来学习。
五、面试资料
我们学习Python必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有阿里大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。
成为一个Python程序员专家或许需要花费数年时间,但是打下坚实的基础只要几周就可以,如果你按照我提供的学习路线以及资料有意识地去实践,你就有很大可能成功!
最后祝你好运!!!
一个人可以走的很快,但一群人才能走的更远。如果你从事以下工作或对以下感兴趣,欢迎戳这里加入程序员的圈子,让我们一起学习成长!
AI人工智能、Android移动开发、AIGC大模型、C C#、Go语言、Java、Linux运维、云计算、MySQL、PMP、网络安全、Python爬虫、UE5、UI设计、Unity3D、Web前端开发、产品经理、车载开发、大数据、鸿蒙、计算机网络、嵌入式物联网、软件测试、数据结构与算法、音视频开发、Flutter、IOS开发、PHP开发、.NET、安卓逆向、云计算
到了这里,关于20个经典面试问题Python面向对象实战--飞机大战(1),Python中高级面试必知必会的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!