PYTHON飞机大战(第六天)

2023-11-09

OK,今天成功做出了多个外星人,代码来了

import sys
import pygame
from bullet import Bullet
from alien import Alien


def check_keydown_events(event, ship, ai_settings, screen, bullets):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = True
        print(event.key)
    elif event.key == pygame.K_LEFT:
        ship.moving_left = True
        print(event.key)
    elif event.key == pygame.K_UP:
        ship.moving_up = True
        print(event.key)
    elif event.key == pygame.K_DOWN:
        ship.moving_down = True
        print(event.key)
    elif event.key == pygame.K_SPACE:
        fire_bullet(ai_settings, screen, ship, bullets)
    elif event.key == pygame.K_ESCAPE:
        sys.exit()


# 上面的函数可以调用下面的函数!!!!

def fire_bullet(ai_settings, screen, ship, bullets):
    if len(bullets) < ai_settings.bullet_allow:
        new_bullet = Bullet(ai_settings, screen, ship)
        bullets.add(new_bullet)


def check_keyup_events(event, ship):
    if event.key == pygame.K_RIGHT:
        ship.moving_right = False
    elif event.key == pygame.K_LEFT:
        ship.moving_left = False
    elif event.key == pygame.K_UP:
        ship.moving_up = False
    elif event.key == pygame.K_DOWN:
        ship.moving_down = False


def check_events(ship, ai_settings, screen, bullets):
    # 控制飞船移动
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            sys.exit()
        elif event.type == pygame.KEYDOWN:
            check_keydown_events(event, ship, ai_settings, screen, bullets)
            # Event就是for循环里的event
        elif event.type == pygame.KEYUP:
            check_keyup_events(event, ship)


def update_bullets(aliens,bullets):
    bullets.update()
    for bullet in bullets.copy():
        if bullet.rect.bottom <= 0:
            bullets.remove(bullet)
            print(len(bullets))
        #检测是否有子弹击中外星人
        #如果集中,就删除对应的子弹和外星人
        #这行代码遍历编组子弹和编组外星人判断是否子弹和外星人的rect是否重叠,重叠的话返回字典的键值对
    collisions = pygame.sprite.groupcollide(bullets,aliens,True,True)


def update_screen(ai_settings, screen, ship, bullets, aliens):
    # 更新屏幕,将颜色和飞船印上去,以及子弹
    screen.fill(ai_settings.bg_color)
    for bullet in bullets.sprites():
        bullet.draw_buller()

    ship.blitme()
    ship.update()
    aliens.draw(screen)
    pygame.display.flip()

def update_aliens(aliens):
    aliens_moving = aliens.update()


def get_number_aliens_x(ai_settings, alien_width):
    available_space_x = ai_settings.screen_width - 2 * alien_width
    number_aliens_x = int(available_space_x / (2 * alien_width))

    return number_aliens_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 creat_fleet(ai_settings, screen, aliens):
    alien = Alien(ai_settings, screen)
    number_aliens_x = get_number_aliens_x(ai_settings, alien.rect.width)
    number_rows = get_number_rows(ai_settings,alien.rect.height,alien.rect.height)
    for row_number in range(number_rows):
        for alien_number in range(number_aliens_x):
        # 这一步是创建外星人的X轴和Y轴,调用create_alien模块,同时用两个for循环
        # 这一步的意义在于,建立每个外星人都是独立的个体
            create_alien(ai_settings, ai_settings.screen, aliens, alien_number,row_number)

#让外星人碰到边界然后进行回弹
def check_fleet_edges(ai_settings,aliens):
    for alien in aliens.sprites():
        if alien.check_edges():
            change_fleet_dirction(ai_settings,aliens)
            break
def change_fleet_dirction(ai_settings,aliens):
    for alien in aliens.sprites():
        alien.rect.y += ai_settings.fleet_drop
    ai_settings.fleet_direction *= -1

def update_aliens(ai_settings,aliens):
    check_fleet_edges(ai_settings,aliens)
    aliens.update()

并且也成功发射导弹进行对外星人的射击!但是,说实话今天做的像是抄作业,今天打算不通过写好的代码自己进行尝试自己进行重做一次。今天我尝试做一个矩形,然后让其进行左右上下移动,碰到边界之后返回。今天代码弄了一下午也不知道为什么不能让其进行矩形移动,代码如下:

import pygame


class moving():
    def __init__(self):
        pygame.init()
        self.screen = pygame.display.set_mode((500, 300))
        self.bg_color = self.screen.fill((65, 222, 65))
        self.font = pygame.font.Font(None, 36)
        self.text = self.font.render('Hello There', 1, (10, 10, 10))
        self.rect_width = 50
        self.rect_height = 50
        self.rect_moving = 10
        self.rect1 = pygame.Surface((60, 60), pygame.SRCALPHA)

    def text1(self, screen, text):
        textpos = text.get_rect()
        textpos.centerx = screen.get_rect().centerx
        textpos.bottom = screen.get_rect().bottom / 2
        screen.blit(text, textpos)

    def moving_rect(self,ai_setting):
        pygame.draw.rect(self.rect1, (255, 255, 255), [0, 0, self.rect_width, self.rect_height])
        x = 0
        x += self.rect_moving
        self.screen.blit(self.rect1, [x, 0])


def run():
    ai_setting = moving()
    ai_setting.text1(ai_setting.screen, ai_setting.text)
    ai_setting.moving_rect(ai_setting)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                return

        pygame.display.update()
        pygame.display.flip()


run()

目前只能移动一次,也就是X+=self.rect_moving只能移动一次,也就是在X轴只能移动10个X,但是我想要做的是让其不断移动,直到碰到边界然后反弹。今天熬夜继续试试。。。加油

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PYTHON飞机大战(第六天) 的相关文章

随机推荐

  • WebRTC音视频通话-iOS端调用ossrs直播拉流

    WebRTC音视频通话 iOS端调用ossrs直播拉流 之前实现iOS端调用ossrs服务 文中提到了推流 没有写拉流流程 所以会用到文中的WebRTCClient 请详细查看 https blog csdn net gloryFlow a
  • openstack排错

    创建云主机失败 neutron agent list 计算节点服务没起来 实际是起来的 观察时间有问题 同步时间 root compute ntpdate controller 再看控制节点 起来了 也能创建云主机了
  • 配置HUE 遇到Error in sasl_client_start (-4) SASL(-4): no mechanism available: No worthy mechs found

    错误 Error in sasl client start 4 SASL 4 no mechanism available No worthy mechs found 解决方法 yum install cyrus sasl plain cy
  • c语言中typedef和define的区别

    define是宏替换 编译后代码中不存在 define u8 uint 8 意味着程序中所有u8被替换为uint 8 在最终代码中根本不会存在u8这个符号 只有uint 8这个符号 typedef 是用来定义一种类型的新别名的 它不同于宏
  • Linux搭建服务器之六:安装kafka

    windows安装kafka 请点击 https blog csdn net weixin 44039105 article details 129240685 spm 1001 2014 3001 5502 安装jdk https blo
  • 关于STM32串口烧录后程序正常运行,但是掉电或复位后程序不正常运行的可能解决方法。

    关于STM32串口烧录后程序正常运行 但是掉电或复位后程序不正常运行的可能解决方法 BOOT0 BOOT1 MODE 0 X FLASH 1 1 SRAM 1 0 ISP BOOT0置1 BOOT1置0 开启串口烧录模式 用FlyMcu烧录
  • 回首2021,展望2022

    律回春晖渐 万象始更新 2022年的曙光即将照射到这片平畴沃野 在这辞旧迎新的时刻 观成科技祝大家 新年快乐 吉祥如意 2021年 对于网络安全行业来说又是不平凡的一年 疫情反复 网络安全事件频发 网络安全的攻防对抗烈度和重视程度都达到了一
  • 区块链入门二:区块不可篡改

    区块链入门一 什么是区块链 在上一篇中 简单介绍了什么是区块链 这一篇主要介绍区块链的不可篡改的特性 首先我们来了解下哈希 Hash 算法 这是百度的描述 简单来说就是一种不可逆的摘要算法 哈希算法的目的就是为了验证原始数据是否被篡改 常用
  • 【满分】【华为OD机试真题2023 JAVA&JS】字母组合

    华为OD机试真题 2023年度机试题库全覆盖 刷题指南点这里 字母组合 知识点回溯 时间限制 1s 空间限制 256MB 限定语言 不限 题目描述 每个数字对应多个字母 对应关系如下 0 a b c 1 d e f 2 g h i 3 j
  • vue面试题汇总

    HTML篇 CSS篇 JS篇 TypeScript篇 React篇 微信小程序篇 前端面试题汇总大全 含答案超详细 HTML JS CSS汇总篇 持续更新 前端面试题汇总大全二 含答案超详细 Vue TypeScript React 微信小
  • MySQL索引底层:B+树详解

    前言 当我们发现SQL执行很慢的时候 自然而然想到的就是加索引 对于范围查询 索引的底层结构就是B 树 今天我们一起来学习一下B 树哈 Mysql有什么索引 索引模型是什么 树简介 树种类 B 树 B 树简介 B 树插入 B 树查找 B 树
  • python3 清除过滤emoji表情

    前段时间发现了一个 人工智能学习网站 通俗易懂 风趣幽默 分享一下给大家 学习链接 python3 清除过滤emoji表情 第一种方法 使用emoji处理库 安装emoji 使用 import emoji test str 服务周到 性价比
  • 汇编语言(王爽第三版)实验二

    实验二 题目预览 使用Debug 将下面的程序段写入程序 逐条进行 根据指令执行后的实际运行情况填空 仔细观察图3 19中的实验过程 然后分析 为什么2000 0 2000 F中的内容会发生改变 1 使用Debug 将下面的程序段写入程序
  • 目标跟踪整理(1)之MOSSE

    之前读过一遍MOSSE了 读完还是有一种懵懵的感觉 最近还需要入基于相关滤波的目标跟踪的坑 所以又屁颠屁颠跑来深入理解一下 毕竟是相关滤波的始祖啊 Visual Object Tracking using Adaptive Correlat
  • SpringMVC学习(一)——快速搭建SpringMVC开发环境(非注解方式)

    目录 1 开发环境准备 1 1 首先电脑需要安装JDK环境 略 1 2 准备一个以供开发的tomcat 1 3 准备Maven工具 1 4 准备IDE编译器 1 5 准备一个本地的数据库 2 搭建SpringMVC开发环境 2 1 创建we
  • Golang - restful-url的接口注册处理

    一 注册 根请求转到rootHandle 在rootHandle中为不同的url查找对应的处理接口并执行 1 tars业务端配置restful url与处理函数 指定url与对应的处理函数 type TarsHttpMux struct h
  • ubuntu20下安装配置x11vnc的步骤——多次亲测可用

    在Ubuntu 20 04中安装和配置x11vnc的步骤如下 打开终端并输入以下命令以安装x11vnc sudo apt get install x11vnc 安装完成后 输入以下命令以生成密码文件 sudo x11vnc storepas
  • 安徽大学研究生院计算机与科学,安徽大学研究生导师简介院系所计算机科学与技术学院姓名赵.doc...

    安徽大学研究生导师简介院系所计算机科学与技术学院姓名赵 安徽大学研究生导师简介 院 系 所 计算机科学与技术学院 姓名 赵姝 性别 女 出生年月 1979 10 导师类别 硕士生导师 技术职称 副教授 联系方式 zhaoshuzs2002
  • 数据预处理之重复值

    目录 0 前言 1 重复值的识别 1 1 DataFrame识别重复值 duplicated 1 2 Serier识别重复值 is unique 2 统计重复行的数量 duplicated sum 3 重复值的处理 0 前言 在实际数据采集
  • PYTHON飞机大战(第六天)

    OK 今天成功做出了多个外星人 代码来了 import sys import pygame from bullet import Bullet from alien import Alien def check keydown events