========给在校的小妹妹做个游戏玩:.

弹珠游戏主要是靠坐标xy,接板长度,球的半径等决定:


# -*- coding: utf-8 -*-# @Author: Codeooo# @Time: 2022/04/29import sysimport timeimport randomimport pygame as pgprint("""欢迎使用Codeooo游戏平台1.登录账号密码,正确直接进入2,若输入3次也可以进入,但提示游客身份进入。2.系统产生1-20随机数,猜对直接进入3,或猜错6次也可以进入,但提示未通关。3.接小球游戏,每三次速度加快,分数翻倍。********谢谢大家观看*******""")def game_login():count = 0while count < 3:name = str(input("请输入帐号"))passwd = str(input("请输入密码"))if (name != "codeooo" or passwd != "666"):count += 1s = 3 - countprint("输入错误,还剩%d次机会\n" % s)if s == 0:print("您是游客身份登录")else:print("尊敬的VIP Codeooo 登录成功,直接进入游戏\n")breakdef game_random():count = 0number = random.randint(1, 20)print("""######系统将要产生1-20随机数###############猜对直接进入游戏############### ########猜大会提示大,猜小提示小了###########猜错6次也可以进入游戏,但本次游戏未通关####""")print(number)while True:num = int(input("请输入您要猜的数"))count += 1if (count <= 6):if (num == number):print("您通关了,总共输入了%d次\n" % (count))print("成功,进入下一个游戏\n")breakelif (num < number):print("您输入小了,请再猜猜看\n")else:print("您输入大了,请再猜猜看\n")else:print("""******本关未通关****************输入次数已经达到6次************进入下一个游戏************""")breakdef boll_game():pg.init()# 对模块进行初始化操作game_window = pg.display.set_mode((600, 500))# 画窗口,用方法,这个方法可以生成一个游戏窗口,里面的参数需要给一个元组,元组的两个元素分别是窗口的宽和高pg.display.set_caption('接球')# 标题window_color = (0, 0, 255)# 蓝色rgb元组里面的元素,用rgb来表示ball_color = (255, 165, 0)# 黄色的rgb值rect_color = (255, 0, 0)score = 0font = pg.font.SysFont('arial', 70)ball_x = random.randint(20, 580)# 用random模块生成一个随机数,不让球固定定义两个变量来保存球的位置,球的半径定义为20ball_y = 20# 球在y轴的变量move_x = 1# 通过一个变量将值保存下来,通过改变变值得大小来改变球的速度move_y = 1point = 1count = 0print("\n")print("游戏开始\n")while True:game_window.fill(window_color)# 传递参数for event in pg.event.get():# 可退出,这是一个状态if event.type == pg.QUIT:#sys.exit()# sys模块里面的方法mouse_x, mouse_y = pg.mouse.get_pos()# 用来接收鼠标返回的xy坐标pg.draw.circle(game_window, ball_color, (ball_x, ball_y), 20)#pg.draw.rect(game_window, rect_color, (mouse_x, 490, 100, 10))# rectangle的缩写,画一个矩形my_text = font.render(str(score), False, (255, 255, 255))game_window.blit(my_text, (500, 30))# 这个位置是经过调试,感觉比较合适ball_x += move_x# 每次横纵坐标都加1,这样看起来比较快,就像球在动ball_y += move_yif ball_x <= 20 or ball_x >= 580:move_x = -move_x# 将加改为减就是向反方向移动if ball_y <= 20:move_y = -move_yelif mouse_x - 20 < ball_x < mouse_x + 120 and ball_y >= 470:move_y = -move_yscore += point# 需要一个变量来保存每次加的点数count += 1if count == 3:# 需要一个变量来保存每次接的次数count = 0# 将其重置为0point += pointif move_x > 0:move_x += 1else:move_x -= 1move_y -= 1elif ball_y >= 480 and (ball_x <= mouse_x - 20 or ball_x >= mouse_x + 120):print("游戏结束")time.sleep(3)breakpg.display.update()# 更新窗口time.sleep(0.005)# 如果感觉慢的话,自己可以调def run():game_login()game_random()boll_game()if __name__ == '__main__':run()