#————————————————————————————————————————————————模块——————————————————————————————————————————#
import random as r
import pygame as py
py.init()
#————————————————————————————————————————————————变量——————————————————————————————————————————#
map_w = 288
map_h = 512
FPS = 60
frame = 0
gameScreen = py.display.set_mode((map_w,map_h))
py.display.set_caption("Flappy Bird")
clock = py.time.Clock()
随机数 = r.randint(1,9)
bird_y = map_h // 2
bird_x = map_w // 2 - 17
bird_mid = py.image.load("images/蓝鸟1.png")
bird_up = py.image.load("images/蓝鸟3.png")
bird_down = py.image.load("images/蓝鸟2.png")
conduit = py.image.load("images/管道身.png")
Conduit = py.image.load("images/管道头.png")
ground = py.image.load("images/地面.png")
pips = [[450,r.randint(1,7)],[650,r.randint(1,7)],[850,r.randint(1,7)]]
pipsud = [[pips[0][1]*32+24,(pips[0][1]+6)*32],[pips[1][1]*32+24,(pips[1][1]+6)*32],[pips[2][1]*32+24,(pips[2][1]+6)*32]]
scores = [0,0,0]
background=py.image.load("images/早晨.png")
ground_x = [0,288]
gameover = py.image.load("images/游戏结束.png")
getready = py.image.load("images/开始.png")
startkey = py.image.load("images/开始键.png")
block = py.image.load("images/列表.png")
startkey_rect = startkey.get_rect()
标题 = py.image.load("images/标题.png")
教程 = py.image.load("images/游戏教程.png")
num = [py.image.load(f"images/{j}.png") for j in range(10)]
s = 0
start = False
game = True
ready = False
jm = True
score = 0
#————————————————————————————————————————————————函数定义——————————————————————————————————————————#
def gameLoop():
global 随机数
global bird_y,ready,s,yv
yv = 1
while True:
if jm:
_jm()
gameScreen.blit(startkey,(map_w // 2 - 52,300))
else:
if game and ready:
yv += 1
bird_y += yv
安全()
gameScreen.blit(background,(0,0))
if ready :
画鸟(bird_x,bird_y)
画管()
if s < 60 and game:
_ready()
s += 1
else :
画鸟(bird_x,map_h//2)
地面()
if not ready :
_教程()
if not game:
Gameover()
计分(score)
else:
_score()
随机数 = r.randint(0,7)
clock.tick(FPS)
key()
py.display.update()
def 画鸟(x,y):
global frame
if game:
if 0 <= frame <= 20:
gameScreen.blit(bird_up,(x,y))
elif 20 <= frame <= 40:
gameScreen.blit(bird_mid,(x,y))
else:
gameScreen.blit(bird_down,(x,y))
frame+=1
if frame == 60:frame = 0
else:gameScreen.blit(bird_mid,(x,y))
def 画管():
global pips
global 随机数,score,scores
for n in range(len(pips)):
if game:
if pips[n][0] <- 50:
pips.append([550,随机数])
pipsud.append([随机数*32,(随机数+6)*32])
scores.append(0)
del pips[0]
del pipsud[0]
del scores[0]
else:
pips[n][0] -= 3
for m in range(pips[n][1]):
gameScreen.blit(conduit,(pips[n][0],m * 32))
for m in range(pips[n][1] + 6,16):
gameScreen.blit(conduit,(pips[n][0],m*32))
gameScreen.blit(Conduit,(pips[n][0] - 2,pips[n][1] * 32))
gameScreen.blit(Conduit,(pips[n][0] - 2,(pips[n][1] + 6) * 32))
if pips[n][0] < bird_x:
if scores[n] == 0 :
score += 1
scores[n] = 1
def 地面():
global ground_x
if game:
if ground_x[0] < -288:
ground_x[0] += 576
else:
ground_x[0] -= 3
if ground_x[1] < -288:
ground_x[1] += 576
else:
ground_x[1]-=3
gameScreen.blit(ground,(ground_x[0],424))
gameScreen.blit(ground,(ground_x[1],424))
def 安全():
global game,h
for n in range(3):
if bird_y > 400 or ((pips[n][0] <= (bird_x + 34) and bird_x <= (pips[n][0]+52)) and
not (bird_y > pipsud[n][0] and (bird_y +24) < pipsud[n][1])):
game = False
h = map_h//2
def Gameover():
global h
gameScreen.blit(gameover,(map_w // 2 - 96,h))
if h > 99:
h -= 10
return
gameScreen.blit(block,(map_w // 2 - 113,200))
def _ready():
gameScreen.blit(getready,(map_w // 2 - 96,100))
def _start(pos):
global jm,startkey_rect
a = map_w // 2 - 52
b = 300
c = a <= pos[0] >= a + startkey_rect[0]
d = b <= pos[1] >= b + startkey_rect[1]
if c and d:
jm = False
def _教程():
gameScreen.blit(教程,(map_w // 2 - 57,350))
def _jm():
gameScreen.fill((170,200,230))
gameScreen.blit(标题,(map_w // 2 - 89,100))
画鸟(bird_x,200)
def _score():
k = len(str(score))*24 / 2
j = 0
for i in str(score):
gameScreen.blit(num[int(i)],(map_w // 2 - k + 24 * j,100))
j += 1
def 计分(score):
with open("score.txt","r+") as score_w:
score_w.write(f"{score}\n")
score_lines = score_w.readlines()
n = 0
for s in score_lines:
del score_lines[n]
score_lines.insert(n,int(s))
n += 1
score_lines.sort()
with open("score.txt","w") as score_w:
for w in score_lines :
score_w.write(f"{w}\n")
def key():
global yv,ready,jm
for event in py.event.get():
if event.type == py.QUIT:
py.quit()
return
elif event.type == py.MOUSEBUTTONDOWN:
if not jm:
ready = True
yv = -10
elif event.type == py.MOUSEBUTTONUP:
if jm:
mouse_pos = py.mouse.get_pos()
_start(mouse_pos)
else:
if event.type == py.KEYDOWN:
if event.key == py.K_q:
py.quit()
return
elif event.key == py.K_SPACE:
yv = -10
#————————————————————————————————————————————————主程序——————————————————————————————————————————#
if __name__ == "__main__":
gameLoop()
你这程序太长了,估计大多数人都没耐心看,更别说找bug了😂(个人观点,不喜勿喷)
ps:很想看看你的运行情况😇
错误是py.display.update()
pygame.error: video system not initialized
就是不知道哪里错了