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 ...@@ -2,47 +2,51 @@ import pygame
from pygame import locals from pygame import locals
import random import random
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()
# 载入图片
background = pygame.image.load('bg.png') # 背景 background = pygame.image.load('bg.png')
road = pygame.image.load('road.png') # 路 road = pygame.image.load('road.png')
stone = pygame.image.load('stone.png') # 石头 stone = pygame.image.load('stone.png')
cacti = pygame.image.load('cacti.png') # 仙人掌 cacti = pygame.image.load('cacti.png')
bush = pygame.image.load('bush.png') bush = pygame.image.load('bush.png')
hole = pygame.image.load('hole.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'),] 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" state = "moving"
i=0 i=0
y=400 y=400
t=30 t=30
road_x=0 road_x=0
bg_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: while True:
##Keys
for event in pygame.event.get(): for event in pygame.event.get():
if event.type == locals.QUIT: if event.type == locals.QUIT:
# 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: if event.type == locals.KEYDOWN:
if state=="moving" and event.key==locals.K_UP: if state=="moving" and event.key==locals.K_UP:
state = "up" state = "up"
if state=="down" and event.key==locals.K_DOWN: if state=="down" and event.key==locals.K_DOWN:
pass pass
##Shadow
if blocks.rect.x-350<=50:
state="up"
##Action
if state=="up": if state=="up":
if t>0: if t>0:
y-=t y-=t
...@@ -56,36 +60,25 @@ while True: ...@@ -56,36 +60,25 @@ while True:
else: else:
state="moving" state="moving"
t-=2 t-=2
# 将背景图画上去
bg_x-=10 bg_x-=10
if bg_x<-1000: if bg_x<-1000:
bg_x=0 bg_x=0
screen.blit(background, (bg_x, 0))
road_x-=30 road_x-=30
if road_x<-1000: if road_x<-1000:
road_x=0 road_x=0
screen.blit(road, (road_x, 500))
screen.blit(heroes[i], (150, y))
if i<4 and state == "moving": if i<4 and state == "moving":
i+=1 i+=1
else: else:
i=0 i=0
if rect.x<0-rect.width: if blocks.rect.x<0-blocks.rect.width:
block=random.choice([stone,cacti,bush]) blocks=Block(grass,hole,stone,bush,cacti)
rect=block.get_rect()
rect.x=1000+rect.width ##Play
rect.y=500-rect.height blocks.rect.x-=30
if crect.x<0-crect.width: screen.blit(background, (bg_x, 0))
clock=random.choice([stone,cacti,bush]) screen.blit(road, (road_x, 500))
crect=clock.get_rect() screen.blit(heroes[i], (150, y))
crect.x=1000+crect.width+random.randint(100,400) screen.blit(blocks.image,(blocks.rect.x,blocks.rect.y))
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))
# 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(20) 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