Commit 09cd21c0 by BellCodeEditor

auto save

parent eb56baaf
Showing with 17 additions and 11 deletions
import pygame import pygame
from pygame import locals from pygame import locals
import random import random
pygame.init() # 初始化
class Block(pygame.sprite.Sprite): class Block(pygame.sprite.Sprite):
def __init__(self,image1,image2,image3): def __init__(self,image1,image2,image3):
super().__init__() super().__init__()
...@@ -16,7 +16,6 @@ class player(pygame.sprite.Sprite): ...@@ -16,7 +16,6 @@ class player(pygame.sprite.Sprite):
self.rect = self.image.get_rect() self.rect = self.image.get_rect()
self.rect.x = 150 self.rect.x = 150
self.rect.y = 400 self.rect.y = 400
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() # pygame时钟,控制游戏速度(帧数)
...@@ -51,13 +50,15 @@ while True: ...@@ -51,13 +50,15 @@ while True:
jumpState = "up" jumpState = "up"
if jumpState == "up": # 起跳状态 if jumpState == "up": # 起跳状态
if t > 0: if t > 0:
y-= t y -= t
wukong.rect.y = y
t -= 2 t -= 2
else: else:
jumpState = "down" jumpState = "down"
if jumpState == "down": # 降落状态 if jumpState == "down": # 降落状态
if t <= 30: if t <= 30:
y += t y += t
wukong.rect.y = y
t += 2 t += 2
else: else:
jumpState = "runing" jumpState = "runing"
...@@ -79,14 +80,20 @@ while True: ...@@ -79,14 +80,20 @@ while True:
lu_x=0 lu_x=0
screen.blit(road, (lu_x, 500)) # 路 screen.blit(road, (lu_x, 500)) # 路
screen.blit(wukong.image, (150, y)) # 悟空 screen.blit(wukong.image, (150, y)) # 悟空
time += 1
if time >= 60:
time = 0
num = ramdon.randint(0,50)
if num >20:
obstacle=Block(bush,cacti,stone)
block_list.add(obstacle)
for sprit in block_list:
obstacle.rect.x -= 8
screen.blit(obstacle.image, (obstacle.rect.x, obstacle.rect.y))
if obstacle.rect.x <= 0-obstacle.rect.width:
sprite.kill() # 障碍物消失
if pygame.sprite.collide_rect(wukong,sprite):
if obstacle.rect.x <= 0-obstacle.rect.width: # 障碍物消失
# 创建障碍物对象
obstacle=Block(bush,cacti,stone)
obstacle.rect.x -= 8
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