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,135

class 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,100
        self.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=1
        yuchuan.rect.left-=5
    if kkk[K_RIGHT]:
        nnn=0
        yuchuan.rect.left+=5
    if kkk[K_SPACE]:
        bullet=Bullet()
        bullet.rect.center=yuchuan.rect.left+50,yuchuan.rect.top+150
        danjia.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 = False

while running:
    clock.tick(5)  # 控制游戏速度

    # 处理事件
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        elif 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_SIZE
        elif direction == "DOWN":
            head[1] += SNAKE_SIZE
        elif direction == "LEFT":
            head[0] -= SNAKE_SIZE
        elif direction == "RIGHT":
            head[0] += SNAKE_SIZE

        # 检测碰撞
        if (head[0] < 0 or head[0] >= WIDTH or
                head[1] < 0 or head[1] >= HEIGHT or
                head in snake[1:]):
            game_over = True

        snake.insert(0, head)

        # 吃食物逻辑
        if head == food:
            score += 10
            food = [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()
相关推荐
吃一口大米饭几秒前
JAVA线程的几种状态
java·开发语言·jvm
多多*几秒前
写两个hascode和equals的例子
java·开发语言·jvm·spring boot·算法·spring·缓存
爱喝水的鱼丶6 分钟前
SAP-ABAP:在DEBUG过程中修改内表中的数据的方法详解
运维·开发语言·sap·异常处理·abap·修改内表
fc&&fl9 分钟前
pyspark实践
大数据·python
weixin_5150696634 分钟前
线程池详解:原理、使用与优化
java·开发语言
非小号37 分钟前
PaddleNLP 的文本分类项目
python·机器学习·分类
天才测试猿1 小时前
自动化测试工具:Selenium详解
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
怀旧,1 小时前
【Python】2. 基础语法(2)
开发语言·python
敲代码的瓦龙1 小时前
C++?继承!!!
c语言·开发语言·c++·windows·后端·算法