python3 pygame實(shí)現(xiàn)接小球游戲
更新時(shí)間:2019年05月14日 08:32:05 作者:Higashino_Keigo
這篇文章主要為大家詳細(xì)介紹了python3 pygame實(shí)現(xiàn)接小球游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了python3 pygame接小球游戲的具體代碼,供大家參考,具體內(nèi)容如下
操作方法:鼠標(biāo)操作
截圖:
直接放代碼:
# -*- coding:utf-8 -*- import sys,pygame,random #導(dǎo)入庫(kù) from pygame.locals import * def print_text(font,x,y,text,color=(255,255,255)): imgText = font.render(text,True,color) # 創(chuàng)建字體,三個(gè)參數(shù)是文本.抗鋸齒.顏色 screen.blit(imgText,(x,y)) #built screen 創(chuàng)建文本窗口 pygame.init() #init 初始化 #窗口設(shè)置 screen = pygame.display.set_mode((600,500))#screen-size 窗口大小設(shè)置 pygame.display.set_caption('BallFall') #title 窗口標(biāo)題 font1 = pygame.font.Font(None,24) #font,size 字體類型(None為pygame默認(rèn)字體).字體大小 pygame.mouse.set_visible(False) #mouse-visible 光標(biāo)可視 #顏色設(shè)置 white = 255,255,255 #rgb red = 220,50,50 yellow = 230,230,50 blue = 0,0,100 #計(jì)數(shù)設(shè)置 lives = 3 #初始生命 score = 0 #初始分?jǐn)?shù) #初始化設(shè)置 game_over = True #游戲結(jié)束判斷 mouse_x = mouse_y = 0 #光標(biāo)初始化 pos_x = 300 #擋板位置初始化 pos_y = 460 bomb_x = random.randint(0,500) #小球位置隨機(jī)初始化 bomb_y = -50 #小球下落高度初始化 vel_y = 0.3 #小球下落速度 while True: for event in pygame.event.get(): #事件判斷 if event.type == QUIT: pygame.quit() sys.exit() elif event.type == MOUSEMOTION: #鼠標(biāo)運(yùn)動(dòng) mouse_x,mouse_y = event.pos elif event.type == MOUSEBUTTONUP: #鼠標(biāo)抬起 if game_over: game_over = False lives = 3 score = 0 keys = pygame.key.get_pressed() #獲取鍵盤 if keys[K_ESCAPE]: #鍵盤右上角esc鍵 pygame.quit() sys.exit() screen.fill(blue) #背景顏色 if game_over: print_text(font1,100,200,'click to play') else: #判斷小球運(yùn)行軌跡 bomb_y += vel_y if bomb_y > 500: #fallen bomb_x = random.randint(0,500) #小球隨機(jī)出現(xiàn) bomb_y = -50 lives -= 1 if lives == 0: game_over = True elif bomb_y > pos_y: if bomb_x > pos_x and bomb_x < pos_x + 120: score += 1 bomb_x = random.randint(0,500) bomb_y = -50 pygame.draw.circle(screen,yellow,(bomb_x,int(bomb_y)),30,0) #繪制圓形 五個(gè)參數(shù)為屏幕.顏色.位置.實(shí)心半徑.空心半徑 pos_x = mouse_x #擋板位置變化設(shè)置 if pos_x < 0: pos_x = 0 elif pos_x > 500: pos_x = 500 pygame.draw.rect(screen,red,(pos_x,pos_y,120,40),0) #繪制矩形 參數(shù)跟圓形一樣 print_text(font1,0,0,'Lives:' + str(lives)) #文字顯示 print_text(font1,500,0,'Score:' + str(score)) pygame.display.update() #更新
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python pygame實(shí)現(xiàn)五子棋小游戲
- python實(shí)現(xiàn)飛機(jī)大戰(zhàn)游戲(pygame版)
- python使用pygame模塊實(shí)現(xiàn)坦克大戰(zhàn)游戲
- python實(shí)現(xiàn)五子棋游戲(pygame版)
- python使用pygame框架實(shí)現(xiàn)推箱子游戲
- Python加pyGame實(shí)現(xiàn)的簡(jiǎn)單拼圖游戲?qū)嵗?/a>
- 使用Python第三方庫(kù)pygame寫個(gè)貪吃蛇小游戲
- python pygame 憤怒的小鳥(niǎo)游戲示例代碼
- Python3+Pygame實(shí)現(xiàn)射擊游戲完整代碼
- python游戲庫(kù)pygame經(jīng)典教程(推薦!)
相關(guān)文章
python中hasattr()、getattr()、setattr()函數(shù)的使用
這篇文章主要介紹了python中hasattr()、getattr()、setattr()函數(shù)的使用方法,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08