Boren-python接小球游戏

本文介绍了使用Boren和Python实现的小球游戏,探讨了游戏的实现细节和技术要点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import random
import pygame
pygame.init()  # 1.游戏初始化

screen = pygame.display.set_mode((700,600)) # 2.创建一个窗口,设置大小
pygame.display.set_caption("接小球游戏")
ball_x,ball_y = 400,0
ban_x, ban_y ,ban_width, ban_height = 400,550,220,100
font = pygame.font.Font('ziti.ttf',24)
ball_true1 = pygame.image.load("ball.bmp")
ball_true2 = pygame.image.load("ball1.png")
ball_true3 = pygame.image.load("ball2.png")
ball_true4 = pygame.image.load("ball3.png")


# pygame.mixer.init() # 2.加载音效
# hit = pygame.mixer.Sound("hit_wall.wav") # 加载音乐
# hit.set_volume(0.4) # 设置音量
#

#pygame.mixer.music.load("bg.mp3")
#pygame.mixer.music.set_volume(0.3)
#pygame.mixer.music.plat(-1)
score=0
hp = 3
game_over = True
a_left = 0
a_right = 0
while True:
    
    key = pygame.key.get_pressed()
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEBUTTONUP:
            if game_over:
                game_over = False
                score = 0
                hp = 4
        elif event.type == pygame.MOUSEMOTION:
            ban_x,_ = event.pos
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                a_right = 0
                a_left =a_left+10
                ban_x = ban_x-(10+a_left)
            elif event.key == pygame.K_RIGHT:
                a_left = 0
                a_right = a_right+10
                ban_x= ban_x+10+a_right
    screen.fill((255,255,255))
    if game_over:
        imgtext = font.render('请点击屏幕开始游戏' , True, (0, 0, 0))  # 设置文字参数,内容,锯齿话,颜色
        screen.blit(imgtext, (350, 300))  # 将文字放在屏幕上
    else:
        ball_y = ball_y + 1
        # 没接到小球的判定
        if ball_y>600:
            ball_x = random.randint(0,600)
            ball_y = 0
            hp = hp-1
            if hp ==0:
                game_over=True

        # 接到小球的判定结果
        if ban_x <=ball_x<=ban_x+220 and ban_y <=ball_y<=ban_y+100:
            score = score+1
            # hit.play()
            ball_x = random.randint(0, 600)
            ball_y = 0
        imgtext = font.render('分数:%d'%score,True,(0,0,0)) # 设置文字参数,内容,锯齿话,颜色
        screen.blit(imgtext,(0,0)) # 将文字放在屏幕上

        imgtext2 = font.render("生命值:%d"%hp,True,(150,162,22))
        screen.blit(imgtext2,(580,0))
        # pygame.draw.circle(screen,(100,255,0),(ball_x,ball_y),30,0)
        if 0< score <=5:
            screen.blit(ball_true1,(ball_x,ball_y))
        elif 10> score>=5:
            screen.blit(ball_true2,(ball_x,ball_y))
        elif 15>score>=10:
            screen.blit(ball_true3, (ball_x, ball_y))
        elif 20>score>=15:
            screen.blit(ball_true4, (ball_x, ball_y))


        pygame.draw.rect(screen,(100,255,0),(ban_x, ban_y ,ban_width, ban_height),0)
        pygame.display.update() #3.不断循环
    pygame.display.update()  # 3.不断循环


2.0

import random
import pygame
import sys


def figure(screen, photo, x, y):
    content = pygame.image.load(photo)
    screen.blit(content, (x, y))

def button(screen, x,y,w,h,color):
    pygame.draw.rect(screen,color,(x,y,w,h))
    ziti(screen,'排行榜',(x+10),(y+10))

def ziti(screen, text, x, y, size = 24,color=(232, 206, 144)):
    font = pygame.font.Font("ziti.ttf", size)
    img_text = font.render(text, True, color)  # 设置文字参数,内容,锯齿话,颜色
    screen.blit(img_text, (x, y))  # 将文字放在屏幕上


def run():
    screen_w, screen_h = 1080, 652
    score = 0
    result = {}
    play_num = 1
    hp = 3
    speed = 5
    ball_x, ball_y = 400, 0
    ban_x, ban_y, ban_width, ban_height = 400, screen_h - 40, 360, 53
    # bg_volume = 0.9
    # sound_volume = 0.4

    start = "请点击开始游戏"
    result = ""
    title = "接亚瑟"

    # sound_score = "hit_wall.wav"
    ball_figure = "yase.png"
    bj_figure = "bg.jpg"
    # bg_music = "bj.mp3"
    ban ="ban5.png"
    game_over = True
    # 检测按键是不是按着不放
    moving_left = False
    moving_right = False
    moving_up = False
    moving_down = False

    screen = pygame.display.set_mode((screen_w, screen_h))  # 2.创建一个窗口,设置大小
    pygame.display.set_caption(title)
    # hit = pygame.mixer.Sound(sound_score)  # 加载音乐
    # hit.set_volume(sound_volume)  # 设置音量
    # pygame.mixer.music.load(bg_music)
    # pygame.mixer.music.set_volume(bg_volume)
    # pygame.mixer_music.play(-1)

    while True:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif event.type == pygame.MOUSEBUTTONUP:
                # 判断鼠标的坐标是不是在排行榜那个长方形的中间
                if 
                    # 把分数显示到屏幕上
                    
                if game_over:
                    game_over = False
                    score = 0
                    hp = 4
            elif event.type == pygame.MOUSEMOTION:
                ban_x, _ = event.pos

            elif event.type == pygame.KEYDOWN:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    moving_left = True
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    moving_right = True
                elif event.key == pygame.K_w or event.key == pygame.K_UP:
                    moving_up = True
                elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    moving_down = True

            elif event.type == pygame.KEYUP:
                if event.key == pygame.K_a or event.key == pygame.K_LEFT:
                    moving_left = False
                elif event.key == pygame.K_d or event.key == pygame.K_RIGHT:
                    moving_right = False
                elif event.key == pygame.K_w or event.key == pygame.K_UP:
                    moving_up = False
                elif event.key == pygame.K_s or event.key == pygame.K_DOWN:
                    moving_down = False

        figure(screen, bj_figure, 0, 0)
        if game_over:
            ziti(screen, start, 100,screen_h / 2,50)
            ziti(screen, result, 100, screen_h / 2 + 50,50)
            button(screen, screen_w/2-50, 20,100,50,(0,0,0))


        else:
            ball_y = ball_y + speed
            if moving_right:
                ban_x += 30
            elif moving_left:
                ban_x -= 30
            elif moving_down:
                ban_y += 30
            elif moving_up:
                ban_y -= 30

            # 没接到小球的判定
            if ball_y > screen_h:
                ball_x = random.randint(0, screen_w)
                ball_y = 0
                hp = hp - 1
                speed = 1
                if hp == 0:
                    start = "重新开始请点击屏幕"
                    result = "你击杀了%d个小球" % score
                    game_over = True

            # 接到小球的判定结果
            if ban_x <= ball_x <= ban_x + ban_width and ban_y <= ball_y <= ban_y + ban_height:
                score = score + 1
                speed = speed + 1
                # hit.play()
                ball_x = random.randint(0, 600)
                ball_y = 0
            ziti(screen, '分数:%d' % score, 0, 0)
            ziti(screen, "生命值:%d" % hp, screen_w - 120, 0)
            figure(screen, ball_figure, ball_x, ball_y)
            figure(screen,ban,ban_x, ban_y)
            #pygame.draw.rect(screen, (100, 255, 0), (ban_x, ban_y, ban_width, ban_height), 0)
        pygame.display.update()  # 3.不断循环


def main():
    pygame.init()  # 1.游戏初始化
    # pygame.mixer.init()  # 2.音乐初始化
    run()


if __name__ == '__main__':
    main()
### PIC16F1939-I/PT 微控制器概述 PIC16F1939-I/PT 是一款由 Microchip Technology 生产的高性能 8 位单片机,属于增强型 mid-range PIC 单片机家族的一部分。该设备集成了丰富的外设资源和低功耗特性,适用于多种嵌入式应用场合[^4]。 #### 主要特点 - **内核性能**: 高达 20 MIPS 的运行速度(在 32 MHz 下),支持精简指令集架构 RISC。 - **存储结构**: - 程序闪存: 最大可达 16 KB。 - 数据 SRAM: 提供高达 368 字节的数据存储空间。 - **I/O 口**: 支持多达 35 个可编程 I/O 引脚。 - **模拟功能**: - 内置 10 位 ADC,最多支持 13 路输入通道。 - 模拟比较器模块。 - **定时器/计数器**: 多种定时器选项,包括 Timer0、Timer1 和 Timer2。 - **通信口**: - SPI/I²C Master Slave 模块。 - EUSART(Enhanced Universal Synchronous Asynchronous Receiver Transmitter)。 - **其他特性**: - 看门狗定时器 (WDT)。 - 低压检测 (LVD) 功能。 - 可选内部振荡器模式。 --- ### 数据手册获取方式 Microchip 官方网站提供了完整的数据手册和技术文档,涵盖了 PIC16F1939-I/PT 的所有技术细节。以下是访问路径: - 访问 [Microchip 官方产品页面](https://www.microchip.com/) 并搜索型号 `PIC16F1939`。 - 在下载部分可以找到 PDF 版本的数据手册以及相关工具链的支持说明[^5]。 --- ### 示例代码片段 以下是一个简单的 C 语言程序示例,展示如何初始化并使用 PIC16F1939 的 GPIO 输出: ```c #include <xc.h> // 配置时钟源为 FOSC = INTOSC, CLKOUTEN = OFF #pragma config FOSC = INTRC_NOCLKOUT #pragma config WDTE = OFF // 关闭看门狗定时器 #pragma config PWRTE = ON // 上电延时开启 #pragma config MCLRE = ON // RA5/MCLR/VPP 引脚启用复位功能 #pragma config BOREN = ON // 启用 Brown-out Reset #pragma config CP = OFF // 关闭代码保护 #pragma config CPD = OFF // 关闭数据 EEPROM 保护 void main(void) { TRISAbits.TRISA0 = 0; // 将 RA0 配置为输出 while (1) { LATAbits.LATA0 = 1; // 设置 RA0 输出高电平 __delay_ms(500); // 延迟 500 ms LATAbits.LATA0 = 0; // 设置 RA0 输出低电平 __delay_ms(500); // 延迟 500 ms } } ``` 此代码实现了通过 RA0 引脚控制 LED 的闪烁效果[^6]。 --- ### 应用领域 PIC16F1939-I/PT 凭借其强大的功能组合,在多个行业中得到了广泛应用,具体如下: - **消费电子产品**: 如家用电器中的温控系统、风扇调速等。 - **工业自动化**: 实现电机驱动、传感器信号采集等功能。 - **医疗设备**: 用于便携式健康监测仪或其他小型化医疗器械中。 - **汽车电子**: 控制车灯亮度调节或车内环境监控单元。 --- ### 开发工具推荐 为了更高效地开发基于 PIC16F1939 的项目,建议采用以下工具链: - **编译器**: MPLAB X IDE 结合 XC8 编译器提供全面的调试和支持能力。 - **仿真器**: PICKIT 3 或 REAL ICE 设备可用于硬件验证阶段。 - **烧录工具**: 利用在线串行编程口完成固件更新过程[^7]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值