猫史档案馆


【Python作品分享】气球戳戳戳【作业帖】

用户:温柔的重金怪7EO3温柔的重金怪7EO3查看:85 回复:0 评论:0 创建时间:2019-06-20T15:58:38


【作品展示】

center_image

 

【作品介绍】

1

 

【作品源代码】

import pygame
import random
from pygame.locals import *

# 准备工作
pygame.init()
screen = pygame.display.set_mode((600, 500))
# 加载背景图片
background = pygame.image.load('background.png')

# 加载气球
balloon_size = 50   #气球的尺寸,高和宽都是50
balloon_num = 20  #气球的数量
balloonList = []
for i in range(balloon_num):
    balloonList.append(pygame.image.load('balloon.png'))

# 确定气球的初始位置
balloonPosList = []
for i in range(balloon_num):
    # 随机产生气球初始位置的坐标
    balloon_x = random.randint(0, 550)   #比屏幕宽度少50
    balloon_y = random.randint(0, 450)   #比屏幕高度少50
    balloonPosList.append( (balloon_x, balloon_y) )

############################## step1 start ##############################
# 加载幽灵
ghost_size = 50   #幽灵的尺寸,高和宽都是50
ghost_num = 20    #幽灵的数量
ghostList = []
for i in range(ghost_num):
    ghostList.append(pygame.image.load('ghost.png'))

# 确定幽灵的初始位置
ghostPosList = []
for i in range(ghost_num):
    # 随机产生幽灵初始位置的坐标
    ghost_x = random.randint(0, 550)   #比屏幕宽度少50
    ghost_y = random.randint(0, 450)   #比屏幕高度少50
    ghostPosList.append( (ghost_x, ghost_y) )

############################## step1 end ##############################

############################## step5 start ##############################     
total = 0
############################## step5 end ##############################
while True:
    
    # 如果没气球了,画胜利界面
    if len(balloonList) == 0:
        # 画胜利界面
        screen.blit(background, (0, 0))
        youwin = pygame.image.load('youwin.png')
        screen.blit(youwin, (150, 244.5))
        pygame.display.update()
        
    ############################## step4 start ##############################     
    #如果幽灵被清空,画失败界面
    elif len(ghostList) == 0:
        # 画gameover界面
        screen.blit(background, (0, 0))
        gameover = pygame.image.load('gameover.png')
        screen.blit(gameover, (150, 244.5))
    ############################## step4 end ##############################

    ############################## step5 start ##############################  
        # 给出戳破气球数量
        ziti = pygame.font.Font("msyh.ttc",25)
        text = ziti.render("戳破气球"+str(total)+"个,加油哦",True,(255,255,255))
        screen.blit(text, (180, 180))
        pygame.display.update()
    ############################## step5 end ############################## 
        
    else:  #游戏进行中,不停绘制背景、气球和幽灵
        # 画背景
        screen.blit(background, (0, 0))
        
        # 画气球(气球的位置固定不变)
        for i in range(len(balloonList)):
            b_x, b_y = balloonPosList[i]
            screen.blit(balloonList[i], (b_x, b_y) )

        ############################## step2 start ##############################
        # 画幽灵(幽灵的位置会变, 根据初始位置随机上下左右4个方向改变坐标)
        for i in range(len(ghostList)):
            g_x, g_y = ghostPosList[i]
            #随机获取坐标要改变的方向
            direction = random.randint(1, 4)  
            # 避免幽灵跑出去
            if direction == 1 and g_y > 0:   #向上
                g_y = g_y - 3
            elif direction == 2 and  g_y < 450:  #向下
                g_y = g_y + 3
            elif direction == 3 and  g_x > 0:  #向左
                g_x = g_x - 3
            elif direction == 4 and  g_y < 550:  #向右
                g_x = g_x + 3
            #修改幽灵的初始位置,较大的改变幽灵运动的幅度
            ghostPosList[i] = (g_x, g_y)      #给列表元素重新赋值
            
            screen.blit(ghostList[i], (g_x, g_y))
        ############################## step2 end ##############################
            
        pygame.display.update()
    
    # 侦测事件
    for event in pygame.event.get():
        # 获取鼠标坐标
        x, y = pygame.mouse.get_pos()
        
        # 退出事件
        if event.type == QUIT:
            pygame.display.quit()
            exit()
        
        # 鼠标点击事件
        if event.type == MOUSEBUTTONDOWN:
            #如果点击气球,气球消失
            for balloon in balloonList:
                #当前气球的下标
                index = balloonList.index(balloon)
                #当前气球的坐标
                b_x,b_y = balloonPosList[index]
                if (b_x

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页第 1 页 / 共 0 页下一页