Commit ad779672 by BellCodeEditor

auto save

parent 022ec8df
Showing with 110 additions and 8 deletions
import pygame
from pygame import locals
import random
pygame.init() # 初始化
# 创建一个窗口
screen = pygame.display.set_mode((1000, 600))
pygame.display.set_caption('悟空跑酷')
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
font = pygame.font.SysFont('华文隶书',30)
class Zhangai(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3):
super().__init__()
self.image = random.choice([image1,image2,image3])
self.rect = self.image.get_rect()
self.rect.x = 1000
self.rect.y = 500-self.rect.height
self.score = 1
class Wukong(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
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
apple = pygame.image.load('bush.png') # 灌木丛
hero = pygame.image.load('hero1.png')
hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'),
pygame.image.load('hero3.png'),
pygame.image.load('hero4.png'),
pygame.image.load('hero5.png')]
index = 0
wukong = 'running'
y = 400
t = 30
zhangai = Zhangai(stone, cacti, apple)
zhangai_group = pygame.sprite.Group()
wukong1 = Wukong(hero[index])
bg_x = 0
time = 0
gamestate = 0
score_i = 0
score = 0
speed = 10
road_x = speed
while True:
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if event.key == locals.K_SPACE:
if wukong == 'running':
wukong = 'up'
speed = 10+score//3
wukong1 = Wukong(hero[index])
if wukong == 'up':
y -= t
wukong1.rect.y = y
t = t-2
if y>=400:
wukong = 'running'
t = 30
# if wukong == 'up':
# if t > 0 :
# y -= t
# t -=2
# else:
# wukong = 'down'
# if wukong == 'down':
# if t <= 30 :
# y += t
# t += 2
# else:
# wukong= 'running'
# t = 30
# 将背景图画上去
screen.blit(background, (0, 0))
screen.blit(road, (0, 500))
screen.blit(hero, (150, 400))
if gamestate == 0:
screen.blit(background, (bg_x, 0))
screen.blit(road, (road_x, 500))
screen.blit(wukong1.image, (150, y))
if wukong =='running':
index +=1
if index >4:
index = 0
# 刷新画面
time +=1
if time>=60:
time = 0
num = random.randint(0,60)
if num<=30:
zhangai = Zhangai(stone, cacti, apple)
zhangai_group.add(zhangai)
for i in zhangai_group:
i.rect.x-= speed
screen.blit(i.image,(i.rect.x,i.rect.y))
if i.rect.x <0-i.rect.width:
score_i =0
i.kill()
if pygame.sprite.collide_rect(wukong1,i):
over = pygame.image.load('gameover.png')
screen.blit(over,(400,200))
gamestate = 1
else:
if wukong1.rect.x>i.rect.x:
score += i.score
i.score = 0
info = font.render('分数:'+str(score),True,(255,0,255))
screen.blit(info,(800,20))
print(speed)
# zhangai.rect.x -= 8
# if zhangai.rect.x<0-zhangai.rect.width:
# zhangai.rect.x = 1000
# zhangai.rect.y = 400
# zhangai = Zhangai(stone, cacti, apple)
road_x -=speed
bg_x -=1
if road_x<= -1000:
road_x =0
if bg_x<=-1000:
bg_x = 0
screen.blit(zhangai.image,(zhangai.rect.x,zhangai.rect.y))
print(score)
pygame.display.update()
FPS.tick(60)
\ No newline at end of file
FPS.tick(50)
\ 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