Python弹球小游戏

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

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


# -*- coding: utf-8 -*-
# @Author  : Codeooo
# @Time    : 2022/04/29


import sys
import time
import random
import pygame as pg

print("""				欢迎使用Codeooo游戏平台

	1.登录账号密码,正确直接进入2,若输入3次也可以进入,但提示游客身份进入。
	2.系统产生1-20随机数,猜对直接进入3,或猜错6次也可以进入,但提示未通关。
	3.接小球游戏,每三次速度加快,分数翻倍。

		********谢谢大家观看*******
	""")


def game_login():
    count = 0
    while count < 3:
        name = str(input("请输入帐号"))
        passwd = str(input("请输入密码"))
        if (name != "codeooo" or passwd != "666"):
            count += 1
            s = 3 - count
            print("输入错误,还剩%d次机会\n" % s)
            if s == 0:
                print("您是游客身份登录")
        else:
            print("尊敬的VIP Codeooo 登录成功,直接进入游戏\n")
            break


def game_random():
    count = 0
    number = random.randint(1, 20)
    print("""		######系统将要产生1-20随机数######
          #########猜对直接进入游戏###############
         ########猜大会提示大,猜小提示小了########
        ###猜错6次也可以进入游戏,但本次游戏未通关####
    
        """)
    print(number)
    while True:
        num = int(input("请输入您要猜的数"))
        count += 1
        if (count <= 6):
            if (num == number):
                print("您通关了,总共输入了%d次\n" % (count))
                print("成功,进入下一个游戏\n")
                break
            elif (num < number):
                print("您输入小了,请再猜猜看\n")
            else:
                print("您输入大了,请再猜猜看\n")
        else:
            print("""	  ******本关未通关*********
                  *******输入次数已经达到6次***
                *********进入下一个游戏************
    
                    """)
            break


def 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 = 0
    font = pg.font.SysFont('arial', 70)
    ball_x = random.randint(20, 580)  # 用random模块生成一个随机数,不让球固定定义两个变量来保存球的位置,球的半径定义为20
    ball_y = 20  # 球在y轴的变量
    move_x = 1  # 通过一个变量将值保存下来,通过改变变值得大小来改变球的速度
    move_y = 1
    point = 1
    count = 0
    print("\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_y
        if ball_x <= 20 or ball_x >= 580:
            move_x = -move_x  # 将加改为减就是向反方向移动
        if ball_y <= 20:
            move_y = -move_y
        elif mouse_x - 20 < ball_x < mouse_x + 120 and ball_y >= 470:
            move_y = -move_y
            score += point  # 需要一个变量来保存每次加的点数
            count += 1
            if count == 3:  # 需要一个变量来保存每次接的次数
                count = 0  # 将其重置为0
                point += point
                if move_x > 0:
                    move_x += 1
                else:
                    move_x -= 1
                move_y -= 1
        elif ball_y >= 480 and (ball_x <= mouse_x - 20 or ball_x >= mouse_x + 120):
            print("游戏结束")
            time.sleep(3)
            break
        pg.display.update()  # 更新窗口
        time.sleep(0.005)  # 如果感觉慢的话,自己可以调


def run():
    game_login()
    game_random()
    boll_game()


if __name__ == '__main__':
    run()

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
青葱年少的头像青葱年少普通用户
上一篇 2023年7月29日
下一篇 2023年7月29日

相关推荐