import random
import pygame
import sys
import time
# 1。加载中 loading 初始化
pygame.init()
# 2.设置窗口大小 dis分开 展览馆 展示 (宽,高)
screen = pygame.display.set_mode((890, 550))
# 4.设置标题
pygame.display.set_caption("接药水游戏")
ball_x = 445
ball_y = 300
rect_w, rect_h = 150, 50
rect_x, rect_y = 300, 525
# 文1.0 设置文字类型和大小 font 字体
letter_style = pygame.font.Font("msyh.ttf", 50)
# 图1.0 加载图片 image 图片 load 加载
boom1 = pygame.image.load("兴奋饮料1.png")
boom2 = pygame.image.load("兴奋饮料2.png")
boom3 = pygame.image.load("兴奋饮料3.png")
boom4 = pygame.image.load("兴奋饮料4.png")
game_over = pygame.image.load("gameover.png")
index = 1
xi = 1
# 分数
score = 0
# hit point
Hp = 5
speed = 30
game = True
# 3.刷新
while True:
# 电脑监控我们的操作
# event 事件 pos position 位置
for event in pygame.event.get():
# print(event)
# 判断事件类型是不是退出
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
elif event.type == pygame.MOUSEMOTION:
rect_x, _ = event.pos
# 填充颜色
screen.fill((250, 240, 230))
if game:
ball_y = ball_y + speed
# 判断没接到小球
if ball_y > 550:
ball_y = 50
ball_x = random.randint(50, 800)
Hp = Hp - 1
if Hp <= 0:
game = False
# 判断接到炸药?
# 板的x坐标《球的x坐标 < 板的x坐标+宽度
# print(ball_x, ball_y)
# print(rect_x, rect_y)
if rect_x + rect_w > ball_x > rect_x and rect_y + rect_h > ball_y > rect_y:
score = score + 1
ball_y = 50
ball_x = random.randint(50, 800)
# 图2.0 将图片放到窗口上 blit传送
if 0*xi < index <= 1*xi:
screen.blit(boom1, (ball_x, ball_y))
index = index + 1
elif 1*xi < index <= 2*xi:
screen.blit(boom2, (ball_x, ball_y))
index = index + 1
elif 2*xi < index <= 3*xi:
screen.blit(boom3, (ball_x, ball_y))
index = index + 1
elif 3*xi < index <= 4*xi:
screen.blit(boom4, (ball_x, ball_y))
index = index + 1
if index > 4*xi:
index = 1
# 画小球: 窗口, 颜色 rgb , 位置(x,y)
# pygame.draw.circle(screen, (135, 206, 250), (ball_x, ball_y), 20)
# 画长方形: 窗口, 颜色 rgb , (x, y, 长,宽)
pygame.draw.rect(screen, (0, 255, 0), (rect_x, rect_y, rect_w, rect_h))
# 文2.0 设置文字内容和颜色
text = letter_style.render("分数:%d" % score, True, (0, 0, 0))
text2 = letter_style.render("生命值:%d" % Hp, True, (0, 0, 0))
# 文3.0 将文字放到窗口上 blit传送
screen.blit(text, (10, 10))
screen.blit(text2, (10, 60))
else:
screen.blit(game_over, (200, 70))
pygame.display.update()
time.sleep(0.07)
binrui - 接药水游戏4.0
猜你喜欢
转载自blog.csdn.net/houlaos/article/details/105716863
今日推荐
周排行