Python簡單版本flappy bird

2021-08-22 07:28:19 字數 3736 閱讀 5733

import pygame

from pygame.locals import *

from sys import exit

import random

# 螢幕寬度

screenwidth = 288

# 螢幕高度

screenheight = 512

images = {}

# 背景位址

background_path = 'back_ground.png'

pipe_path = 'pipe.png'

base_path = 'base.png'

player_path = (

'bird2_0.png',

'bird2_1.png',

'bird2_2.png',

)# 初始化

pygame.init()

# 建立視窗

screen = pygame.display.set_mode((screenheight, screenheight))

# 設定視窗標題

# 載入,透明用convert_alpha,不透明用convert

images['background'] = pygame.image.load(background_path).convert()

images['base'] = pygame.image.load(base_path).convert_alpha()

images['bird'] = (

pygame.image.load(player_path[0]).convert_alpha(),

pygame.image.load(player_path[1]).convert_alpha(),

pygame.image.load(player_path[2]).convert_alpha(),

)images['pipe'] = (

pygame.transform.rotate(pygame.image.load(pipe_path).convert_alpha(), 180),

pygame.image.load(pipe_path).convert_alpha()

)basey = screenheight * 0.82

# 設定幀率

fps = 30

fpsclock = pygame.time.clock()

pipe_width = images['pipe'][0].get_width()

pipe_height = images['pipe'][0].get_height()

player_width = images['bird'][0].get_width()

player_height = images['bird'][0].get_height()

pipegapsize = 100 # 兩個水管之間的距離

x = screenwidth//2

y = screenheight//2

move_x = 0

move_y = 0

flap = 0 # 小鳥初始狀態

pipevelx = -4 # 管道x方向的速度

playervely = 0 # 小鳥y方向的初速度

playermaxvely = 10 # 小鳥y方向的最大速度

playerminvely = -8 # 小鳥y方向的最小速度

playeraccy = 2 # 小鳥y方向的下降加速度

playerflapacc = -3 # 小鳥y方向的上公升加速度

playery = int((screenheight - player_height)/2)

# 隨機移動柱子

def getrandompipe():

# 兩個水管之間的距離有如下變數

gapys = [20, 30, 40, 50, 60, 70, 80, 90]

index = random.randint(0, len(gapys) - 1)

gapy = gapys[index]

gapy += int(basey * 0.2)

# 水管x座標

pipex = screenwidth + 10

return [

, # 上面水管的左上角位置

, # 下面水管的左上角位置

]newpipel = getrandompipe()

upperpipes = [

]lowerpipes = [

]while true:

for event in pygame.event.get():

if event.type == quit:

exit()

elif event.type == keydown:

if event.key == k_left:

move_x = -3

elif event.key == k_right:

move_x = 3

elif event.key == k_up:

move_y = -3

elif event.key == k_down:

move_y = 3

elif event.type == keyup:

move_x = 0

move_y = 0

x = x + move_x

y = y + move_y

# 防止衝出邊界

if x > screenwidth:

x = 0

elif x < 0:

x = screenwidth

if y > screenheight:

y = 0

elif y < 0:

y = screenheight

# 貼圖在左上角

screen.blit(images['background'], (0, 0)) # 背景

# 顯示水管

for upipe, lpipe in zip(upperpipes, lowerpipes):

screen.blit(images['pipe'][0], (upipe['x'], upipe['y']))

screen.blit(images['pipe'][1], (lpipe['x'], lpipe['y']))

# 放小鳥

screen.blit(images['bird'][flap], (x, y))

flap = flap + 1

if flap % 3 == 0:

flap = 0

for upipe, lpipe in zip(upperpipes, lowerpipes):

upipe['x'] += pipevelx

lpipe['x'] += pipevelx

# 當水管移動到某一位置的時候,生成新的水管

if 0 < upperpipes[0]['x'] < 5:

newpipe = getrandompipe()

# 如果水管從右往左移動到邊緣,則摧毀水管

if upperpipes[0]['x'] < -pipe_width:

# 佇列頭出隊

upperpipes.pop(0)

lowerpipes.pop(0)

# 重新整理畫面

pygame.display.update()

fpsclock.tick(fps)

python 指定執行版本,python 版本

具體 檢視python位置 which python 先備份 1 sudo cp usr bin python usr bin python cp 刪除 2 sudo rm usr bin python 預設設定成python3.5,建立鏈結 3 sudo ln s usr bin python3....

python版本更新 python 版本公升級

python 版本公升級 公升級python 檢視python的版本 python v python 2.6.6 wget 解壓tar xvf python 2.7.12.tgz 安裝cd python 2.7.12 configure prefix usr local python2.7 make...

快排簡單版本

快排的思想很簡單,但是要手寫,有些細節可能要想一陣子。看到一種相對於傳統快排更簡單的方式 public class quick private static int partition int array,int l,int r private static void swap int array,...