import pygame, sys
import random
class Person(): # Person
def __init__(self, surf=None, y=None):
self.surface = surf
self.y = y # y coordinate
self.w = (surf.get_width()) / 12 # width
self .h = surf.get_height() / 2 # height
self.cur_frame = -1 # current motion state frame
self.state = 0 # 0 represents running state, 1 represents jumping state, 2 represents continuous jumping
self.gravity = 1 # Gravity acceleration
self.velocity_y = 0 # Velocity in the y direction
self.vy_start = -20 # Jump start velocity
def getPos(self): # Get the current position information for collision detection
return (0, self.y + 12, self.w, self.h)
class Obstacle(object): # Obstacle
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) # Obtain a type of obstacle randomly
self.w = 100
self.h = 100
def getPos(self): # Current coordinate information
return (self.x, self.y, self.w, self.h)
def judgeCollision(self, rect1, rect2): # 碰撞检测
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): # Background
def __init__(self, surf):
self.surface = surf # Initialize a Surface object
self.dx = -10
self.w = surf.get_width() # Return the width of the Surface object in pixels as the unit.
self.rect = surf.get_rect() # Get the rectangular area of the Surface object
class PaoKu(object):
def __init__(self):
pygame.init()
pygame.mixer.init()
self.width = 1200 # window width
self.height = 500 # window height
self.size = (self.width, self.height)
self.screen = pygame.display.set_mode(self.size)
pygame.display.set_caption(' parkour')
self.score = 0 # score
self.font1 = pygame.font.Font("resource/simkai.ttf", 32)
self.font2 = pygame.font.Font("resource/simkai.ttf", 64) # font
self.obstacle_pic = pygame.image.load("resource/obstacles.png").convert_alpha() # obstacle image
self.game_over = pygame.image.load("resource/gameover.bmp").convert_alpha () # Game over image
self.bg = BackGround(pygame.image.load("resource/bg.png").convert_alpha()) # Background object
self.person = Person(pygame.image.load("resource/person.png").convert_alpha(), 500 - 85) # Person object
self.screen.blit(self.bg.surface, [0, 0]) # Initialize the game background
self.obstacle_list = [] # array of obstacle objects
self.game_state = 0 # game state, 0 means the game is in, 1 means the game is over
self.life = 3 # initial life value
self.clock = pygame.time.Clock() # Clock
self.bg_music = pygame.mixer.Sound(r"resource\bgm.wav").play(-1, 0) # Play Beijing music in a loop
def startGame(self, screen): # 开始游戏界面
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(): # 关闭窗口
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): # Add obstacles
rate = 4
# Whether to generate obstacles
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)
# Listen for keyboard events , and do processing
def ListenKeyBoard(self): #Keyboard event processing
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: # restart the game
self.bg_music.stop()
self.__init__()
if self.game_state == 0:
# BackGorund的运动
self.bg.dx += 10
if self.bg.dx == 1200:
self.bg.dx = 0
# Person的移动
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 moves ten pixels to the left
if obstacle.x + obstacle.w <= 0: # When the obstacle leaves the interface
self.obstacle_list.remove(obstacle)
self.score += 10 # Avoid the obstacle, add 10 points
if obstacle.judgeCollision(self.person.getPos (), obstacle.getPos()): # Collision detection
if obstacle.cur_frame == 6:
self.obstacle_list.remove(obstacle)
self.score += 100 #Eat gold coins plus 100 points
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 # Game failed
die_sound = pygame.mixer.Sound(
r"resource\die.wav") # Add the sound effect after collision
die_sound. play()
# Update the display interface
def updateScreen(self, screen):
screen.blit(self.bg.surface, [-self.bg.dx, 0]) # Background texture
screen.blit(self.bg.surface, [1200 - self.bg.dx, 0])
text = self.font1.render("score:%d" % self.score, True, (128, 128, 128)) # score texture
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])
# 判断游戏的状态
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))
# 游戏结束
def terminate(self):
pygame.quit()
sys.exit()
# Game main entry function
def main(self):
self.startGame(self.screen)
while True:
self.clock.tick(40) # Set clock frequency
self.judgeState(self.screen)
self.ListenKeyBoard()
pygame.display .flip()
if __name__ == '__main__':
paoku = PaoKu()
paoku.main()
Material packaging:
person
start1new
startgame1
startgame2
bg
gameover
obstacles