Python彈球小游戲的項目代碼
更新時間:2023年08月09日 16:40:14 作者:Codeoooo
本文主要介紹了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.系統(tǒng)產生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(""" ######系統(tǒng)將要產生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(): # 可退出,這是一個狀態(tài) 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()
到此這篇關于Python彈球小游戲的項目代碼的文章就介紹到這了,更多相關Python彈球小游戲內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!