Commit 9328ccce by BellCodeEditor

save project

parent c46afe5a
Showing with 99 additions and 60 deletions
import pygame
from pygame import locals
import random
pygame.mixer.init()
class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
......@@ -9,12 +9,24 @@ class Block(pygame.sprite.Sprite):
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500 - self.rect.height
self.score = 1
class Player(pygame.sprite.Sprite):
def __init__(self,image):
super().__init__()
self.image = image
self.rect = self.image.get_rect()
self.rect.x = 150
self.rect.y = 400
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片
pygame.display.set_caption("Monkey King Running Game")
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
......@@ -26,6 +38,8 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
over = pygame.image.load("gameover.png")
text = pygame.font.Font("STKAITI.TTF",18)
high = 30
a = 0 #悟空造型变量
......@@ -41,6 +55,14 @@ background_x = 0
time = 0
bikl = True
score = 0
SD = 10
sdd = 0
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
......@@ -52,64 +74,82 @@ while True:
if jump == "running":
if event.key == locals.K_SPACE:
jump = "up"
if jump == "up":
if high > 0:
kingy -= high
high -= 2
player = Player(hero[a])
if bikl == True:
if jump == "up":
player.rect.y = kingy
if high > 0:
kingy -= high
high -= 2
else:
jump = "down"
if jump == "down":
player.rect.y = kingy
if high <= 30:
kingy += high
high += 2
else:
jump = "running"
high = 30
screen.blit(background, (background_x, 0))
screen.blit(road, (rode_x, 500))
screen.blit(player.image, (150, kingy))
time += 1
if time >= 60:
time = 0
num = random.randint(0,50)
if num > 20:
face = Block(stone,cacti,apple)
name_list.add(face)
for sprite in name_list:
sprite.rect.x -= SD
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill()
if pygame.sprite.collide_rect(player,sprite):
screen.blit(over,(400,200))
bikl = False
else:
if sprite.rect.x + sprite.rect.width <= player.rect.x:
score+=sprite.score
sprite.score = 0
sdd += 1
if sdd == 5:
SD += 5
sdd = 0
# screen.blit(face.image,(face.rect.x,face.rect.y))
# if face.rect.x <= 0 - face.rect.width:
# face = Block(stone,cacti,apple)
# else:
# face.rect.x -= 8
if rode_x<=-1000:
rode_x = 0
else:
jump = "down"
if jump == "down":
if high <= 30:
kingy += high
high += 2
rode_x-= SD
if background_x <= -1000:
background_x = 0
else:
jump = "running"
high = 30
screen.blit(background, (background_x, 0))
screen.blit(road, (rode_x, 500))
screen.blit(hero[a], (150, kingy))
time += 1
if time >= 60:
time = 0
num = random.randint(0,50)
if num > 20:
face = Block(stone,cacti,apple)
name_list.add(face)
for sprite in name_list:
sprite.rect.x -= 8
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill()
# screen.blit(face.image,(face.rect.x,face.rect.y))
# if face.rect.x <= 0 - face.rect.width:
# face = Block(stone,cacti,apple)
# else:
# face.rect.x -= 8
if rode_x<=-1000:
rode_x = 0
else:
rode_x-= 8
if background_x <= -1000:
background_x = 0
else:
background_x -= 2
if a <=3:
a+=1
else:
a = 0
background_x -= 2
if a <=3:
a+=1
else:
a = 0
# 刷新画面
pygame.display.update()
FPS.tick(30)
import pygame
\ No newline at end of file
T = text.render("SCORE:" + str(score),True,(0,0,0))
screen.blit(T,(880,20))
# 刷新画面
pygame.display.update()
FPS.tick(30)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment