【Python】pygame弹球游戏实现

news/2025/1/1 16:17:49/

弹球游戏实现

游戏源码:

import pygame,pygame_os,random,math
"""
砖块设定:80*30 一个砖块 其中 70*20是有颜色的,边缘的5*5(四周)是白色--为了区分块与块之间总块数在10*13=130块,第一行是(0,0)-(0,9);第二行是(1,0)-(1,9)如此类推,用random函数进行随机放置方块
"""# 查找数组元素
def find(list, num):try:index = list.index(num)except:return -1else:return index# 初始变量
white = (255, 255, 255)
red = (255, 0, 0)
green = (0, 255, 0)
yellow = (255, 255, 0)
font_file = "IPix中文像素字体.ttf"
win_width, win_height, bg_color, caption_title = (800, 600, white, "弹球游戏")
clock = pygame.time.Clock()if __name__ == '__main__':# 程序死循环while 1:# 初始游戏窗口window_init = pygame_os.Start_Name(win_width, win_height, bg_color, caption_title)window = pygame_os.Start_Name.start_init(window_init)# 初始游戏开始界面pygame_os.Font.set_font(window, font_file, 45, "欢迎来到弹球游戏", (255, 0, 0),"top")for i in range(3):button_pos = ((win_width - 200)/2, 200 + 100 * i)button_name = ["简单难度", "普通难度", "困难难度"]locals()[f"button_{i}"] = pygame_os.Button(window, (255, 0, 0), button_pos, 200, 50, font_file, 30, white, button_name[i])pygame_os.Button.set_button(locals()[f"button_{i}"])#游戏选择难度while循环Start_bool = Truedifficulty = Nonetem_bool = True # 控制只执行一次的按钮if判断while Start_bool:for event in pygame.event.get():# 当按钮经过时:if event.type == pygame.MOUSEMOTION:# 简单难度按钮if (win_width - 200)/2 <= event.pos[0] <= (win_width + 200)/2 and 200 <= event.pos[1] <=250:if tem_bool == True:pygame_os.Button.button_MOUSEMOTION(button_0)tem_bool = False# 普通难度按钮elif (win_width - 200)/2 <= event.pos[0] <= (win_width + 200)/2 and 300 <= event.pos[1] <=350:if tem_bool == True:pygame_os.Button.button_MOUSEMOTION(button_1)tem_bool = False# 困难难度按钮elif (win_width - 200)/2 <= event.pos[0] <= (win_width + 200)/2 and 400 <= event.pos[1] <=450:if tem_bool == True:pygame_os.Button.button_MOUSEMOTION(button_2)tem_bool = False#重画else:pygame_os.Button.button_MOUSEMOTION_end(button_0)pygame_os.Button.button_MOUSEMOTION_end(button_1)pygame_os.Button.button_MOUSEMOTION_end(button_2)tem_bool = True# 当按钮按下时:if event.type == pygame.MOUSEBUTTONDOWN:# 简单难度按钮if (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 200 <= event.pos[1] <= 250:pygame_os.Button.button_MOUSEDOWN(button_0)difficulty = 0# 普通难度按钮elif (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 300 <= event.pos[1] <= 350:pygame_os.Button.button_MOUSEDOWN(button_1)difficulty = 1# 困难难度按钮elif (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 400 <= event.pos[1] <= 450:pygame_os.Button.button_MOUSEDOWN(button_2)difficulty = 2# 当按钮松开时:if event.type == pygame.MOUSEBUTTONUP:# 简单难度按钮if (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 200 <= event.pos[1] <= 250:pygame_os.Button.button_MOUSEUP(button_0)Start_bool = Falsebreak# 普通难度按钮elif (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 300 <= event.pos[1] <= 350:pygame_os.Button.button_MOUSEUP(button_1)Start_bool = Falsebreak# 困难难度按钮elif (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 400 <= event.pos[1] <= 450:pygame_os.Button.button_MOUSEUP(button_2)Start_bool = Falsebreak# 退出游戏代码if event.type == pygame.QUIT:exit()# 清屏pygame.draw.rect(window, white, (0, 0, win_width, win_height))pygame.display.update()# 画长板以及小球plank_size = 25 + 50 * (3 - difficulty)pygame.draw.rect(window, red, ((win_width - plank_size) / 2, 500, plank_size, 10))pygame.draw.circle(window, green, (win_width / 2, 490), 10)pygame.display.update()# 游戏开始设置开关Start_bool = Truelast_x, last_y, last_plank_x = win_width / 2, 490, (win_width - plank_size) / 2while Start_bool:for event in pygame.event.get():# 长板随着鼠标移动而移动if event.type == pygame.MOUSEMOTION:if event.pos[1] >= 500:pygame.draw.rect(window, white, (0, 500, win_width, 10))pygame.draw.rect(window, red, (event.pos[0] - plank_size/2, 500, plank_size, 10))pygame.draw.rect(window, white, (0, 480, win_width, 20))last_x, last_y, last_plank_x = event.pos[0], 490, event.pos[0] - plank_size/2pygame.draw.circle(window, green, (last_x, last_y), 10)pygame.display.update()if event.type == pygame.MOUSEBUTTONUP:angle = random.choice(range(20,60))x_speed = math.cos(math.radians(angle)) * (difficulty * 3 + 5)y_speed = - math.sin(math.radians(angle)) * (difficulty * 3 + 5)Start_bool = Falsebreak# 退出游戏代码if event.type == pygame.QUIT:exit()# 砖块放置函数brick_list = []def set_brick(difficulty):global brick_list, last_x, last_ybrick_list = []tem_list = []# 设立砖块数列for row in range(10):for column in range(13):brick_list.append([row, column, False])brick_num = random.choice(range(50 + 20 * difficulty, 80 + 20 * difficulty))# 删除球附近的砖块index = find(brick_list, [int((last_x-10)/80),int((last_y-10)/30), False])if index != -1:del brick_list[index]index = find(brick_list, [int((last_x + 10) / 80), int((last_y - 10) / 30), False])if index != -1:del brick_list[index]index = find(brick_list, [int((last_x - 10) / 80), int((last_y + 10) / 30), False])if index != -1:del brick_list[index]index = find(brick_list, [int((last_x + 10) / 80), int((last_y + 10) / 30), False])if index != -1:del brick_list[index]# 随机砖块数列for i in range(len(brick_list)):tem_list.append(i)for num in range(brick_num):random_num = random.choice(range(len(tem_list)))brick_list[tem_list[random_num]][2] = Truedel tem_list[random_num]# 构建被抽中砖块for num in range(len(brick_list)):if brick_list[num][2] == True:random_color = (random.choice(range(50, 200)),random.choice(range(50, 200)),random.choice(range(50, 200)))x = 80*brick_list[num][0]+5y = 30*brick_list[num][1]+5pygame.draw.rect(window, random_color, (x, y, 70, 20))# 游戏正式开始game_score = 0while True:# 刷新分数pygame.draw.rect(window, white, (0, 530, win_width, win_height - 530))pygame_os.Font.set_font(window, font_file, 30, "目前得分:{0}".format(game_score), (0, 0, 0), (0, 550))# 无尽模式砖块打完刷新Start_bool = Falsetem_num = 0for brick in brick_list:if brick[2] == True:tem_num += 1if tem_num >= 5:Start_bool = Trueif Start_bool == False:pygame.draw.rect(window, white, (0, 0, win_width, 490))set_brick(difficulty)# 画球的运动轨迹pygame.draw.circle(window, white, (last_x, last_y), 10)last_x += x_speedlast_y += y_speedpygame.draw.circle(window, green, (last_x , last_y), 10)# 碰到边缘反弹if last_x + 10 >= win_width or last_x - 10 <= 0:x_speed = -x_speedif (last_y <= 500 <= last_y + 10 and last_plank_x <= last_x <= last_plank_x + plank_size) or last_y - 10 <= 0:y_speed = -y_speed# 画边缘线pygame.draw.line(window, (0, 0, 0), (0, 0), (win_width, 0), 2)pygame.draw.line(window, (0, 0, 0), (0, 0), (0, 498), 2)pygame.draw.line(window, (0, 0, 0), (win_width - 2, 0), (win_width - 2, 498), 2)pygame.draw.line(window, (0, 0, 0), (0, 498), (win_width, 498), 2)# 画长板pygame.draw.rect(window, red, (last_plank_x, 500, plank_size, 10))# 判断砖块碰撞消失# 碰撞删除砖块def clean_break(window, pos_x, pos_y, ball_last_x, ball_last_y):global whitex = 80 * pos_x + 5y = 30 * pos_y + 5pygame.draw.rect(window, white, (x, y, 70, 20))# 重新画球pygame.draw.circle(window, green, (last_x, last_y), 10)pygame.display.update()# 从砖块左边碰撞judge_x = int((last_x+10)/80)judge_y = int(last_y/30)index = find(brick_list, [judge_x, judge_y, True])if index != -1 and x_speed > 0:brick_list[index][2] = Falsegame_score += 1x_speed = -x_speedclean_break(window, judge_x, judge_y, last_x, last_y)# 从砖块右边碰撞judge_x = int((last_x-10)/80)judge_y = int(last_y/30)index = find(brick_list, [judge_x, judge_y, True])if index != -1 and x_speed < 0:brick_list[index][2] = Falsegame_score += 1x_speed = -x_speedclean_break(window, judge_x, judge_y, last_x, last_y)# 从砖块上边碰撞judge_x = int(last_x/80)judge_y = int((last_y+10)/30)index = find(brick_list, [judge_x, judge_y, True])if index != -1 and y_speed > 0:brick_list[index][2] = Falsegame_score += 1y_speed = -y_speedclean_break(window, judge_x, judge_y, last_x, last_y)# 从砖块下边碰撞judge_x = int(last_x/80)judge_y = int((last_y-10)/30)index = find(brick_list, [judge_x, judge_y, True])if index != -1 and y_speed < 0:brick_list[index][2] = Falsegame_score += 1y_speed = -y_speedclean_break(window, judge_x, judge_y, last_x, last_y)pygame.display.update()# 判断Gameoverif last_y > 525:pygame.draw.circle(window, white, (last_x, last_y), 10)pygame.draw.rect(window, red, (last_plank_x, 500, plank_size, 10))pygame.display.update()breakfor event in pygame.event.get():if event.type == pygame.MOUSEMOTION:if event.pos[1] >= 500:last_plank_x = event.pos[0] - plank_size / 2pygame.draw.rect(window, white, (0, 500, win_width, 10))pygame.draw.rect(window, red, (last_plank_x, 500, plank_size, 10))pygame.display.update()if event.type == pygame.QUIT: exit()# 限制最大帧数为60clock.tick(60)# 画Gameover画面Start_bool = Truetem_bool = True  # 控制只执行一次的按钮if判断while Start_bool:pygame.draw.rect(window, (128, 128, 128), (0, 0, win_width, win_height / 4))pygame.display.update()clock.tick(1)pygame.draw.rect(window, (128, 128, 0), (0, win_height / 4, win_width, win_height*2 / 4))pygame.display.update()clock.tick(1)pygame.draw.rect(window, (128, 0, 128), (0, win_height*2 / 4, win_width, win_height*3 / 4))pygame.display.update()clock.tick(1)pygame.draw.rect(window, (0, 128, 128), (0, win_height*3 / 4, win_width, win_height))pygame.display.update()clock.tick(1)Start_bool = False# 画结束按钮button_pos = ((win_width - 200)/2, 350)button_refresh_init = pygame_os.Button(window, (255, 0, 0), button_pos, 200, 50, font_file, 30, white, "重来")pygame_os.Button.set_button(button_refresh_init)button_pos = ((win_width - 200)/2, 450)button_end_init = pygame_os.Button(window, (255, 0, 0), button_pos, 200, 50, font_file, 30, white, "退出")pygame_os.Button.set_button(button_end_init)# 画GAMEOVER字样pygame_os.Font.set_font(window, "IPix中文像素字体.ttf", 45, "GAMEOVER", (0, 0, 0), "center")pygame.display.update()Start_bool = Truewhile Start_bool:for event in pygame.event.get():if event.type == pygame.MOUSEMOTION:if (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 350 <= event.pos[1] <= 400:if tem_bool == True:pygame_os.Button.button_MOUSEMOTION(button_refresh_init)tem_bool = Falseelif (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 450 <= event.pos[1] <= 500:if tem_bool == True:pygame_os.Button.button_MOUSEMOTION(button_end_init)tem_bool = False# 重画else:pygame_os.Button.button_MOUSEMOTION_end(button_refresh_init)pygame_os.Button.button_MOUSEMOTION_end(button_end_init)tem_bool = Trueif event.type == pygame.MOUSEBUTTONDOWN:if (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 350 <= event.pos[1] <= 400:pygame_os.Button.button_MOUSEDOWN(button_refresh_init)if (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 450 <= event.pos[1] <= 500:pygame_os.Button.button_MOUSEDOWN(button_end_init)if event.type == pygame.MOUSEBUTTONUP:if (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 350 <= event.pos[1] <= 400:pygame_os.Button.button_MOUSEUP(button_refresh_init)Start_bool = Falseif (win_width - 200) / 2 <= event.pos[0] <= (win_width + 200) / 2 and 450 <= event.pos[1] <= 500:pygame_os.Button.button_MOUSEUP(button_end_init)exit()if event.type == pygame.QUIT:exit()"""
①道具
②砖块消失 √
③分裂球
④按Esc游戏暂停且跳出窗口
"""

pygame_os库:

import pygameclass Start_Name:def __init__(self, win_width, win_height, bg_color, caption_title):self.win_width = win_widthself.win_height = win_heightself.bg_color = bg_colorself.caption_title = caption_titledef start_init(self):pygame.init()window = pygame.display.set_mode((self.win_width, self.win_height))pygame.display.set_caption(self.caption_title)window.fill(self.bg_color)return windowclass Button:def __init__(self, window, button_color, pos, width, height, font_name, font_size, font_color, font_text):self.window = windowself.button_color = button_colorself.pos = posself.width = widthself.height = heightself.font_name = font_nameself.font_size = font_sizeself.font_color = font_colorself.font_text = font_textself.button_tem_DU_color = button_colorself.button_tem_motion_color = button_colordef set_button(self):x, y = self.pospygame.draw.rect(self.window, self.button_color, (x, y, self.width, self.height))font01 = pygame.font.Font(self.font_name, self.font_size)text01 = font01.render(self.font_text, True, self.font_color)w, h = text01.get_size()self.window.blit(text01, (x + (self.width - w) / 2, y + (self.height - h) / 2))pygame.display.update()return text01def button_MOUSEDOWN(self):self.button_tem_DU_color = self.button_colorself.button_color = (128, 128, 128)Button.set_button(self)return Truedef button_MOUSEMOTION(self):self.button_tem_motion_color = self.button_colorself.button_color = (175, 175, 175)Button.set_button(self)return Truedef button_MOUSEUP(self):self.button_color = self.button_tem_DU_colorButton.set_button(self)return Truedef button_MOUSEMOTION_end(self):self.button_color = self.button_tem_motion_colorButton.set_button(self)return Trueclass Font:@staticmethoddef set_font(window, font_file, font_size, text, font_color, text_pos):font01 = pygame.font.Font(font_file, font_size)text01 = font01.render(text, True, font_color)if text_pos == "top":text_pos = ((window.get_size()[0] - text01.get_size()[0])/2, 0)if text_pos == "bottom":text_pos = (0, (window.get_size()[1] - text01.get_size()[1]) / 2)if text_pos == "center":text_pos = ((window.get_size()[0] - text01.get_size()[0])/2, (window.get_size()[1] - text01.get_size()[1])/2)window.blit(text01, text_pos)pygame.display.update()return text01

http://www.ppmy.cn/news/375701.html

相关文章

Java学习--弹球游戏

用Java写的一个简单的小游戏&#xff0c;叫弹球游戏。使用AWT画图技术及Timer定时器实现。 Java可以用于开发一些动画。所谓动画&#xff0c;就是间隔一定时间&#xff08;通常小于0.1秒&#xff09;重新绘制新的图像&#xff0c;两次绘制的图像之间差异较小。肉眼看起来就成了…

用python编写一个弹球游戏

用python编写一个简单的弹球游戏 这是学习python时用来练习的一个项目&#xff0c;作为笔记。 最终是实现一个简单的弹球游戏&#xff0c;效果图如下&#xff1a; 源代码&#xff1a; #无限命版的弹球游戏python代码 from tkinter import * # 来源于python的标准库&#x…

基于Python中Tkinter库实现弹球游戏

文章目录 前言游戏玩法程序设计思路问题修改问题代码展示修改后代码展示 完整代码 前言 在看一本Python书&#xff0c;正好有一个弹球游戏。书中的代码可以跑&#xff0c;就是有一个问题。若球遇到边界&#xff0c;就不弹回。正好发现一篇文章&#xff0c;解决了书中代码的问题…

python弹球游戏实验报告_Python实现的弹球小游戏示例

本文实例讲述了Python实现的弹球小游戏。分享给大家供大家参考&#xff0c;具体如下&#xff1a; 弹球 1. Ball 类 draw负责移动Ball 碰撞检测&#xff0c;反弹&#xff0c;Ball检测Paddle 2.Paddle类 draw负责移动Paddle 碰撞检测&#xff0c;确定能不能继续 监听键盘事件 3.主…

python中的pygame弹球游戏代码_python pygame实现挡板弹球游戏

学了一天pygame&#xff0c;用python和pygame写一个简单的挡板弹球游戏 GitHub&#xff1a; # -*- coding:utf-8 -*- from sys import exit import pygame from pygame.locals import * pygame.init() # 创建窗口 ScreenWidth 500 ScreenHright 720 ScreenSize (ScreenWidth…

游戏开发实战之弹球游戏

文/Steffen Itterheim、Andreas Lw 为了更好地使用Box2D物理引擎&#xff0c;本文我们将制作一个真实的弹球游戏。弹球游戏桌利用各种物理世界的效果来创造有趣的体验。然而&#xff0c;在使用物理引擎时&#xff0c;并不局限于真实世界中的物理定律。 通过设定合适的摩擦力、弹…

弹球游戏java怎么设置分数_Java实现简单的弹球游戏

简单的弹球游戏 该程序主要是用于对java图形化界面编程进行联系&#xff0c;程序实现全部采用的是AWT包下的类。 程序仅做参考&#xff0c;供学习使用。 import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.Fr…

C++ 简易弹球游戏(分块解释、源码、注释)

模拟一定范围内用挡板使小球不下落的经典小游戏。 目录 宽高在此修改 设置小球可能出现的形状 其他全局参数 建立挡板类 建立小球类 实现与挡板碰撞 考虑小球间的相互碰撞 实现小球移动 打印界面 主程序 完整源代码及注释 首先需要初始化游戏参数&#xff1a;宽度…