Python战机

2024-01-10 04:44
文章标签 python 战机

本文主要是介绍Python战机,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

基础版

import pygame
import random# 设置游戏屏幕大小
screen_width = 480
screen_height = 600# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)# 初始化pygame
pygame.init()# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("雷霆战机")# 加载背景音乐
pygame.mixer.music.load("background.wav")
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)# 加载玩家飞机图片
player_img = pygame.image.load("player.png")
player_rect = player_img.get_rect()
player_rect.centerx = screen_width // 2
player_rect.bottom = screen_height - 10# 创建子弹精灵组
bullet_group = pygame.sprite.Group()# 设置游戏时钟
clock = pygame.time.Clock()# 初始化分数
score = 0# 游戏主循环
running = True
while running:# 设置帧率clock.tick(60)# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:bullet = pygame.Rect(player_rect.centerx, player_rect.top - 10, 5, 10)bullet_group.add(bullet)# 检测子弹是否击中敌机for bullet in bullet_group:bullet.y -= 10if bullet.y < 0:bullet_group.remove(bullet)else:pygame.draw.rect(screen, WHITE, bullet)# 绘制玩家飞机screen.blit(player_img, player_rect)# 更新屏幕pygame.display.flip()# 清空屏幕screen.fill(BLACK)

完善版

import pygame
import random# 设置游戏屏幕大小
screen_width = 480
screen_height = 600# 定义颜色
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)# 初始化pygame
pygame.init()# 创建游戏窗口
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("雷霆战机")# 加载背景音乐
pygame.mixer.music.load("background.wav")
pygame.mixer.music.set_volume(0.3)
pygame.mixer.music.play(-1)# 加载玩家飞机图片
player_img = pygame.image.load("player.png")
player_rect = player_img.get_rect()
player_rect.centerx = screen_width // 2
player_rect.bottom = screen_height - 10# 加载敌机图片
enemy_img = pygame.image.load("enemy.png")# 创建子弹精灵组
bullet_group = pygame.sprite.Group()# 创建敌机精灵组
enemy_group = pygame.sprite.Group()# 设置游戏时钟
clock = pygame.time.Clock()# 初始化分数
score = 0# 显示分数的函数
def show_score():font = pygame.font.Font(None, 36)score_text = font.render(f"Score: {score}", True, WHITE)screen.blit(score_text, (10, 10))# 显示游戏结束的函数
def show_game_over():font = pygame.font.Font(None, 48)game_over_text = font.render("Game Over", True, RED)screen.blit(game_over_text, (screen_width//2 - 100, screen_height//2 - 50))# 游戏主循环
running = True
while running:# 设置帧率clock.tick(60)# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_SPACE:bullet = pygame.Rect(player_rect.centerx, player_rect.top - 10, 5, 10)bullet_group.add(bullet)# 检测子弹是否击中敌机for bullet in bullet_group:bullet.y -= 10if bullet.y < 0:bullet_group.remove(bullet)else:pygame.draw.rect(screen, WHITE, bullet)# 绘制玩家飞机screen.blit(player_img, player_rect)# 生成敌机if random.randint(0, 100) < 3:enemy_rect = enemy_img.get_rect()enemy_rect.x = random.randint(0, screen_width - enemy_rect.width)enemy_rect.y = -enemy_rect.heightenemy_group.add(enemy_rect)# 移动敌机for enemy in enemy_group:enemy.y += 5if enemy.y > screen_height:enemy_group.remove(enemy)# 绘制敌机screen.blit(enemy_img, enemy_rect)# 检测敌机和玩家飞机是否碰撞if pygame.sprite.spritecollide(player_rect, enemy_group, True):running = False# 检测子弹和敌机是否碰撞if pygame.sprite.groupcollide(bullet_group, enemy_group, True, True):score += 1# 显示分数show_score()# 更新屏幕pygame.display.flip()# 清空屏幕screen.fill(BLACK)# 显示游戏结束界面
show_game_over()# 更新屏幕
pygame.display.flip()# 等待2秒钟后退出游戏
pygame.time.wait(2000)# 退出游戏
pygame.quit()

这篇关于Python战机的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/589583

相关文章

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

python panda库从基础到高级操作分析

《pythonpanda库从基础到高级操作分析》本文介绍了Pandas库的核心功能,包括处理结构化数据的Series和DataFrame数据结构,数据读取、清洗、分组聚合、合并、时间序列分析及大数据... 目录1. Pandas 概述2. 基本操作:数据读取与查看3. 索引操作:精准定位数据4. Group

Python pandas库自学超详细教程

《Pythonpandas库自学超详细教程》文章介绍了Pandas库的基本功能、安装方法及核心操作,涵盖数据导入(CSV/Excel等)、数据结构(Series、DataFrame)、数据清洗、转换... 目录一、什么是Pandas库(1)、Pandas 应用(2)、Pandas 功能(3)、数据结构二、安

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

Python安装Pandas库的两种方法

《Python安装Pandas库的两种方法》本文介绍了三种安装PythonPandas库的方法,通过cmd命令行安装并解决版本冲突,手动下载whl文件安装,更换国内镜像源加速下载,最后建议用pipli... 目录方法一:cmd命令行执行pip install pandas方法二:找到pandas下载库,然后

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

Python标准库之数据压缩和存档的应用详解

《Python标准库之数据压缩和存档的应用详解》在数据处理与存储领域,压缩和存档是提升效率的关键技术,Python标准库提供了一套完整的工具链,下面小编就来和大家简单介绍一下吧... 目录一、核心模块架构与设计哲学二、关键模块深度解析1.tarfile:专业级归档工具2.zipfile:跨平台归档首选3.

使用Python构建智能BAT文件生成器的完美解决方案

《使用Python构建智能BAT文件生成器的完美解决方案》这篇文章主要为大家详细介绍了如何使用wxPython构建一个智能的BAT文件生成器,它不仅能够为Python脚本生成启动脚本,还提供了完整的文... 目录引言运行效果图项目背景与需求分析核心需求技术选型核心功能实现1. 数据库设计2. 界面布局设计3

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON: