当前位置: 首页 > article >正文

python炸鱼船

import pygame, random  # 加载库
from pygame.locals import *
pygame.init()
pygame.display.set_caption("炸渔船")
canvas = pygame.display.set_mode((700, 500))
bg=pygame.image.load("bg.png")
bg=pygame.transform.scale(bg,(700,500))class Hero(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)#加载图片,修改大小,获取矩形,设置中心位置self.image=[pygame.image.load("hero1.png"),pygame.image.load("hero2.png")]# 列表的创建,列表元素的调用,列表的索引,列表切片self.image=[pygame.transform.scale(self.image[0],(100,100)),pygame.transform.scale(self.image[1],(100,100))]self.rect=self.image[0].get_rect()self.rect.center=350,135class Bullet(pygame.sprite.Sprite):def __init__(self):pygame.sprite.Sprite.__init__(self)self.image=pygame.image.load("bullet.png")self.image=pygame.transform.scale(self.image,(30,100))self.rect=self.image.get_rect()self.rect.center=100,100self.speed=[0,5]def xianshi(self):canvas.blit(self.image,self.rect)
danjia=pygame.sprite.Group()def handleEvent():  # 点叉叉关闭游戏界面for event in pygame.event.get():if event.type == QUIT:quit()
life=0
score=0
nnn=0
yuchuan=Hero()
while True:canvas.fill((255, 255, 255))canvas.blit(bg,(0,0))font=pygame.font.SysFont("kaiti",20)text=font.render("当前生命值:"+str(life)+"分数:"+str(score),True,(255,0,0))canvas.blit(text,(0,0))canvas.blit(yuchuan.image[nnn],yuchuan.rect)kkk=pygame.key.get_pressed()if kkk[K_LEFT]:nnn=1yuchuan.rect.left-=5if kkk[K_RIGHT]:nnn=0yuchuan.rect.left+=5if kkk[K_SPACE]:bullet=Bullet()bullet.rect.center=yuchuan.rect.left+50,yuchuan.rect.top+150danjia.add(bullet)for d in danjia:d.xianshi()d.rect=d.rect.move(d.speed)# d.rect.center=yuchuan.rect.center#类属性的调用handleEvent()pygame.display.update()import pygame
import random
import time# 初始化 Pygame
pygame.init()# 游戏窗口设置
WIDTH, HEIGHT = 640, 480
window = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("贪吃蛇")# 颜色定义
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = (0, 255, 0)# 蛇与食物设置
SNAKE_SIZE = 20
snake = [[100, 100], [80, 100], [60, 100]]  # 初始蛇身坐标
food = [random.randrange(10, (WIDTH // SNAKE_SIZE)) * SNAKE_SIZE,random.randrange(10, (HEIGHT // SNAKE_SIZE)) * SNAKE_SIZE]
direction = "RIGHT"  # 初始方向
score = 10# 游戏主循环
clock = pygame.time.Clock()
running = True
game_over = Falsewhile running:clock.tick(5)  # 控制游戏速度# 处理事件for event in pygame.event.get():if event.type == pygame.QUIT:running = Falseelif event.type == pygame.KEYDOWN:if event.key == pygame.K_UP and direction != "DOWN":direction = "UP"elif event.key == pygame.K_DOWN and direction != "UP":direction = "DOWN"elif event.key == pygame.K_LEFT and direction != "RIGHT":direction = "LEFT"elif event.key == pygame.K_RIGHT and direction != "LEFT":direction = "RIGHT"if not game_over:# 移动蛇头head = snake[0].copy()if direction == "UP":head[1] -= SNAKE_SIZEelif direction == "DOWN":head[1] += SNAKE_SIZEelif direction == "LEFT":head[0] -= SNAKE_SIZEelif direction == "RIGHT":head[0] += SNAKE_SIZE# 检测碰撞if (head[0] < 0 or head[0] >= WIDTH orhead[1] < 0 or head[1] >= HEIGHT orhead in snake[1:]):game_over = Truesnake.insert(0, head)# 吃食物逻辑if head == food:score += 10food = [random.randrange(1, (WIDTH // SNAKE_SIZE)) * SNAKE_SIZE,random.randrange(1, (HEIGHT // SNAKE_SIZE)) * SNAKE_SIZE]else:snake.pop()# 绘制画面window.fill(BLACK)# 绘制蛇身for pos in snake:pygame.draw.rect(window, GREEN, pygame.Rect(pos[0], pos[1], SNAKE_SIZE, SNAKE_SIZE))# 绘制食物pygame.draw.rect(window, RED, pygame.Rect(food[0], food[1], SNAKE_SIZE, SNAKE_SIZE))# 显示分数font = pygame.font.Font(None, 36)text = font.render(f"Score: {score}", True, WHITE)window.blit(text, (10, 10))# 游戏结束显示if game_over:font = pygame.font.Font(None, 72)text = font.render("GAME OVER!", True, RED)window.blit(text, (WIDTH // 2 - 140, HEIGHT // 2 - 40))pygame.display.flip()pygame.quit()
http://www.lryc.cn/news/2386776.html

相关文章:

  • 使用AutoKeras2.0的AutoModel进行结构化数据回归预测
  • 好用但不常用的Git配置
  • ULVAC VWR-400M/ERH 真空蒸发器 Compact Vacuum Evaporator DEPOX (VWR-400M/ERH)
  • P1068 [NOIP 2009 普及组] 分数线划定
  • PPT连同备注页(演讲者模式)一块转为PDF
  • 第三十二天打卡
  • 项目三 - 任务8:实现词频统计功能
  • MongoDB 快速整合 SpringBoot 示例
  • 2025.05.22-得物春招机考真题解析-第二题
  • ollama list模型列表获取 接口代码
  • OPC Client第5讲(wxwidgets):初始界面的事件处理;按照配置文件初始化界面的内容
  • 什么是BFC,如何触发BFC,BFC有什么特性?
  • python做题日记(9)
  • Leetcode 3557. Find Maximum Number of Non Intersecting Substrings
  • 【C++进阶篇】初识哈希
  • Spring Boot——自动配置
  • 免费轻量便携截图 录屏 OCR 翻译四合一!提升办公效率
  • 使用 Vuex 实现用户注册与登录功能
  • 进程通信(管道,共享内存实现)
  • 电池预测 | 第28讲 基于CNN-GRU的锂电池剩余寿命预测
  • 快速上手SHELL脚本常用命令
  • 【无标题】前端如何实现分页?
  • 【自然语言处理与大模型】大模型Agent四大的组件
  • 小巧高效的目录索引生成软件
  • 云原生架构设计相关原则
  • android实现使用RecyclerView详细
  • 华为云Flexus+DeepSeek征文 | Flexus X实例助力 Dify-LLM 一键部署:性能跃升与成本优化的革新实践
  • 曼昆经济学原理第九版目录
  • 数据库blog7_MySql的下载与配置准备
  • YOLOv11助力地铁机场安检!!!一键识别刀具