前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Bomb Catcher 游戏 (Pygame)

Bomb Catcher 游戏 (Pygame)

作者头像
用户2965768
发布2018-12-19 17:10:33
5750
发布2018-12-19 17:10:33
举报
文章被收录于专栏:wym

 简单的处理事件,以及对鼠是的操作

不足就是不能让小球自己下落

代码语言:javascript
复制
import sys, random, time, pygame
from pygame.locals import *


def print_text(font, x, y, text, color=(255, 255, 255)):
    imgText = font.render(text, True, color)
    screen.blit(imgText, (x, y))


pygame.init()#初始化
screen = pygame.display.set_mode((600, 500))#创建高600,宽500的界面
pygame.display.set_caption("Bomb Catching Game")#窗口标题
font1 = pygame.font.Font(None, 24)#数字越大 字体越大 
pygame.mouse.set_visible(False)  #隐藏或显示当前的鼠标
white = 255, 255, 255
red = 220, 50, 50
yellow = 230, 230, 50
black = 0, 0, 0
lives = 3
score = 0
game_over = True
mouse_x = mouse_y = 0
pos_x = 300
pos_y = 460
bomb_x = random.randint(0, 500)#随机产生数字
bomb_y = -50#初始炸弹在上方
vel_y = 1.7#下降速度


while True:
    for event in pygame.event.get():
        if event.type == QUIT:#叉掉窗口
            sys.exit()
        elif event.type == MOUSEMOTION:#鼠标动作
            mouse_x, mouse_y = event.pos#鼠标当前坐标
            move_x, move_y = event.rel#鼠标相对移动
        elif event.type == MOUSEBUTTONUP:
            if game_over:
                game_over = False
                lives = 3
                score = 0

        keys = pygame.key.get_pressed()#轮询键盘接口,返回一个布尔值的大列表
        if keys[K_ESCAPE]:
            sys.exit()

        screen.fill((0, 0, 100))
        if game_over:
            print_text(font1, 100, 200, "<CLICK TO PLAY>")
        else:
            bomb_y += vel_y
        if bomb_y > 500:
            bomb_x = random.randint(0, 500)
            bomb_y = -50
            lives -= 1
            if lives == 0:
                game_over = True
        elif bomb_y > pos_y:
            if (bomb_x > pos_x) and (bomb_x < pos_x+120):
                score += 10
                bomb_x = random.randint(0, 500)
                bomb_y = -50
        #pygame.draw.circle(screen, black, (bomb_x-4, int(bomb_y)-4), 30, 0)
        pygame.draw.circle(screen, yellow, (bomb_x, int(bomb_y)), 30, 0)

        pos_x = mouse_x
        if pos_x < 0:
            pos_x = 0
        elif pos_x > 500:
            pos_x = 500
        #pygame.draw.rect(screen, black, (pos_x-4, pos_y-4, 120, 40), 0)
        pygame.draw.rect(screen, red, (pos_x, pos_y, 120, 40), 0)

        print_text(font1, 0, 0, "LIVES: "+str(lives))

        print_text(font1, 500, 0, "SCORE: "+str(score))
        pygame.display.update()
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年11月27日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档