用户:小翊_YWI查看:0 回复:1 评论:0 创建时间:2022-08-15T17:30:54
【作品展示】

【作品介绍】
玩法:任意键或鼠标键跳跃,点击X键退出如有建议,私聊我,感谢
【作品源代码】
import pygame, sys, random
class Bird(object):
def __init__(self):
self.birdRect = pygame.Rect(65, 50, 50, 50)
self.birdStatus = [pygame.image.load('./bird0_0.png'), pygame.image.load('./bird0_1.png'), pygame.image.load('./bird2_2.png')]
self.status = 0
self.birdX = 120
self.birdY = 350
self.jump = False
self.jumpSpeed = 15
self.gravity = 5
self.dead = False
def updateBird(self):
if self.jump:
self.jumpSpeed -= 1
self.birdY -= self.jumpSpeed
else:
self.gravity += 0.2
self.birdY += self.gravity
self.birdRect[1] = self.birdY
class Pipeline(object):
def __init__(self):
self.wallx = 400
self.pineUp = pygame.image.load('./pipe_down.png')
self.pineDown = pygame.image.load('./pipe_up.png')
def updatePipeline(self):
self.wallx -= 5
if self.wallx < -80:
global score, y
y = random.randint(100, 200)
score += 1
self.wallx = 400
def createMap():
screen.fill((0, 100, 0))
screen.blit(background, (0, 0))
screen.blit(Pipeline.pineUp, (Pipeline.wallx, -100+y))
screen.blit(Pipeline.pineDown, (Pipeline.wallx, 500+y))
Pipeline.updatePipeline()
if Bird.dead:
Bird.status = 2
elif Bird.jump:
Bird.status = 1
screen.blit(Bird.birdStatus[Bird.status], (Bird.birdX, Bird.birdY))
Bird.updateBird()
screen.blit(font.render(str(score), -1, (255, 25, 255)), (100, 50))
pygame.display.update()
def checkDead():
upRect = pygame.Rect(Pipeline.wallx, -100, Pipeline.pineUp.get_width() - 10, Pipeline.pineUp.get_height())
downRect = pygame.Rect(Pipeline.wallx, 500, Pipeline.pineDown.get_width() - 10, Pipeline.pineDown.get_height())
if upRect.colliderect(Bird.birdRect) or downRect.colliderect(Bird.birdRect):
Bird.dead = True
elif not 0 < Bird.birdRect[1] < height:
Bird.dead = True
return True
else:
return False
def getResult():
final_text1 = 'Game Over'
final_text2 = 'Your final score is: ' + str(score)
ft1_font = pygame.font.SysFont('Arial', 70)
ft1_surf = font.render(final_text1, 1, (242, 3, 36))
ft2_font = pygame.font.SysFont('Arial', 50)
ft2_surf = font.render(final_text2, 1, (253, 177, 6))
screen.blit(ft1_surf, [screen.get_width() / 2 - ft1_surf.get_width() / 2, 100])
screen.blit(ft2_surf, [screen.get_width() / 2 - ft2_surf.get_width() / 2, 200])
pygame.display.flip()
if __name__ == '__main__':
"""主程序"""
pygame.init()
pygame.font.init()
font = pygame.font.SysFont('Arial', 50)
size = width, height = 400, 650
global score, y
y = random.randint(100, 200)
score = 0
screen = pygame.display.set_mode(size)
clock = pygame.time.Clock()
Pipeline = Pipeline()
Bird = Bird()
# while not Bird.dead:
# for i in range(3):
# Bird.status = i
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if (event.type == pygame.KEYDOWN or event.type == pygame.MOUSEBUTTONDOWN) and not Bird.dead:
Bird.jump = True
Bird.gravity = 5
Bird.jumpSpeed = 10
background = pygame.image.load('./bg_day.png')
if checkDead():
getResult()
else:
createMap()
pygame.quit()
sys.exit()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn