Commit 7cf60527 by BellCodeEditor

save project

parent ba14b6dd
Showing with 25 additions and 7 deletions
import pygame 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))
...@@ -18,11 +17,27 @@ hero = [pygame.image.load('hero1.png'), ...@@ -18,11 +17,27 @@ hero = [pygame.image.load('hero1.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')]
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
class Player(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
group=pygame.sprite.Group()
index = 0 index = 0
y = 400 y = 400
jumpState = "runing" jumpState = "runing"
t = 30 t = 30
obstacle = random.choice([bush, stone, cacti])
rect = obstacle.get_rect() rect = obstacle.get_rect()
rect.x = 1000 rect.x = 1000
rect.y = 500 - rect.height rect.y = 500 - rect.height
...@@ -55,7 +70,7 @@ while True: ...@@ -55,7 +70,7 @@ while True:
t =30 t =30
# 悟空造型 # 悟空造型
wukong = image.wukong(hero[index]) wukong = hero[index]
if jumpState == "runing": # 跑步状态下 if jumpState == "runing": # 跑步状态下
index += 1 index += 1
if index >= 5: if index >= 5:
...@@ -63,7 +78,7 @@ while True: ...@@ -63,7 +78,7 @@ while True:
# 将背景图画上去 # 将背景图画上去
screen.blit(background, (0, 0)) # 远处背景 screen.blit(background, (0, 0)) # 远处背景
screen.blit(road, (changex, 500)) # 路 screen.blit(road, (changex, 500)) # 路
screen.blit(wukong, (150, y)) # 悟空 screen.blit(wukong.image, (150, y)) # 悟空
if rect.x <= 0-rect.width: # 障碍物消失 if rect.x <= 0-rect.width: # 障碍物消失
# 创建障碍物对象 # 创建障碍物对象
obstacle = random.choice([bush,stone,cacti]) obstacle = random.choice([bush,stone,cacti])
...@@ -71,7 +86,10 @@ while True: ...@@ -71,7 +86,10 @@ while True:
rect.x = 1000 rect.x = 1000
rect.y = 500 - rect.height rect.y = 500 - rect.height
rect.x -= 8 rect.x -= 8
screen.blit(obstacle, (rect.x, rect.y)) for sprite in group:
screen.blit(obstacle, (rect.x, rect.y))
pygame.sprite.collide_rect()
# 刷新画面 # 刷新画面
pygame.display.update() pygame.display.update()
FPS.tick(30) FPS.tick(30)
\ No newline at end of file
\ 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