import pygame, sys
import random
class Person(): ?# 人物
? ? def __init__(self, surf=None, y=None):
? ? ? ? self.surface = surf
? ? ? ? self.y = y ?# y坐標(biāo)
? ? ? ? self.w = (surf.get_width()) / 12 ?# 寬度
? ? ? ? self.h = surf.get_height() / 2 ?# 高度
? ? ? ? self.cur_frame = -1 ?# 當(dāng)前的運(yùn)動(dòng)狀態(tài)幀
? ? ? ? self.state = 0 ?# 0代表跑步狀態(tài),1代表跳躍狀態(tài),2代表連續(xù)跳躍
? ? ? ? self.gravity = 1 ?# 重力加速度
? ? ? ? self.velocity_y = 0 ?# y方向的速度
? ? ? ? self.vy_start = -20 ?# 起跳開(kāi)始速度
? ? def getPos(self): ? # 獲取當(dāng)前的位置信息,用于碰撞檢測(cè)
? ? ? ? return (0, self.y + 12, self.w, self.h)
class Obstacle(object): ?# 障礙物
? ? def __init__(self, surf, x=0, y=0):
? ? ? ? self.surface = surf
? ? ? ? self.x = x
? ? ? ? self.y = y
? ? ? ? self.w = surf.get_width()
? ? ? ? self.h = surf.get_height()
? ? ? ? self.cur_frame = random.randint(0, 6) ?# 隨機(jī)獲取一種障礙物的類型
? ? ? ? self.w = 100
? ? ? ? self.h = 100
? ? def getPos(self): ?# 當(dāng)前的坐標(biāo)信息
? ? ? ? return (self.x, self.y, self.w, self.h)
? ? def judgeCollision(self, rect1, rect2): ?# 碰撞檢測(cè)
? ? ? ? if (rect2[0] >= rect1[2] - 20) or (rect1[0] + 40 >= rect2[2]) or (rect1[1] + rect1[3] < rect2[1] + 20) or (
? ? ? ? ? ? ? ? rect2[1] + rect2[3] < rect1[1] + 20):
? ? ? ? ? ? return False
? ? ? ? return True
class BackGround(object): ?# 背景
? ? def __init__(self, surf):
? ? ? ? self.surface = surf ?# 初始化一個(gè)Surface 對(duì)象
? ? ? ? self.dx = -10
? ? ? ? self.w = surf.get_width() ? ?# 返回 Surface 對(duì)象的寬度,以像素為單位。
? ? ? ? self.rect = surf.get_rect() ?# 獲取 Surface 對(duì)象的矩形區(qū)域
class PaoKu(object):
? ? def __init__(self):
? ? ? ? pygame.init()
? ? ? ? pygame.mixer.init()
? ? ? ? self.width = 1200 ?# 窗口寬度
? ? ? ? self.height = 500 ?# 窗口高度
? ? ? ? self.size = (self.width, self.height)
? ? ? ? self.screen = pygame.display.set_mode(self.size)
? ? ? ? pygame.display.set_caption('跑酷運(yùn)動(dòng)')
? ? ? ? self.score = 0 ?# 分?jǐn)?shù)
? ? ? ? self.font1 = pygame.font.Font("resource/simkai.ttf", 32)
? ? ? ? self.font2 = pygame.font.Font("resource/simkai.ttf", 64) ?# 字體
? ? ? ? self.obstacle_pic = pygame.image.load("resource/obstacles.png").convert_alpha() ?# 障礙物圖片
? ? ? ? self.game_over = pygame.image.load("resource/gameover.bmp").convert_alpha() ?# 游戲結(jié)束圖片
? ? ? ? self.bg = BackGround(pygame.image.load("resource/bg.png").convert_alpha()) ?# 背景對(duì)象
? ? ? ? self.person = Person(pygame.image.load("resource/person.png").convert_alpha(), 500 - 85) ?# 人物對(duì)象
? ? ? ? self.screen.blit(self.bg.surface, [0, 0]) ?# 初始化游戲背景
? ? ? ? self.obstacle_list = [] ?# 障礙物對(duì)象數(shù)組
? ? ? ? self.game_state = 0 ?# 游戲狀態(tài),0表示游戲中,1表示游戲結(jié)束
? ? ? ? self.life = 3 ? ? ? ?# 初始的生命值
? ? ? ? self.clock = pygame.time.Clock() ?# 時(shí)鐘
? ? ? ? self.bg_music = pygame.mixer.Sound(r"resource\bgm.wav").play(-1, 0) ?# 循環(huán)播放北京音樂(lè)
? ? def startGame(self, screen): ? # 開(kāi)始游戲界面
? ? ? ? gameStart = pygame.image.load("resource/start1new.png")
? ? ? ? screen.blit(gameStart, (0, 0))
? ? ? ? font = pygame.font.SysFont("resource/simkai.ttf", 70)
? ? ? ? tip = font.render("Press Any Key To Start!, Press Esc To Quit", True, (65, 105, 225))
? ? ? ? screen.blit(tip, (self.width / 2 - 550, self.height / 2 + 150))
? ? ? ? pygame.display.update()
? ? ? ? while True:
? ? ? ? ? ? for event in pygame.event.get(): ?# 關(guān)閉窗口
? ? ? ? ? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? ? ? ? ? self.terminate()
? ? ? ? ? ? ? ? elif event.type == pygame.KEYDOWN:
? ? ? ? ? ? ? ? ? ? if (event.key == pygame.K_ESCAPE): ?# 按下ESC鍵
? ? ? ? ? ? ? ? ? ? ? ? self.terminate()
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ? return
? ? def addObstacle(self): ? # 添加障礙物
? ? ? ? rate = 4
? ? ? ? # 是否生成障礙物
? ? ? ? if not random.randint(0, 300) < rate:
? ? ? ? ? ? return
? ? ? ? y = random.choice([self.height - 100, self.height - 200, self.height - 300, self.height - 400])
? ? ? ? obstacle = Obstacle(self.obstacle_pic, self.width + 40, y)
? ? ? ? self.obstacle_list.append(obstacle)
? ? # 監(jiān)聽(tīng)鍵盤事件,并做處理
? ? def ListenKeyBoard(self): ?# 鍵盤事件處理
? ? ? ? for event in pygame.event.get():
? ? ? ? ? ? if event.type == pygame.QUIT:
? ? ? ? ? ? ? ? sys.exit()
? ? ? ? ? ? elif event.type == pygame.KEYDOWN:
? ? ? ? ? ? ? ? # 空格鍵跳躍
? ? ? ? ? ? ? ? if self.game_state == 0:
? ? ? ? ? ? ? ? ? ? if event.key == pygame.K_SPACE:
? ? ? ? ? ? ? ? ? ? ? ? pygame.mixer.Sound(r"resource\jump.wav").play()
? ? ? ? ? ? ? ? ? ? ? ? if self.person.state == 0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.person.state = 1
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.person.velocity_y = self.person.vy_start
? ? ? ? ? ? ? ? ? ? ? ? elif self.person.state == 1:
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.person.state = 2
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.person.velocity_y = self.person.vy_start
? ? ? ? ? ? ? ? elif self.game_state == 1:
? ? ? ? ? ? ? ? ? ? if event.key == pygame.K_RETURN: ? # 重新開(kāi)始游戲
? ? ? ? ? ? ? ? ? ? ? ? self.bg_music.stop()
? ? ? ? ? ? ? ? ? ? ? ? self.__init__()
? ? ? ? if self.game_state == 0:
? ? ? ? ? ? # BackGorund的運(yùn)動(dòng)
? ? ? ? ? ? self.bg.dx += 10
? ? ? ? ? ? if self.bg.dx == 1200:
? ? ? ? ? ? ? ? self.bg.dx = 0
? ? ? ? ? ? ? ? # Person的移動(dòng)
? ? ? ? ? ? if self.person.state == 0:
? ? ? ? ? ? ? ? self.person.cur_frame += 1
? ? ? ? ? ? ? ? if self.person.cur_frame == 12:
? ? ? ? ? ? ? ? ? ? self.person.cur_frame = 0
? ? ? ? ? ? else:
? ? ? ? ? ? ? ? self.person.y += self.person.velocity_y
? ? ? ? ? ? ? ? self.person.velocity_y += self.person.gravity
? ? ? ? ? ? ? ? if self.person.y >= 500 - 85:
? ? ? ? ? ? ? ? ? ? self.person.y = 500 - 85
? ? ? ? ? ? ? ? ? ? self.person.state = 0
? ? ? ? ? ? # bstacle的操作
? ? ? ? ? ? self.addObstacle()
? ? ? ? ? ? for obstacle in self.obstacle_list:
? ? ? ? ? ? ? ? obstacle.x -= 10# obstacle向左移動(dòng)十個(gè)像素
? ? ? ? ? ? ? ? if obstacle.x + obstacle.w <= 0: ?# 當(dāng)obstacle離開(kāi)界面時(shí)
? ? ? ? ? ? ? ? ? ? self.obstacle_list.remove(obstacle)
? ? ? ? ? ? ? ? ? ? self.score += 10 ?# 避開(kāi)obstacle,加10分
? ? ? ? ? ? ? ? if obstacle.judgeCollision(self.person.getPos(), obstacle.getPos()): ?# 碰撞檢測(cè)
? ? ? ? ? ? ? ? ? ? if obstacle.cur_frame == 6:
? ? ? ? ? ? ? ? ? ? ? ? self.obstacle_list.remove(obstacle)
? ? ? ? ? ? ? ? ? ? ? ? self.score += 100 ?# 吃金幣加100分
? ? ? ? ? ? ? ? ? ? ? ? coin_sound = pygame.mixer.Sound(
? ? ? ? ? ? ? ? ? ? ? ? ? ? r"resource/coin.wav")
? ? ? ? ? ? ? ? ? ? ? ? coin_sound.play()
? ? ? ? ? ? ? ? ? ? else:
? ? ? ? ? ? ? ? ? ? ? ?self.life -= 1
? ? ? ? ? ? ? ? ? ? ? ?self.obstacle_list.remove(obstacle)
? ? ? ? ? ? ? ? ? ? ? ?if self.life <= 0:
? ? ? ? ? ? ? ? ? ? ? ? ? ? self.game_state = 1 ?# 游戲失敗
? ? ? ? ? ? ? ? ? ? ? ?die_sound = pygame.mixer.Sound(
? ? ? ? ? ? ? ? ? ? ? ? ? ?r"resource\die.wav") ?# 添加碰撞之后產(chǎn)生的音效
? ? ? ? ? ? ? ? ? ? ? ?die_sound.play()
? ? # 更新顯示界面
? ? def updateScreen(self, screen):
? ? ? ? ? ? screen.blit(self.bg.surface, [-self.bg.dx, 0]) ?# 背景的貼圖
? ? ? ? ? ? screen.blit(self.bg.surface, [1200 - self.bg.dx, 0])
? ? ? ? ? ? text = self.font1.render("score:%d" % self.score, True, (128, 128, 128)) ?# 分?jǐn)?shù)的貼圖
? ? ? ? ? ? screen.blit(text, (500, 20))
? ? ? ? ? ? del text
? ? ? ? ? ? rest_life = self.font1.render("life:%d" % self.life, True, (128, 128, 128)) ?# 剩余生命
? ? ? ? ? ? screen.blit(rest_life, (400, 20))
? ? ? ? ? ? del rest_life
? ? ? ? ? ? screen.blit(self.person.surface, [0, self.person.y], [int(self.person.cur_frame) * self.person.w, 0, self.person.w, self.person.h]) ?# 人物的貼圖
? ? ? ? ? ? for obstacle in self.obstacle_list: ?# 障礙物的貼圖
? ? ? ? ? ? ? ? screen.blit(obstacle.surface, [obstacle.x, obstacle.y],
? ? ? ? ? ? ? ? ? ? ? ? ? ? [int(obstacle.cur_frame) * obstacle.w, 0, obstacle.w, obstacle.h])
? ? # 判斷游戲的狀態(tài)
? ? def judgeState(self, screen):
? ? ? ? if self.game_state == 0:
? ? ? ? ? ? self.updateScreen(screen)
? ? ? ? ? ? return
? ? ? ? elif self.game_state == 1:
? ? ? ? ? ? screen.blit(self.game_over, [0, 0])
? ? ? ? ? ? text = self.font1.render("GameOver Score:%d ?Press Enter to restart" % self.score, True, (255, 0, 0))
? ? ? ? ? ? screen.blit(text, (self.width / 2 - 350, self.height / 2 + 150))
? ? # 游戲結(jié)束
? ? def terminate(self):
? ? ? ? pygame.quit()
? ? ? ? sys.exit()
? ? # 游戲主入口函數(shù)
? ? def main(self):
? ? ? ? self.startGame(self.screen)
? ? ? ? while True:
? ? ? ? ? ? self.clock.tick(40) ?# 設(shè)置時(shí)鐘頻率
? ? ? ? ? ? self.judgeState(self.screen)
? ? ? ? ? ? self.ListenKeyBoard()
? ? ? ? ? ? pygame.display.flip()
if __name__ == '__main__':
? ? paoku = PaoKu()
? ? paoku.main()
素材包:
person
?start1new
startgame1?
?
?startgame2
bg
?gameover
?文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-471457.html
?obstacles文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-471457.html
到了這里,關(guān)于跑酷游戲源代碼的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!