pygame外星人入侵,2024最新Python大厂面试真题大全-程序员宅基地

技术标签: 2024年程序员学习  面试  python  pygame  

def cheak_events(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets):

“”“响应按键和鼠标事件”“”

for event in pygame.event.get():

if event.type == pygame.QUIT:

sys.exit()

elif event.type == pygame.KEYDOWN:

cheak_keydowen_events(event, ai_settings, screen, ship, bullets)

elif event.type == pygame.KEYUP:

cheak_keyup_events(event, ship)

elif event.type == pygame.MOUSEBUTTONDOWN:

mouse_x, mouse_y = pygame.mouse.get_pos()

cheak_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y)

def cheak_play_button(ai_settings, screen, stats, sb, play_button, ship, aliens, bullets, mouse_x, mouse_y):

“”“在玩家单击play按钮时开始新游戏”“”

bullets_clicked = play_button.rect.collidepoint(mouse_x, mouse_y)

if bullets_clicked and not stats.game_active:

重置游戏设置

ai_settings.initialize_dynamic_settings()

隐藏光标

pygame.mouse.set_visible(False)

重置游戏统计信息

stats.reset_stats()

stats.game_active = True

重置记分牌对象

sb.prep_score()

sb.prep_high_score()

sb.prep_level()

sb.prep_ships()

清空外星人列表和子弹列表

aliens.empty()

bullets.empty()

创建一群新的外星人,并让飞船居中

create_fleet(ai_settings, screen, ship, aliens)

ship.center_ship()

def update_screen(ai_settings, screen, stats, sb, ship, aliens, bullets, play_button):

“”“更新屏幕上的图像,并切换到新屏幕”“”

每次循环都重绘屏幕

screen.fill(ai_settings.bg_color)

在飞船和外星人后面重绘所有子弹

for bullet in bullets.sprites():

bullet.draw_bullet()

ship.blitme()

aliens.draw(screen)

显示得分

sb.show_score()

如果游戏处于非活动状态,就绘制play按钮

if not stats.game_active:

play_button.draw_button()

让最近绘制的屏幕可见

pygame.display.flip()

def update_bullets(ai_settings, screen, stats, sb, ship, aliens, bullets):

“”“更新子弹的位置,并删除已消失子弹”“”

更新子弹位置

bullets.update()

删除已消失的子弹

for bullet in bullets.copy():

if bullet.rect.bottom <= 0:

bullets.remove(bullet)

cheak_bullets_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets)

def cheak_bullets_alien_collisions(ai_settings, screen, stats, sb, ship, aliens, bullets):

“”“响应子弹和外星人碰撞”“”

删除发生碰撞的子弹和外星人

collections = pygame.sprite.groupcollide(bullets, aliens, True, True)

if collections:

for aliens in collections.values():

stats.score += ai_settings.alien_points * len(aliens)

sb.prep_score()

cheak_high_score(stats, sb)

if len(aliens) == 0:

如果整群外星人都被消灭,就提高一个等级

删除现有的子弹,加快游戏节奏,并创建一群新的外星人

bullets.empty()

ai_settings.increase_speed()

提高等级

stats.level += 1

sb.prep_level()

create_fleet(ai_settings, screen, ship, aliens)

def get_number_alien_x(ai_settings, alien_width):

“”“计算每行可容纳多少外星人”“”

available_space_x = ai_settings.screen_width - 2 * alien_width

number_alien_x = int(available_space_x / (2 * alien_width))

return number_alien_x

def get_number_rows(ai_settings, ship_height, alien_height):

“”“计算屏幕可容纳多少行机器人”“”

available_space_y = (ai_settings.screen_height - (3 * alien_height) - ship_height)

number_rows = int(available_space_y / (2 * alien_height))

return number_rows

def create_alien(ai_settings, screen, aliens, alien_number, row_number):

创建第一行外星人并将其加入当前行

alien = Alien(ai_settings, screen)

alien_width = alien.rect.width

alien.x = alien_width + 2 * alien_width * alien_number

alien.rect.x = alien.x

alien.rect.y = alien.rect.height + 2 * alien.rect.height * row_number

aliens.add(alien)

def create_fleet(ai_settings, screen, ship, aliens):

“”“创建外星人群”“”

创建一个外星人,并计算一行可容纳多少个外星人

外星人间距为外星人宽度

alien = Alien(ai_settings, screen)

number_alien_x = get_number_alien_x(ai_settings, alien.rect.width)

number_rows = get_number_rows(ai_settings, ship.rect.height, alien.rect.height)

创建外星人群

for row_number in range(number_rows):

for alien_number in range(number_alien_x):

create_alien(ai_settings, screen, aliens, alien_number, row_number)

def cheak_fleet_edges(ai_settings, aliens):

“”“有外星人到达屏幕边缘时采取相应的措施”“”

for alien in aliens.sprites():

if alien.cheak_edgs():

change_fleet_direction(ai_settings, aliens)

break

def change_fleet_direction(ai_settings, aliens):

“”“将整群外星人下移,并改变它们的位置”“”

for alien in aliens.sprites():

alien.rect.y += ai_settings.fleet_drop_speed

ai_settings.fleet_direction *= -1

def ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets):

“”“响应被外星人撞到的飞船”“”

if stats.ships_left > 0:

将ships_left减1

stats.ships_left -= 1

更新记分牌

sb.prep_ships()

清空外星人列表和子弹列表

aliens.empty()

bullets.empty()

创建一群新的外星人,并将飞船放在屏幕底端中央

create_fleet(ai_settings, screen, ship, aliens)

ship.center_ship()

暂停

sleep(0.5)

else:

stats.game_active = False

pygame.mouse.set_visible(True)

def cheak_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets):

“”“检查是否有外星人到达了屏幕底端”“”

screen_rect = screen.get_rect()

for alien in aliens.sprites():

if alien.rect.bottom >= screen_rect.bottom:

像飞船被撞到一样进行处理

ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)

break

def update_aliens(ai_settings, screen, stats, sb, ship, aliens, bullets):

“”“检查是否有外星人位于屏幕边缘,并更新整群外星人的位置”“”

cheak_fleet_edges(ai_settings, aliens)

aliens.update()

检查外星人和飞船之间的碰撞

if pygame.sprite.spritecollideany(ship, aliens):

ship_hit(ai_settings, screen, stats, sb, ship, aliens, bullets)

检查是否有外星人到达屏幕底端

cheak_aliens_bottom(ai_settings, screen, stats, sb, ship, aliens, bullets)

def cheak_high_score(stats, sb):

“”“检查是否诞生了新的最高得分”“”

if stats.score > stats.high_score:

stats.high_score = stats.score

sb.prep_high_score()

game_stats.py

跟踪统计游戏信息类

class GameSrats():

“”“跟踪游戏的统计信息”“”

def init(self, ai_settings):

“”“初始化统计信息”“”

self.ai_settings = ai_settings

self.reset_stats()

游戏刚启动时处于活动状态

self.game_active = False

在任何情况下都不应重置最高得分

self.high_score = 0

def reset_stats(self):

“”“初始化在游戏运行期间可能变化的统计信息”“”

self.ships_left = self.ai_settings.ship_limit

self.score = 0

self.level = 1

scoreboard.py

创建scoerboard类,用来显示当前得分,最高得分,玩家等级,余下的飞船数。

import pygame.font

from pygame.sprite import Group

from ship import Ship

class Scoreboard():

“”“显示得分信息的类”“”

def init(self, ai_settings, screen, stats):

“”“初始化显示得分涉及的属性”“”

self.screen = screen

self.screen_rect = screen.get_rect()

self.ai_settings = ai_settings

self.stats = stats

显示得分信息时使用的字体设置

self.text_color = (30, 30, 30)

self.font = pygame.font.SysFont(None, 48)

准备包含得分的初始图像

self.prep_score()

self.prep_high_score()

self.prep_level()

self.prep_ships()

def prep_ships(self):

“”“显示还余下多少搜飞船”“”

self.ships = Group()

for ship_number in range(self.stats.ships_left):

ship = Ship(self.ai_settings, self.screen)

ship.rect.x = 10 + ship_number * ship.rect.width

ship.rect.y = 10

self.ships.add(ship)

def prep_level(self):

“”“将等级转换为渲染的图像”“”

self.level_image = self.font.render(str(self.stats.level), True, self.text_color, self.ai_settings.bg_color)

将等级放在得分下方

self.level_rect = self.level_image.get_rect()

self.level_rect.right = self.score_rect.right

self.level_rect.top = self.score_rect.bottom + 10

def prep_high_score(self):

“”“将最高得分转换为渲染的图像”“”

high_score = int(round(self.stats.high_score, -1))

high_score_str = str(“{:,}”.format(high_score))

self.high_score_image = self.font.render(high_score_str, True, self.text_color, self.ai_settings.bg_color)

将最高得分放在屏幕顶部中央

self.high_score_rect = self.high_score_image.get_rect()

self.high_score_rect.centerx = self.screen_rect.centerx

self.high_score_rect.top = self.screen_rect.top

def prep_score(self):

“”“将得分转换为一幅渲染的图像”“”

rounded_score = int(round(self.stats.score, -1))

score_str = “{:,}”.format(rounded_score)

self.score_image = self.font.render(score_str, True, self.text_color, self.ai_settings.bg_color)

将得分放在屏幕右上角

self.score_rect = self.score_image.get_rect()

self.score_rect.right = self.screen_rect.right - 20

self.score_rect.top = 20

def show_score(self):

“”“在屏幕上显示得分”“”

self.screen.blit(self.score_image, self.score_rect)

self.screen.blit(self.high_score_image, self.high_score_rect)

self.screen.blit(self.level_image, self.level_rect)

绘制飞船

self.ships.draw(self.screen)

settings.py

存储游戏所有设置类

class Settings():

“”“存储外星人入侵所有设置的类”“”

def init(self):

“”“初始化游戏静态设置”“”

屏幕设置

self.screen_width = 1200

self.screen_height = 800

self.bg_color = (230, 230, 230)

飞船的速度设置

self.ship_speed_factor = 1.5

self.ship_limit = 3

子弹设置

self.bullet_speed_factor = 10

self.bullet_width = 10

self.bullet_height = 15

self.bullet_color = 60, 60, 60

self.bullets_allowed = 3

外星人设置

self.alien_speed_factor = 1

self.fleet_drop_speed = 5

fleet_direction为1表示向右移,为-1表示向左移

self.fleet_direction = 1

加快游戏节奏的速度

self.speedup_scale = 1.1

外星人点数的提高速度

self.score_scale = 1.5

self.initialize_dynamic_settings()

def initialize_dynamic_settings(self):

“”“初始化随游戏进行而变化的位置”“”

self.ship_speed_factor = 1.5

self.bullet_speed_factor = 3

self.alien_speed_factor = 1

fleet_direction为1表示向右,为-1表示向左

self.fleet_direction = 1

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python爬虫全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:python)
img

img.cn/img_convert/8c4513c1a906b72cbf93031e6781512b.png)

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python爬虫全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频

如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注:python)
[外链图片转存中…(img-C8VtJtFc-1710973899686)]

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/weixin_58132019/article/details/136894486

智能推荐

海康威视网络摄像头开发流程(五)------- 直播页面测试_ezuikit 测试的url-程序员宅基地

文章浏览阅读3.8k次。1、将下载好的萤石js插件,添加到SoringBoot项目中。位置可参考下图所示。(容易出错的地方,在将js插件在html页面引入时,发生路径错误的问题)所以如果对页面中引入js的路径不清楚,可参考下图所示存放路径。2、将ezuikit.js引入到demo-live.html中。(可直接将如下代码复制到你创建的html页面中)<!DOCTYPE html><html lan..._ezuikit 测试的url

如何确定组态王与多动能RTU的通信方式_组态王ua-程序员宅基地

文章浏览阅读322次。第二步,在弹出的对话框选择,设备驱动—>PLC—>莫迪康—>ModbusRTU—>COM,根据配置软件选择的协议选期期,这里以此为例,然后点击“下一步”。第四步,把使用虚拟串口打勾(GPRS设备),根据需要选择要生成虚拟口,这里以选择KVCOM1为例,然后点击“下一步”设备ID即Modbus地址(1-255) 使用DTU时,为下485接口上的设备地址。第六步,Modbus的从机地址,与配置软件相同,这里以1为例,点击“下一步“第五步,Modbus的从机地址,与配置软件相同,这里以1为例,点击“下一步“_组态王ua

npm超详细安装(包括配置环境变量)!!!npm安装教程(node.js安装教程)_npm安装配置-程序员宅基地

文章浏览阅读9.4k次,点赞22次,收藏19次。安装npm相当于安装node.js,Node.js已自带npm,安装Node.js时会一起安装,npm的作用就是对Node.js依赖的包进行管理,也可以理解为用来安装/卸载Node.js需要装的东西_npm安装配置

火车头采集器AI伪原创【php源码】-程序员宅基地

文章浏览阅读748次,点赞21次,收藏26次。大家好,小编来为大家解答以下问题,python基础训练100题,python入门100例题,现在让我们一起来看看吧!宝子们还在新手村练级的时候,不单要吸入基础知识,夯实自己的理论基础,还要去实际操作练练手啊!由于文章篇幅限制,不可能将100道题全部呈现在此除了这些,下面还有我整理好的基础入门学习资料,视频和讲解文案都很齐全,用来入门绝对靠谱,需要的自提。保证100%免费这不,贴心的我爆肝给大家整理了这份今天给大家分享100道Python练习题。大家一定要给我三连啊~

Linux Ubuntu 安装 Sublime Text (无法使用 wget 命令,使用安装包下载)_ubuntu 安装sumlime text打不开-程序员宅基地

文章浏览阅读1k次。 为了在 Linux ( Ubuntu) 上安装sublime,一般大家都会选择常见的教程或是 sublime 官网教程,然而在国内这种方法可能失效。为此,需要用安装包安装。以下就是使用官网安装包安装的教程。打开 sublime 官网后,点击右上角 download, 或是直接访问点击打开链接,即可看到各个平台上的安装包。选择 Linux 64 位版并下载。下载后,打开终端,进入安装..._ubuntu 安装sumlime text打不开

CrossOver for Mac 2024无需安装 Windows 即可以在 Mac 上运行游戏 Mac运行exe程序和游戏 CrossOver虚拟机 crossover运行免安装游戏包-程序员宅基地

文章浏览阅读563次,点赞13次,收藏6次。CrossOver24是一款类虚拟机软件,专为macOS和Linux用户设计。它的核心技术是Wine,这是一种在Linux和macOS等非Windows操作系统上运行Windows应用程序的开源软件。通过CrossOver24,用户可以在不购买Windows授权或使用传统虚拟机的情况下,直接在Mac或Linux系统上运行Windows软件和游戏。该软件还提供了丰富的功能,如自动配置、无缝集成和实时传输等,以实现高效的跨平台操作体验。

随便推点

一个用聊天的方式让ChatGPT写的线程安全的环形List_为什么gpt一写list就卡-程序员宅基地

文章浏览阅读1.7k次。一个用聊天的方式让ChatGPT帮我写的线程安全的环形List_为什么gpt一写list就卡

Tomcat自带的设置编码Filter-程序员宅基地

文章浏览阅读336次。我们在前面的文章里曾写过Web应用中乱码产生的原因和处理方式,旧文回顾:深度揭秘乱码问题背后的原因及解决方式其中我们提到可以通过Filter的方式来设置请求和响应的encoding,来解..._filterconfig selectencoding

javascript中encodeURI和decodeURI方法使用介绍_js encodeur decodeurl-程序员宅基地

文章浏览阅读651次。转自:http://www.jb51.net/article/36480.htmencodeURI和decodeURI是成对来使用的,因为浏览器的地址栏有中文字符的话,可以会出现不可预期的错误,所以可以encodeURI把非英文字符转化为英文编码,decodeURI可以用来把字符还原回来_js encodeur decodeurl

Android开发——打包apk遇到The destination folder does not exist or is not writeable-程序员宅基地

文章浏览阅读1.9w次,点赞6次,收藏3次。前言在日常的Android开发当中,我们肯定要打包apk。但是今天我打包的时候遇到一个很奇怪的问题Android The destination folder does not exist or is not writeable,大意是目标文件夹不存在或不可写。出现问题的原因以及解决办法上面有说报错的中文大意是:目标文件夹不存在或不可写。其实问题就在我们的打包界面当中图中标红的Desti..._the destination folder does not exist or is not writeable

Eclipse配置高大上环境-程序员宅基地

文章浏览阅读94次。一、配置代码编辑区的样式 <1>打开Eclipse,Help —> Install NewSoftware,界面如下: <2>点击add...,按下图所示操作: name:随意填写,Location:http://eclipse-color-th..._ecplise高大上设置

Linux安装MySQL-5.6.24-1.linux_glibc2.5.x86_64.rpm-bundle.tar_linux mysql 安装 mysql-5.6.24-1.linux_glibc2.5.x86_6-程序员宅基地

文章浏览阅读2.8k次。一,下载mysql:http://dev.mysql.com/downloads/mysql/; 打开页面之后,在Select Platform:下选择linux Generic,如果没有出现Linux的选项,请换一个浏览器试试。我用的谷歌版本不可以,换一个别的浏览器就行了,如果还是不行,需要换一个翻墙的浏览器。 二,下载完后解压缩并放到安装文件夹下: 1、MySQL-client-5.6.2_linux mysql 安装 mysql-5.6.24-1.linux_glibc2.5.x86_64.rpm-bundle