用户:
春嵐查看:0 回复:0 评论:0 创建时间:2022-02-19T13:05:52
【作品展示】

【作品介绍】
滑雪
【作品源代码】
import pygame
import random
import time
pygame.init()
keep_going = True
cnt = 0
timer = pygame.time.Clock()
failed = pygame.mixer.Sound("failed.wav")
failed_screen = pygame.image.load("失败.png")
pygame.mixer.music.load("背景音乐.mp3")
font = pygame.font.SysFont("SimHei", 30)
pygame.mixer.music.play(-1)
class Cacti():
def __init__(self):
self.img = pygame.image.load("障碍物.png")
self.img = pygame.transform.scale(
self.img, (self.img.get_width()//4, self.img.get_height()//4))
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.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)
self.rect.center = (self.x+self.img.get_width()/2,
self.y+self.img.get_height()/2)
return (self.x, self.y)
class Cat():
def __init__(self):
self.img = pygame.image.load("滑雪.png")
self.img = pygame.transform.scale(
self.img, (self.img.get_width()//5, self.img.get_height()//5))
self.x = 100
self.y = 300
self.y_speed = 0
self.jumpSpeed = -12
self.gravity = 0.8
self.isJumping = False
self.rect = self.img.get_rect()
self.rect.center = (self.x+self.img.get_width()/2,
self.y+self.img.get_height()/2)
self.score = 0
self.isLanded = False
def jump(self):
if self.y == 100:
self.isJumping = True
self.isLanded = False
self.y_speed = self.jumpSpeed
def get_poseition(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
self.isLanded = True
self.rect.center = (self.x+self.img.get_width()/2,
self.y+self.img.get_height()/2)
return (self.x, self.y)
def Crush(self, Obj):
if self.rect.colliderect(Obj.rect):
failed.play()
return True
def Pass(self, Obj):
if (Obj.x < self.x or Obj.x > 1000) and (self.isLanded == True):
self.score += 1
self.isLanded = False
cacti = Cacti()
codemao = Cat()
while True:
if 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:
codemao.jump()
screen.blit(codemao.img, codemao.get_poseition())
screen.blit(cacti.img, cacti.get_position())
if codemao.Crush(cacti):
screen.blit(failed_screen, (350, 60))
keep_going = False
codemao.Pass(cacti)
txet = font.render("得分:" + str(codemao.score), True, (0, 0, 0))
screen.blit(txet, (800, 20))
pygame.display.update()
cnt += 1
timer.tick(70)
else:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE or event.key == pygame.K_UP:
codemao.score = 0
cacti.x = 2000
codemao.isLanded = False
codemao.isJumping = False
codemao.y = 100
keep_going = True
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn