Commit 2d9a1e42 by BellCodeEditor

save project

parent 5e677b7c
Showing with 35 additions and 7 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random
pygame.init() # 初始化 pygame.init() # 初始化
# 创建一个窗口 # 创建一个窗口
screen = pygame.display.set_mode((1000, 600)) screen = pygame.display.set_mode((1000, 600))
...@@ -19,27 +19,41 @@ hero = [pygame.image.load('hero1.png'), ...@@ -19,27 +19,41 @@ hero = [pygame.image.load('hero1.png'),
index = 0 index = 0
jumpState = "runing" jumpState = "runing"
y =400 y =400
t = 30
roadx = 0
obstale = random.choice([stone,cacti,apple])
rect = obstale.get_rect() # 获取当前的x,y 宽高
print(rect.height,"ssssssssssssssssssssssss")
rect.x = 1000
rect.y = 500-rect.height
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:
# 接收到退出事件后退出程序 # 接收到退出事件后退出程序
exit() exit()
if event.type == locals.KEYDOWN: #获取是否是按键模块信息 if event.type == locals.KEYDOWN: #获取是否是按键模块信息
if jumpState == "runing":
if event.key == locals.K_SPACE: #获取是否的空格的key值 if event.key == locals.K_SPACE: #获取是否的空格的key值
jumpState = "up" jumpState = "up"
if jumpState == "up": #起跳 if jumpState == "up": #起跳
if y >150: if t >0:
y -=5 y -=t
t -=2
else: else:
jumpState = "down" jumpState = "down"
if jumpState == "down": if jumpState == "down":
if y <400: if t<=30:
y +=5 y +=t
t +=2
else: else:
jumpState = "runing" jumpState = "runing"
t =30
if jumpState == "runing":
wukong = hero[index] wukong = hero[index]
index += 1 index += 1
if index>=5: if index>=5:
...@@ -47,8 +61,21 @@ while True: ...@@ -47,8 +61,21 @@ while True:
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) screen.blit(background, (0, 0))
screen.blit(road, (0, 500)) if roadx<=-1000:
roadx = 0
road.x -=8
screen.blit(road, (road.x, 500))
screen.blit(wukong, (150, y)) screen.blit(wukong, (150, y))
if rect.x < 0-rect.width:
obstale = random.choice([stone,cacti,apple])
rect = obstale.get_rect() # 获取当前的x,y 宽高
rect.x = 1000
rect.y = 500-rect.height
rect.x -=8
screen.blit(obstale,(rect.x,rect.y))
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(60) FPS.tick(60)
\ 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