猫史档案馆


【Python作品分享】跑酷【作品秀】

用户:被编程逼疯的墨Itto被编程逼疯的墨Itto查看:0 回复:0 评论:0 创建时间:2021-05-29T00:47:25


【作品展示】

center_image

 

【作品介绍】

作品未完成呢,发出来测试一下(#`O′)轻喷,谢谢

 

【作品源代码】

import pygame
import random
pygame.init()
keep_going = True

cnt = 0
timer = pygame.time.Clock()
failed = pygame.mixer.Sound("failed.wav")

class Cacti():
    def __init__(self):
        self.img = pygame.image.load("仙人掌.png")
        self.x = 1000 + random.randint(200, 500)
        self.y = 300 - self.img.get_height()
        self.speed_x = -20
        self.rect = self.img.get_rect()
        self.rect.center = (self.x+self.img.img.get_width()/2,self.y+self.img.get_height()/2)

    def get_position(self):
        self.x += self.speed_x
        #判断是否超过左边界,若超过则重置位置
        if self.x < -1 * self.img.get_width():
            self.x = 1000 + random.randint(200, 500)
        return(self.x, self.y)
class Cat():
    def __init__(self):
        self.pose_list = [pygame.image.load(
            "cat_{}.png".format(i))for i in range(29)]
        self.x = 100
        self.y = 100
        self.y_speed = 0
        self.jumpSpeed = -10
        self.gravity = 0.8
        self.isJumping = False
        self.rect = self.pose_list[0].get_rect()

        self.rect.center = (self.x+self.img.img.get_width() /
                            2, self.y+self.img.get_height()/2)

    def jump(self):
        if self.y == 100:
            self.y_speed = self.jumpSpeed
            self.isJumping = True

    def get_position(self):
        if self.isJumping:
            self.y += self.y_speed
            self.y_speed += self.gravity
        if self.y > 100:
            self.y = 100
            self.isJumping = False
            self.y_speed = 0
            print("落地啦!")
        return(self.x, self.y)
        
codemao = Cat()
cacti = Cacti()

while keep_going:
    screen = pygame.display.set_mode([1000,300])
    bg_color = (255, 255, 255)
    screen.fill(bg_color)

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            keep_going = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
                print("编程猫跳~")
                codemao.jump()
    screen.blit(codemao.pose_list[cnt % 29], codemao.get_position())
    screen.blit(cacti.img, cacti.get_position())
    pygame.display.update()
    cnt += 1
    timer.tick(70)

pygame.quit()
    

 

【提示】

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

https://python.codemao.cn


回复

上一页1 页 / 共 0下一页