加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

小游戏Bomb Cacher

发布时间:2020-12-17 17:25:53 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import sysimport pygameimport randomimport pdbfrom pygame.locals import *def print_text(font,x,y,text,color=(255,255,255)): imgText = font.r

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import sys
import pygame
import random
import pdb
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))

# main begin
pygame.init()
screen = pygame.display.set_mode((600,500))
pygame.display.set_caption("Bomb Game")
font1 = pygame.font.Font(None,24)
pygame.mouse.set_visible(False)
white = 255,255
red = 220,50,50
yellow = 230,230,50
black = 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 = 0.1

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
                vel_y = 0.1
    keys = pygame.key.get_pressed()
    if keys[K_ESCAPE]:
        sys.exit()
    screen.fill((0,100))

    if game_over:
        print_text(font1,100,200,"CLICK TO PLAY")
    else:
        bomb_y += vel_y

        # has the player missed the bomb
        if bomb_y > 500:
            bomb_x = random.randint(0,500)
            bomb_y = -50
            lives -=1
            if lives == 0:
                game_over = True

        # see if player has caugh the bomb
        elif bomb_y > pos_y:
            if bomb_x > pos_x and bomb_x < pos_x + 120:
                vel_y += 0.05
                score += 10
                bomb_x = random.randint(0,500)
                bomb_y = -50

        # draw the bomb
        pygame.draw.circle(screen,black,(bomb_x-4,int(bomb_y)-4),30,0)
        pygame.draw.circle(screen,yellow,(bomb_x,int(bomb_y)),0)

        # set basket position
        pos_x = mouse_x

        if pos_x < 0:
            pos_x = 0
        elif pos_x > 500:
            pos_x = 500
        # draw basket
        pygame.draw.rect(screen,(pos_x-4,pos_y-4,120,40),0)
        pygame.draw.rect(screen,red,(pos_x,pos_y,0)
        # print
        print_text(font1,"LIVES" + str(lives))
        print_text(font1,500,"SCORE" + str(score))
        pygame.display.update()

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读