Commit 881a8236 by BellCodeEditor

save project

parent a47e4729
Showing with 35 additions and 41 deletions

70.1 KB | W: | H:

6.23 KB | W: | H:

grass.png
grass.png
grass.png
grass.png
  • 2-up
  • Swipe
  • Onion skin
......@@ -2,47 +2,51 @@ import pygame
from pygame import locals
import random
pygame.init() # 初始化
# 创建一个窗口
pygame.init()
screen = pygame.display.set_mode((1000, 600))
FPS = pygame.time.Clock() # pygame时钟,控制游戏速度(帧数)
# 载入图片
background = pygame.image.load('bg.png') # 背景
road = pygame.image.load('road.png') # 路
stone = pygame.image.load('stone.png') # 石头
cacti = pygame.image.load('cacti.png') # 仙人掌
FPS = pygame.time.Clock()
background = pygame.image.load('bg.png')
road = pygame.image.load('road.png')
stone = pygame.image.load('stone.png')
cacti = pygame.image.load('cacti.png')
bush = pygame.image.load('bush.png')
hole = pygame.image.load('hole.png')
grass = pygame.image.load('grass.png') # 灌木丛
grass = pygame.image.load('grass.png')
heroes = [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'),]
##流程
state = "moving"
i=0
y=400
t=30
road_x=0
bg_x=0
class Block(pygame.sprite.Sprite):
def __init__(self,img1,img2,img3,img4,img5):
super().__init__()
self.image = random.choice([img1,img2,img3,img4,img5])
self.rect=self.image.get_rect()
self.rect.x=1000+self.rect.width
self.rect.y=500-self.rect.height
blocks=Block(grass,hole,stone,bush,cacti)
block=random.choice([grass,hole,stone,cacti,bush])
rect=block.get_rect()
rect.x=1000+rect.width
rect.y=500-rect.height
clock=random.choice([grass,hole,stone,cacti,bush])
crect=clock.get_rect()
crect.x=1000+crect.width+random.randint(100,400)
if crect.x-rect.x<70 or crect.x-rect.x>-70:
crect.x+=100
crect.y=500-crect.height
while True:
##Keys
for event in pygame.event.get():
if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit()
if event.type == locals.KEYDOWN:
if state=="moving" and event.key==locals.K_UP:
state = "up"
if state=="down" and event.key==locals.K_DOWN:
pass
##Shadow
if blocks.rect.x-350<=50:
state="up"
##Action
if state=="up":
if t>0:
y-=t
......@@ -56,36 +60,25 @@ while True:
else:
state="moving"
t-=2
# 将背景图画上去
bg_x-=10
if bg_x<-1000:
bg_x=0
screen.blit(background, (bg_x, 0))
road_x-=30
if road_x<-1000:
road_x=0
screen.blit(road, (road_x, 500))
screen.blit(heroes[i], (150, y))
if i<4 and state == "moving":
i+=1
else:
i=0
if rect.x<0-rect.width:
block=random.choice([stone,cacti,bush])
rect=block.get_rect()
rect.x=1000+rect.width
rect.y=500-rect.height
if crect.x<0-crect.width:
clock=random.choice([stone,cacti,bush])
crect=clock.get_rect()
crect.x=1000+crect.width+random.randint(100,400)
if crect.x-rect.x<70 or crect.x-rect.x>-70:
crect.x+=100
crect.y=500-crect.height
rect.x-=30
screen.blit(block,(rect.x,rect.y))
crect.x-=random.randint(5,20)
screen.blit(clock,(crect.x,crect.y))
# 刷新画面
if blocks.rect.x<0-blocks.rect.width:
blocks=Block(grass,hole,stone,bush,cacti)
##Play
blocks.rect.x-=30
screen.blit(background, (bg_x, 0))
screen.blit(road, (road_x, 500))
screen.blit(heroes[i], (150, y))
screen.blit(blocks.image,(blocks.rect.x,blocks.rect.y))
pygame.display.update()
FPS.tick(20)
\ 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