import pygame
import random
import sys
# 初始化
pygame.init()
# 设置屏幕大小
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("打爆气球游戏")
# 加载背景图片
background_img = pygame.image.load("bg.jpg")
background_img = pygame.transform.scale(background_img, (800, 600))
# 颜色定义
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# 加载气球图片
balloon_imgs = [pygame.image.load(f"balloon/{i}.png") for i in range(9)]
for i in range(9):
balloon_imgs[i] = pygame.transform.scale(balloon_imgs[i], (50, 50))
# 游戏结束文字
font = pygame.font.Font(None, 36)
# 初始化气球位置
balloon_x = random.randint(0, 750)
balloon_y = 500
# 设置速度
balloon_speed = 0.1
# 初始化得分
score = 0
def draw_balloons(balloon_index):
screen.blit(balloon_imgs[balloon_index], (balloon_x, balloon_y))
def display_score():
score_text = font.render("score: {}".format(score), True, RED)
screen.blit(score_text, (600, 10))
def game_over():
text = font.render("游戏结束!", True, RED)
text_rect = text.get_rect(center=(400, 300))
screen.blit(text, text_rect)
def calculate_score(balloon_index):
if 0 <= balloon_index <= 2:
return 1
elif 3 <= balloon_index <= 5:
return 1.5
elif 6 <= balloon_index <= 7:
return 3
else:
return 5
# 主循环
balloon_x = random.randint(0, 750) # 将初始化放在循环外面
balloon_y = 500
balloon_index = random.randint(0, 8) # 初始化气球编号
# 主循环
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.MOUSEBUTTONDOWN:
mouse_x, mouse_y = pygame.mouse.get_pos()
# 判断是否点击到气球
if mouse_x >= balloon_x and mouse_x <= balloon_x + 50 and mouse_y >= balloon_y and mouse_y <= balloon_y + 50:
balloon_y = 500
balloon_x = random.randint(0, 750)
balloon_index = random.randint(0, 8) # 初始化气球编号
score += calculate_score(balloon_index)
# 绘制背景图片
screen.blit(background_img, (0, 0))
balloon_y -= balloon_speed
if balloon_y < -50:
balloon_y = 500
balloon_index = random.randint(0, 8)
draw_balloons(balloon_index)
display_score()
if balloon_y == 500:
game_over()
pygame.display.flip()
效果图文章来源:https://www.toymoban.com/news/detail-728326.html
文章来源地址https://www.toymoban.com/news/detail-728326.html
到了这里,关于国庆 射击气球python小游戏的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!