Commit 9328ccce by BellCodeEditor

save project

parent c46afe5a
Showing with 48 additions and 9 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
pygame.mixer.init()
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
...@@ -9,12 +9,24 @@ class Block(pygame.sprite.Sprite): ...@@ -9,12 +9,24 @@ class Block(pygame.sprite.Sprite):
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height 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() # 初始化 pygame.init() # 初始化
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数) FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片 # 载入图片
pygame.display.set_caption("Monkey King Running Game") pygame.display.set_caption("Monkey King Running Game")
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png') # 路
...@@ -26,6 +38,8 @@ hero = [pygame.image.load('hero1.png'), ...@@ -26,6 +38,8 @@ hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero3.png'), pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'), pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')] pygame.image.load('hero5.png')]
over = pygame.image.load("gameover.png")
text = pygame.font.Font("STKAITI.TTF",18)
high = 30 high = 30
a = 0 #悟空造型变量 a = 0 #悟空造型变量
...@@ -41,6 +55,14 @@ background_x = 0 ...@@ -41,6 +55,14 @@ background_x = 0
time = 0 time = 0
bikl = True
score = 0
SD = 10
sdd = 0
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
...@@ -52,7 +74,12 @@ while True: ...@@ -52,7 +74,12 @@ while True:
if jump == "running": if jump == "running":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jump = "up" jump = "up"
player = Player(hero[a])
if bikl == True:
if jump == "up": if jump == "up":
player.rect.y = kingy
if high > 0: if high > 0:
kingy -= high kingy -= high
high -= 2 high -= 2
...@@ -61,6 +88,7 @@ while True: ...@@ -61,6 +88,7 @@ while True:
jump = "down" jump = "down"
if jump == "down": if jump == "down":
player.rect.y = kingy
if high <= 30: if high <= 30:
kingy += high kingy += high
high += 2 high += 2
...@@ -69,11 +97,9 @@ while True: ...@@ -69,11 +97,9 @@ while True:
jump = "running" jump = "running"
high = 30 high = 30
screen.blit(background, (background_x, 0)) screen.blit(background, (background_x, 0))
screen.blit(road, (rode_x, 500)) screen.blit(road, (rode_x, 500))
screen.blit(hero[a], (150, kingy)) screen.blit(player.image, (150, kingy))
time += 1 time += 1
if time >= 60: if time >= 60:
...@@ -85,11 +111,25 @@ while True: ...@@ -85,11 +111,25 @@ while True:
for sprite in name_list: for sprite in name_list:
sprite.rect.x -= 8 sprite.rect.x -= SD
screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y)) screen.blit(sprite.image,(sprite.rect.x,sprite.rect.y))
if sprite.rect.x <= 0 - sprite.rect.width: if sprite.rect.x <= 0 - sprite.rect.width:
sprite.kill() 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)) # screen.blit(face.image,(face.rect.x,face.rect.y))
# if face.rect.x <= 0 - face.rect.width: # if face.rect.x <= 0 - face.rect.width:
# face = Block(stone,cacti,apple) # face = Block(stone,cacti,apple)
...@@ -98,7 +138,7 @@ while True: ...@@ -98,7 +138,7 @@ while True:
if rode_x<=-1000: if rode_x<=-1000:
rode_x = 0 rode_x = 0
else: else:
rode_x-= 8 rode_x-= SD
if background_x <= -1000: if background_x <= -1000:
background_x = 0 background_x = 0
else: else:
...@@ -108,8 +148,8 @@ while True: ...@@ -108,8 +148,8 @@ while True:
else: else:
a = 0 a = 0
T = text.render("SCORE:" + str(score),True,(0,0,0))
screen.blit(T,(880,20))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(30) FPS.tick(30)
import pygame
\ No newline at end of file
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