Commit f6f84259 by BellCodeEditor

auto save

parent d807163f
Showing with 61 additions and 40 deletions
...@@ -2,72 +2,92 @@ import pygame ...@@ -2,72 +2,92 @@ import pygame
from pygame import locals from pygame import locals
import random import random
pygame.init() # 鍒濆鍖? pygame.init() # 鍒濆鍖?
# 鍒涘缓涓€涓獥鍙?
class Block(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
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("悟空跑酷") pygame.display.set_caption("鎮熺┖閰疯窇")
# 杞藉叆鍥剧墖 # 杞藉叆鍥剧墖
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') # 浠欎汉鎺?
apple = pygame.image.load('bush.png') # 鐏屾湪涓? bush = pygame.image.load('bush.png') # 鐏屾湪涓L?
hero = [pygame.image.load('hero1.png'), hero = [pygame.image.load('hero1.png'),
pygame.image.load('hero2.png'), pygame.image.load('hero2.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')]
index = 0 index = 0
y=400 y = 400
jump="runing" jumpState = "runing"
t=30 t = 30
obstacle = random.choice([bush, stone, cacti])
rect = obstacle.get_rect bg_x _= 0
rect.x = 1000 road_x = 0
rect.y = 500-rect.height time=0rreg6e64
# 鍒涘缓闅滅鐗?
Block_list=pygame.sprite.Group()
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 jump =="runing": if jumpState == "runing":
if event.key == locals.K_SPACE: if event.key == locals.K_SPACE:
jump="up" jumpState = "up"
if jump=="up":
if t>0:
y-=t
t-=2
if jumpState == "up": # 璧疯烦鐘舵€?
if t > 0:
y -= t
t -= 2
else: else:
jump="down" jumpState = "down"
if jump=="down": if jumpState == "down": # 闄嶈惤鐘舵€?
if t<=30: if t <= 30:
y+=t y += t
t+=2 t += 2
else: else:
jump="runing" jumpState = "runing"
t=30 t =30
wukong = hero[index] # 鎮熺┖閫犲瀷
if jump=="runing": wukong = hero[index]
if jumpState == "runing": # 璺戞鐘舵€佷笅
index += 1 index += 1
if index == 5: if index >= 5:
index = 0 index = 0
# 灏嗚儗鏅浘鐢讳笂鍘? # 灏嗚儗鏅浘鐢讳笂鍘?
screen.blit(background, (0, 0)) bg_x -= 1
screen.blit(road, (0, 500)) if bg_x<=-1000:
screen.blit(wukong, (150,y)) bg_x=0
if rect.x<0-rect.width:
obstacle = random.choice([bush, stone, cacti]) screen.blit(background, (bg_x))
rect = obstacle.get_rect road_x -= 8
rect.x = 1000 if road_x<=-1000:
rect.y = 500-rect.height road_x = 0
rect.x-=8 screen.blit(road, (road_x, 500))
screen.blit(obstacle, (rect.x,rect.y)) screen.blit(wukong, (150, y)) # 鎮熺┖
obstacle = Block(bush,cacti,stone)
Block_list.add(obstacle)
screen.blit(obstacle.image, (obstacle.rect.x, obstacle.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