boren-接小球游戏

简介: boren-接小球游戏
import random
import pygame
import sys
def figure(screen, photo, x, y):
    content = pygame.image.load(photo)
    screen.blit(content, (x, y))
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 = 640, 480
    score = 0
    hp = 3
    speed = 1
    ball_x, ball_y = 400, 0
    ban_x, ban_y, ban_width, ban_height = 400, screen_h - 40, 220, 100
    bg_volume = 0.9
    sound_volume = 0.4
    start = "请点击开始游戏"
    result = ""
    title = "接亚瑟"
    sound_score = "C:/Users/Administrator/Desktop/涂博仁编程文件夹(只能用Python打开的程序)/hit_wall.wav"
    ball_figure = "yase.png"
    bj_figure = "bj.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 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)
        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)
            #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()
相关文章
|
8月前
|
容器
如何在射击游戏中实现双摇杆控制?
如何在射击游戏中实现双摇杆控制?
88 0
|
8月前
|
流计算
泡泡龙游戏开发实战教程(3):泡泡如何发射,反弹,移动,停靠
泡泡龙游戏开发实战教程(3):泡泡如何发射,反弹,移动,停靠
121 0
|
Python
飞机大战-显示敌机并发射子弹(6)
用Python语言编写显示敌机并发射子弹。
75 2
|
Python
飞机大战-发射子弹(5)
用Python语言编写飞机大战,主要编写发射子弹。
89 0
|
7月前
|
图形学
【unity小技巧】FPS游戏实现相机的偏移震动、武器射击后退和后坐力效果
【unity小技巧】FPS游戏实现相机的偏移震动、武器射击后退和后坐力效果
68 1
|
Go Android开发
圆曾经的小车梦,造一台智能小车(三)之小车前进后退左右转基本框架
圆曾经的小车梦,造一台智能小车(三)之小车前进后退左右转基本框架
324 0
|
8月前
俄罗斯方块游戏开发实战教程(3):玩家如何控制形状
俄罗斯方块游戏开发实战教程(3):玩家如何控制形状
95 1
|
C语言
圆曾经的小车梦,造一台智能小车(四)之QT上位机控制小车
圆曾经的小车梦,造一台智能小车(四)之QT上位机控制小车
265 0
|
机器学习/深度学习
1347:【例4-8】格子游戏
1347:【例4-8】格子游戏
133 0
|
容器
射击游戏中如何实现双摇杆控制
嗨!大家好,我是小蚂蚁。 在移动设备上玩射击游戏,双摇杆控制是一种比较好的方式,我们今天就来分享一下如何在射击游戏中实现双摇杆控制。 所谓的双摇杆控制,指的是一个摇杆控制角色的移动,一个摇杆控制角色的射击。 来到小游戏制作工具中,先导入一些素材。
217 0

热门文章

最新文章