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

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()

相关文章:

  • 【成品论文】2025年电工杯数学建模竞赛B题50页保奖成品论文+matlab/python代码+数据集等(后续会更新)
  • 第十七次CCF-CSP算法(含C++源码)
  • Linux10正式版发布,拥抱AI了!
  • Python性能优化利器:__slots__的深度解析与避坑指南
  • 分布式锁总结
  • MSP430G2553 USCI模块串口通信
  • 【疑难杂症】Vue前端下载文件无法打开 已解决
  • S32K开发环境搭建详细教程(一、S32K IDE安装注册)
  • Spring 框架的JDBC 模板技术
  • 负载均衡笔记
  • python word 读取section.page_height参数错误
  • HarmonyOS:相机选择器
  • Halcon 单目相机标定测量
  • [python]opencv从源码编译python版本发现不生成cv2.pyd
  • 游戏引擎学习第309天:用于重叠检测的网格划分
  • 考研408《计算机组成原理》复习笔记,第二章(3)数值数据的运算和存储(定点数计算)
  • 实战教程:基于Vue.js与Django REST Framework的任务管理SPA开发全流程
  • WPF按钮Radius化
  • 如何选择支持AI接入的开发语言与框架
  • .NET ORM开发手册:基于SqlSugar的高效数据访问全攻略
  • 网站宽度 超宽/网络营销做得好的酒店
  • 基层消防力量建设/seo优化关键词是什么意思
  • 域名收录提交入口/长沙整站优化
  • 百度手机导航官方新版/seo专家是什么意思
  • wordpress app 生成6/如何优化关键词的排名
  • 企业文化宣传策划方案/宁波seo外包引流推广