最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)移動(dòng)木板小游戲

 更新時(shí)間:2020年10月09日 08:26:28   作者:weixin_44313115  
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)移動(dòng)木板小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了python實(shí)現(xiàn)移動(dòng)木板小游戲的具體代碼,供大家參考,具體內(nèi)容如下

一、游戲簡介

本游戲是通過python編寫的小游戲,給初學(xué)者熟悉python編程語言拋磚引玉,希望有所幫助。
成型的效果圖如下:

二、編寫步驟

1.引入庫

代碼如下:

###### AUTHOR:破繭狂龍 ######
###### DATE:20201002 ######
###### DESCRIPTION:移動(dòng)的木板 ######
import pygame
from pygame.locals import *
import sys
import time
import random

2.初始化

代碼如下:

pygame.init()
BLACK = (0, 0, 0) # 黑色
WHITE = (255, 255, 255) # 白色
bg_color = (0,0,70) # 背景顏色
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)

smallText = pygame.font.SysFont('SimHei', 20) #comicsansms
midlText = pygame.font.SysFont('SimHei', 50)

barsize = [30, 10]
SCREEN_SIZE = [400, 500] # 屏幕大小
BALL_SIZE = [15, 15] # 球的尺寸
fontcolor = (255,255,255) # 定義字體的顏色

myimg = r"img\b1.jpg"
background = pygame.image.load(myimg) # 圖片位置
background = pygame.transform.scale(background, SCREEN_SIZE)

# ball 初始位置
ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2
ball_pos_y = 0

# ball 移動(dòng)方向
ball_dir_y = 1 # 1:down
ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

clock = pygame.time.Clock() # 定時(shí)器
screen = pygame.display.set_mode(SCREEN_SIZE)
# 設(shè)置標(biāo)題
pygame.display.set_caption('python小游戲-移動(dòng)木板')
# 設(shè)置圖標(biāo)
image = pygame.image.load(myimg)
pygame.display.set_icon(image)

3.相關(guān)自定義函數(shù)

代碼如下:

###### 自定義函數(shù) ######
def button(msg, x, y, w, h, ic, ac, action=None):
 mouse = pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 if x + w > mouse[0] > x and y + h > mouse[1] > y:
 pygame.draw.rect(screen, ac, (x, y, w, h))
 if click[0] == 1 and action != None:
  action()
 else:
 pygame.draw.rect(screen, ic, (x, y, w, h))
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ((x + (w / 2)), (y + (h / 2)))
 screen.blit(textSurf, textRect)

def text_objects(text, font):
 textSurface = font.render(text, True, fontcolor)
 return textSurface, textSurface.get_rect()

def quitgame():
 pygame.quit()
 quit()

def message_diaplay(text):
 largeText = pygame.font.SysFont('SimHei', 115)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((screen[0] / 2), (screen[1] / 2))
 screen.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()

4.相關(guān)自定義函數(shù)

代碼如下:

def game_first_win():
 intro = True
 while intro:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  quit()
 screen.fill(bg_color)
 ###游戲名稱
 TextSurf, TextRect = text_objects('移動(dòng)木板', midlText)
 TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
 screen.blit(TextSurf, TextRect)
 ###作者
 TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:破繭狂龍', smallText)
 TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
 screen.blit(TextSurf_ZZ, TextRect_ZZ)
 button("開始", 60, 400, 100, 50, green, bright_green, game_loop)
 button("取消", 230, 400, 100, 50, red, bright_red, quitgame)
 pygame.display.update()
 clock.tick(15)

###### 移動(dòng)的木板游戲類 ######
def game_loop():
 pygame.mouse.set_visible(1) # 移動(dòng)鼠標(biāo)不可見
 ###變量###
 score = 0 #分?jǐn)?shù)
 count_O = 0 #循環(huán)的次數(shù)變量1 用于統(tǒng)計(jì)等級(jí)
 count_N = 0 #循環(huán)的次數(shù)變量2 用于統(tǒng)計(jì)等級(jí)
 c_level = 1 #等級(jí)

 x_change = 0 #移動(dòng)的變量
 x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
 y = SCREEN_SIZE[1] - barsize[1]

 # ball 初始位置
 ball_pos_pz = ball_pos
 while True:
 bar_move_left = False
 bar_move_right = False
 ###當(dāng)每次滿X分后,升級(jí)等級(jí)
 if count_O != count_N and score % 5 == 0:
  c_level += 1
 count_O = count_N
 ###### 獲取鍵盤輸入 ######
 for event in pygame.event.get():
  if event.type == QUIT: # 當(dāng)按下關(guān)閉按鍵
  pygame.quit()
  sys.exit() # 接收到退出事件后退出程序
  elif event.type == KEYDOWN:
  ##按鍵盤Q鍵 暫停
  if event.key == pygame.K_q:
   time.sleep(10)
  ##左移動(dòng)
  if event.key == pygame.K_LEFT:
   bar_move_left = True
   x_change = -30
  else:
   bar_move_left = False
  ##右移動(dòng)
  if event.key == pygame.K_RIGHT:
   bar_move_right = True
   x_change = +30
  else:
   bar_move_right = False
  if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
   bar_move_left = False
   bar_move_right = False

  ##木板的位置移動(dòng)
  if bar_move_left == True and bar_move_right == False:
  x += x_change
  if bar_move_left == False and bar_move_right == True:
  x += x_change

 ##填充背景
 screen.blit(background, (0, 0)) # (0,0)代表圖片位置起點(diǎn)x 軸 Y軸
 ##獲取最新的木板位置,并渲染在前臺(tái)
 bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
 bar_pos.left = x
 pygame.draw.rect(screen, WHITE, bar_pos)

 ## 球移動(dòng),并渲染在前臺(tái)
 ball_pos_pz.bottom += ball_dir_y * 3
 pygame.draw.rect(screen, WHITE, ball_pos_pz)

 ## 判斷球是否落到板上
 if bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
  score += 1 # 分?jǐn)?shù)每次加1
  count_N += 1
 elif bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
  print("Game Over: ", score)
  return score

 ## 更新球下落的初始位置
 if bar_pos.top <= ball_pos_pz.bottom:
  ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
  ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

 ######### 顯示游戲等級(jí) #########
 TextSurf_lev, TextRect_lev = text_objects("等級(jí) : " + str(c_level), smallText)
 TextRect_lev.center = (60, 20)
 screen.blit(TextSurf_lev, TextRect_lev)

 ######### 顯示分?jǐn)?shù)結(jié)果 #########
 TextSurf_sco, TextRect_sco = text_objects("分?jǐn)?shù) : " + str(score), smallText)
 TextRect_sco.center = (60, 50)
 screen.blit(TextSurf_sco, TextRect_sco)

 pygame.display.update() # 更新軟件界面顯示
 clock.tick(60)

# 三、完整的代碼

代碼如下:

###### AUTHOR:破繭狂龍 ######
###### DATE:20201002 ######
###### DESCRIPTION:移動(dòng)的木板 ######
import pygame
from pygame.locals import *
import sys
import time
import random

pygame.init()
BLACK = (0, 0, 0) # 黑色
WHITE = (255, 255, 255) # 白色
bg_color = (0,0,70) # 背景顏色
red = (200, 0, 0)
green = (0, 200, 0)
bright_red = (255, 0, 0)
bright_green = (0, 255, 0)

smallText = pygame.font.SysFont('SimHei', 20) #comicsansms
midlText = pygame.font.SysFont('SimHei', 50)

barsize = [30, 10]
SCREEN_SIZE = [400, 500] # 屏幕大小
BALL_SIZE = [15, 15] # 球的尺寸
fontcolor = (255,255,255) # 定義字體的顏色

myimg = r"img\b1.jpg"
background = pygame.image.load(myimg) # 圖片位置
background = pygame.transform.scale(background, SCREEN_SIZE)

# ball 初始位置
ball_pos_x = SCREEN_SIZE[0] // 2 - BALL_SIZE[0] / 2
ball_pos_y = 0

# ball 移動(dòng)方向
ball_dir_y = 1 # 1:down
ball_pos = pygame.Rect(ball_pos_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

clock = pygame.time.Clock() # 定時(shí)器
screen = pygame.display.set_mode(SCREEN_SIZE)
# 設(shè)置標(biāo)題
pygame.display.set_caption('python小游戲-移動(dòng)木板')
# 設(shè)置圖標(biāo)
image = pygame.image.load(myimg)
pygame.display.set_icon(image)

###### 自定義函數(shù) ######
def button(msg, x, y, w, h, ic, ac, action=None):
 mouse = pygame.mouse.get_pos()
 click = pygame.mouse.get_pressed()
 if x + w > mouse[0] > x and y + h > mouse[1] > y:
 pygame.draw.rect(screen, ac, (x, y, w, h))
 if click[0] == 1 and action != None:
  action()
 else:
 pygame.draw.rect(screen, ic, (x, y, w, h))
 textSurf, textRect = text_objects(msg, smallText)
 textRect.center = ((x + (w / 2)), (y + (h / 2)))
 screen.blit(textSurf, textRect)

def text_objects(text, font):
 textSurface = font.render(text, True, fontcolor)
 return textSurface, textSurface.get_rect()

def quitgame():
 pygame.quit()
 quit()

def message_diaplay(text):
 largeText = pygame.font.SysFont('SimHei', 115)
 TextSurf, TextRect = text_objects(text, largeText)
 TextRect.center = ((screen[0] / 2), (screen[1] / 2))
 screen.blit(TextSurf, TextRect)
 pygame.display.update()
 time.sleep(2)
 game_loop()

def game_first_win():
 intro = True
 while intro:
 for event in pygame.event.get():
  if event.type == pygame.QUIT:
  pygame.quit()
  quit()
 screen.fill(bg_color)
 ###游戲名稱
 TextSurf, TextRect = text_objects('移動(dòng)木板', midlText)
 TextRect.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 - 70 ))
 screen.blit(TextSurf, TextRect)
 ###作者
 TextSurf_ZZ, TextRect_ZZ = text_objects('AUTHOR:破繭狂龍', smallText)
 TextRect_ZZ.center = ((SCREEN_SIZE[0] / 2), (SCREEN_SIZE[1] / 2 + 30))
 screen.blit(TextSurf_ZZ, TextRect_ZZ)
 button("開始", 60, 400, 100, 50, green, bright_green, game_loop)
 button("取消", 230, 400, 100, 50, red, bright_red, quitgame)
 pygame.display.update()
 clock.tick(15)

###### 移動(dòng)的木板游戲類 ######
def game_loop():
 pygame.mouse.set_visible(1) # 移動(dòng)鼠標(biāo)不可見
 ###變量###
 score = 0 #分?jǐn)?shù)
 count_O = 0 #循環(huán)的次數(shù)變量1 用于統(tǒng)計(jì)等級(jí)
 count_N = 0 #循環(huán)的次數(shù)變量2 用于統(tǒng)計(jì)等級(jí)
 c_level = 1 #等級(jí)

 x_change = 0 #移動(dòng)的變量
 x = SCREEN_SIZE[0] // 2 - barsize[0] // 2
 y = SCREEN_SIZE[1] - barsize[1]

 # ball 初始位置
 ball_pos_pz = ball_pos
 while True:
 bar_move_left = False
 bar_move_right = False
 ###當(dāng)每次滿X分后,升級(jí)等級(jí)
 if count_O != count_N and score % 5 == 0:
  c_level += 1
 count_O = count_N
 ###### 獲取鍵盤輸入 ######
 for event in pygame.event.get():
  if event.type == QUIT: # 當(dāng)按下關(guān)閉按鍵
  pygame.quit()
  sys.exit() # 接收到退出事件后退出程序
  elif event.type == KEYDOWN:
  ##按鍵盤Q鍵 暫停
  if event.key == pygame.K_q:
   time.sleep(10)
  ##左移動(dòng)
  if event.key == pygame.K_LEFT:
   bar_move_left = True
   x_change = -30
  else:
   bar_move_left = False
  ##右移動(dòng)
  if event.key == pygame.K_RIGHT:
   bar_move_right = True
   x_change = +30
  else:
   bar_move_right = False
  if event.key != pygame.K_LEFT and event.key != pygame.K_RIGHT:
   bar_move_left = False
   bar_move_right = False

  ##木板的位置移動(dòng)
  if bar_move_left == True and bar_move_right == False:
  x += x_change
  if bar_move_left == False and bar_move_right == True:
  x += x_change

 ##填充背景
 screen.blit(background, (0, 0)) # (0,0)代表圖片位置起點(diǎn)x 軸 Y軸
 ##獲取最新的木板位置,并渲染在前臺(tái)
 bar_pos = pygame.Rect(x, y, barsize[0], BALL_SIZE[1])
 bar_pos.left = x
 pygame.draw.rect(screen, WHITE, bar_pos)

 ## 球移動(dòng),并渲染在前臺(tái)
 ball_pos_pz.bottom += ball_dir_y * 3
 pygame.draw.rect(screen, WHITE, ball_pos_pz)

 ## 判斷球是否落到板上
 if bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left <= ball_pos_pz.right and bar_pos.right >= ball_pos_pz.left):
  score += 1 # 分?jǐn)?shù)每次加1
  count_N += 1
 elif bar_pos.top <= ball_pos_pz.bottom and (
  bar_pos.left > ball_pos_pz.right or bar_pos.right < ball_pos_pz.left):
  print("Game Over: ", score)
  return score

 ## 更新球下落的初始位置
 if bar_pos.top <= ball_pos_pz.bottom:
  ball_x = random.randint(0, SCREEN_SIZE[0] - BALL_SIZE[0])
  ball_pos_pz = pygame.Rect(ball_x, ball_pos_y, BALL_SIZE[0], BALL_SIZE[1])

 ######### 顯示游戲等級(jí) #########
 TextSurf_lev, TextRect_lev = text_objects("等級(jí) : " + str(c_level), smallText)
 TextRect_lev.center = (60, 20)
 screen.blit(TextSurf_lev, TextRect_lev)

 ######### 顯示分?jǐn)?shù)結(jié)果 #########
 TextSurf_sco, TextRect_sco = text_objects("分?jǐn)?shù) : " + str(score), smallText)
 TextRect_sco.center = (60, 50)
 screen.blit(TextSurf_sco, TextRect_sco)

 pygame.display.update() # 更新軟件界面顯示
 clock.tick(60)

####程序執(zhí)行順序######
game_first_win()
game_loop()
pygame.quit()

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 全面了解python字符串和字典

    全面了解python字符串和字典

    下面小編就為大家?guī)硪黄媪私鈖ython字符串和字典。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2016-07-07
  • Python綁定方法與非綁定方法詳解

    Python綁定方法與非綁定方法詳解

    這篇文章主要為大家詳細(xì) 介紹了Python綁定方法與非綁定方法的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Python多重繼承之菱形繼承的實(shí)例詳解

    Python多重繼承之菱形繼承的實(shí)例詳解

    繼承是面向?qū)ο缶幊痰囊粋€(gè)重要的方式,通過繼承,子類就可以擴(kuò)展父類的功能。這篇文章主要介紹了Python多重繼承之菱形繼承,需要的朋友可以參考下
    2020-02-02
  • Python3讀寫Excel文件(使用xlrd,xlsxwriter,openpyxl3種方式讀寫實(shí)例與優(yōu)劣)

    Python3讀寫Excel文件(使用xlrd,xlsxwriter,openpyxl3種方式讀寫實(shí)例與優(yōu)劣)

    這篇文章主要介紹了Python3讀寫Excel文件,使用xlrd,xlsxwriter,openpyxl3種方式讀寫實(shí)例與優(yōu)劣,需要的朋友可以參考下
    2020-02-02
  • python對(duì)指定字符串逆序的6種方法(小結(jié))

    python對(duì)指定字符串逆序的6種方法(小結(jié))

    這篇文章主要介紹了python對(duì)指定字符串逆序的6種方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • TensorFlow的環(huán)境配置與安裝方法

    TensorFlow的環(huán)境配置與安裝方法

    這篇文章主要介紹了TensorFlow的環(huán)境配置與安裝方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-02-02
  • Python操作列表之List.insert()方法的使用

    Python操作列表之List.insert()方法的使用

    這篇文章主要介紹了Python操作列表之List.insert()方法的使用,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • Scrapy框架實(shí)現(xiàn)的登錄網(wǎng)站操作示例

    Scrapy框架實(shí)現(xiàn)的登錄網(wǎng)站操作示例

    這篇文章主要介紹了Scrapy框架實(shí)現(xiàn)的登錄網(wǎng)站操作,結(jié)合實(shí)例形式分析了Scrapy登錄網(wǎng)站cookies方式、post請(qǐng)求方式登錄網(wǎng)站相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2020-02-02
  • Appium中scroll和drag_and_drop根據(jù)元素位置滑動(dòng)

    Appium中scroll和drag_and_drop根據(jù)元素位置滑動(dòng)

    本文主要介紹了Appium中scroll和drag_and_drop根據(jù)元素位置滑動(dòng),文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • opencv3/C++圖像像素操作詳解

    opencv3/C++圖像像素操作詳解

    今天小編就為大家分享一篇opencv3/C++圖像像素操作詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2019-12-12

最新評(píng)論

大关县| 安顺市| 福泉市| 武陟县| 肇庆市| 南靖县| 图木舒克市| 南通市| 乐都县| 哈巴河县| 西林县| 云阳县| 武陟县| 新密市| 招远市| 定南县| 乐亭县| 安康市| 宁远县| 禹州市| 措美县| 二连浩特市| 北票市| 新泰市| 仪陇县| 牟定县| 洮南市| 花垣县| 石首市| 和平县| 咸丰县| 肥城市| 石城县| 界首市| 临海市| 平山县| 库尔勒市| 宁城县| 西藏| 固阳县| 万山特区|