猫史档案馆


【Python作品分享】飞翔的小鸟【作品秀】

用户:不学编程终不还不学编程终不还查看:1 回复:3 评论:1 创建时间:2022-08-07T18:21:29


【作品展示】

center_image

 

【作品介绍】

一个python萌新做的游戏,有错误或不好的地方请指出。

懒喵了连注释都没加(:<)

 

【作品源代码】

import pygame,sys
import random
import pickle
pygame.init()

screen = pygame.display.set_mode((400,590))
pygame.display.set_caption('飞翔的小鸟')
best_handle = True
try :
    best_scor = open("bird.pkl",'rb')
except:
    best_handle = False
if best_handle:
    best_scor_q = pickle.load(best_scor)
    best_scor.close()
else:
    best_scor_q = 0

pygame.mixer.music.load("Town.mp3")
pygame.mixer.music.set_volume(1)
pygame.mixer.music.play(-1)
sound_1 = pygame.mixer.Sound("hit_wall.wav")
sound_1.set_volume(0.1)
sound_2 = pygame.mixer.Sound("game_over.wav")
sound_2.set_volume(0.1)


class Bird (pygame.sprite.Sprite):
    def __init__(self,image_load,loacation=[25,25]):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_load)
        self.rect = self.image.get_rect()
        self.rect.left,self.rect.top = loacation
        self.rect.center = loacation
    def fly (self):
        if running :
           global bird_y
           bird_y += 0.3
           self.rect.y += bird_y


class Down_Obstructions(pygame.sprite.Sprite):
    def __init__ (self,image_load,loacation=[25,25]):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_load)
        self.rect = self.image.get_rect()
        self.rect.left, self.rect.top = loacation
        self.rect.center = loacation
    def move (self):
        if running :
            global Obstructions_x
            global Obstructions_y
            Obstructions_x = Obstructions_x - 6
            self.rect.x = Obstructions_x
            self.rect.y = Obstructions_y
            if Obstructions_x <= -50:
                Obstructions_x = 420
                Obstructions_y = random.randint(200,400)
        
class UP_Obstructions(pygame.sprite.Sprite):
    def __init__ (self,image_load,loacation):
        pygame.sprite.Sprite.__init__(self)
        self.image = pygame.image.load(image_load)
        self.rect = self.image.get_rect()
        self.rect.center = loacation
    def move (self):
        self.rect.x = Obstructions_x
        self.rect.bottom = Obstructions_down.rect.top -250


scor_image_1 = pygame.image.load("新角色.png")
scor_best = pygame.image.load('新角色 (1).png')
scor_best_rect = scor_best.get_rect()
scor_image_1_rect = scor_image_1.get_rect()
scor = 0
Failure = pygame.image.load('失败.png')
running = True
bj = pygame.image.load('背景2(1).png')
bird_y = 10
bird = Bird('小鸟.png')
Obstructions_y = random.randint(200,400)
Obstructions_x = 420
Obstructions_down = Down_Obstructions('柱子下.png',[Obstructions_x,Obstructions_y])
Obstructions_up = UP_Obstructions('柱子上.png', [Obstructions_x, Obstructions_y - 850])

screen_rect = screen.get_rect()
bj_rect = bj.get_rect()
bj_rect.center = bj_rect.center
Failure_rect = Failure.get_rect()
Failure_rect.center = screen_rect.center
run = True

group = pygame.sprite.Group(Obstructions_down,Obstructions_up)
while run:
    screen.fill([255, 255, 255])
    screen.blit(bj,bj_rect)
    txt_font = pygame.font.SysFont(None, 50)
    txt_image = txt_font.render(str(scor), False, (10, 60, 60), (0, 200, 180))
    txt_rect = txt_image.get_rect()
    txt_rect.x = 250
    txt_rect.y = 17
    screen.blit(txt_image, txt_rect)

    txt_font_2 = pygame.font.SysFont(None, 45)
    txt_image_2 = txt_font_2.render(str(best_scor_q), False, (10, 60, 60), (0, 200,180 ))
    txt_rect_2 = txt_image.get_rect()
    txt_rect_2.x = 300
    txt_rect_2.y = 70
    screen.blit(txt_image_2, txt_rect_2)
    scor_best_rect.center = [150,80]
    

    screen.blit(scor_image_1, scor_image_1_rect)
    screen.blit(scor_best,scor_best_rect)
    screen.blit(bird.image, bird.rect.center)
    screen.blit(Obstructions_down.image,Obstructions_down.rect)
    screen.blit(Obstructions_up.image,Obstructions_up.rect)
    if not running:
        screen.blit(Failure,Failure_rect)
    if pygame.sprite.spritecollide(bird,group,False):
        sound_2.play()
        running = False
    if bird.rect.right == Obstructions_down.rect.right and  running:
        scor += 1
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            if scor > best_scor_q :
                pickle_file = open('bird.pkl','wb')
                pickle.dump(scor,pickle_file)
                pickle_file.close()
            run = False
        elif event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP:
                bird_y = -30
                sound_1.play()
        elif event.type == pygame.KEYUP:
            if event.key == pygame.K_UP:
                bird_y = 10
    Obstructions_down.move()
    Obstructions_up.move()
    bird.fly()
    pygame.display.flip()
    pygame.time.delay(30)
pygame.quit()

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
2507725077

好  

点赞1


评论


不学编程终不还不学编程终不还

急寻python大佬,邀请者的作品我都赞 imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E8%B0%A2%E8%B0%A2%E8%80%81%E6%9D%BF.gif"alt="emotion_编程猫_谢谢老板"

点赞0


评论


不学编程终不还不学编程终不还

总感觉操作感觉不太一样,几天后我就发个新的   imgsrc="https://static.codemao.cn/emoji/codemao/%E7%BC%96%E7%A8%8B%E7%8C%AB_%E6%90%93%E5%A4%B4.gif"alt="emotion_编程猫_搓头"

点赞0


评论