Qiyuan - 接小球游戏4.0

mac2026-03-31  5

import pygame import time import random # 点击 # loading 加载 初始化 pygame.init() # 1.初始化游戏 screen_height = 652 screen_width = 1080 ball_x = 300 # 小球x坐标 ball_y = 250 # 小球y左边 rect_x, rect_y, rect_w, rect_h = 300, 612, 120, 40 # 接板的x,y坐标,宽度,高度 score = 0 # 分数 lives = 30 # 修改属性 game_over = True screen = pygame.display.set_mode((screen_width, screen_height)) # 设置窗口大小 pygame.display.set_caption("蔡旭坤大招乔碧落") # 设置标题 # 事件 font1 = pygame.font.Font('ziti.ttf', 24) # 设置字体参数 ball1 = pygame.image.load('ball.png') # 加载图片 ball2 = pygame.image.load('ball2.png') # 加载图片 bg = pygame.image.load("bg1.jpg") # 加载音乐 pygame.mixer.init() # 背景音乐 pygame.mixer.music.load('hit_wall.wav') pygame.mixer.music.set_volume(0.3) pygame.mixer.music.play(-1) # 音效 hit = pygame.mixer.Sound("hit_wall.wav") hit.set_volume(0.4) def ball(ball_x, ball_y): pygame.draw.circle(screen, (255., 25, 52), (ball_x, ball_y), 20, 0) while True: # 不断循环 for event in pygame.event.get(): print(event) if event.type == pygame.QUIT: # 判断是否点击退出 pygame.quit() elif event.type == pygame.MOUSEBUTTONUP: # 鼠标抬起 if game_over: game_over=False elif event.type == pygame.MOUSEMOTION: # 监听鼠标动作 rect_x, _ = event.pos elif event.type == pygame.KEYDOWN: if event.key == pygame.K_a: rect_x = rect_x - 50 elif event.key == pygame.K_d: rect_x = rect_x + 50 if score < 10: screen.fill((240, 240, 240)) # regb else: screen.blit(bg, (0, 0)) if game_over: Text_start = font1.render('点击游戏', True, (0, 0, 0)) screen.blit(Text_start, (200, 200)) else: ball_y = ball_y + 10 if ball_y > screen_height: # ball_y = 0 ball_x = random.randint(0, 600) lives = lives -1 if lives == 0: game_over = True score = 0 lives = 30 if score<10: ball(ball_x, ball_y) elif score < 20: screen.blit(ball1,(ball_x, ball_y)) else: screen.blit(ball2, (ball_x, ball_y)) # 判断小球坐标在挡板的左边之间 if rect_x < ball_x < rect_x + rect_w and rect_y < ball_y < rect_y + rect_h: hit.play() # 播放音乐 score = score + 1 ball_y = 0 ball_x = random.randint(0, 600) Text_score = font1.render('分数:%d' % score, True, (0, 0, 0)) # 设置加入文字的参数 screen.blit(Text_score, (0, 0)) # 把文字放到屏幕上 Text_lives = font1.render('生命值:%d' % lives, True, (0, 0, 0)) screen.blit(Text_lives, (screen_width-100, 0)) pygame.draw.rect(screen, (100, 200, 30), (rect_x, rect_y, rect_w, rect_h), 0) pygame.display.update() # 刷新画面 # time.sleep(0.1) pygame.quit() # 2。退出游戏 # 游戏 # 操控游戏,判断输赢 # 添加图片 # bgm
最新回复(0)