Commit a0351b05 by BellCodeEditor

save project

parent 212505d6
Showing with 25 additions and 9 deletions
...@@ -4,11 +4,11 @@ import random ...@@ -4,11 +4,11 @@ import random
pygame.init() # 初始化 pygame.init() # 初始化
class Zhangai(pygame.sprite.Sprite): class Zhangai(pygame.sprite.Sprite):#子类Zhangai继承父类精灵
def __init__ (self,image1,image2,image3): def __init__ (self,image1,image2,image3):#形参,后面再用实参
super().__init__() super().__init__()#继承
self.image = random.choice([stone,cacti,bush]) self.image = random.choice([image1,image2,image3])#随机选,后面用实参
self.rect =self.image.get_rect() self.rect =self.image.get_rect()#前面要加self
self.rect.x = 1000 self.rect.x = 1000
self.rect.y = 500 - self.rect.height self.rect.y = 500 - self.rect.height
...@@ -36,7 +36,10 @@ t = 30 ...@@ -36,7 +36,10 @@ t = 30
road_x = 0 road_x = 0
bg_x = 0 bg_x = 0
zhangai = Zhangai(stone,cacti,bush) time = 0
#zhangai = Zhangai(stone,cacti,bush)#设置变量
zhangai_list = pygame.sprite.Group()#创建精灵组
while True: while True:
for event in pygame.event.get(): for event in pygame.event.get():
...@@ -79,10 +82,22 @@ while True: ...@@ -79,10 +82,22 @@ while True:
screen.blit(road, (road_x, 500)) screen.blit(road, (road_x, 500))
screen.blit(wukong, (150, y)) screen.blit(wukong, (150, y))
if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口 time+=1
if time >=60:
r = random.randint(0,100)
if r >40:
zhangai = Zhangai(stone,cacti,bush) zhangai = Zhangai(stone,cacti,bush)
zhangai.rect.x -=8#左移 zhangai_list.add(zhangai)
screen.blit(zhangai.image, (zhangai.rect.x, zhangai.rect.y))#渲染 time = 0
for prop in zhangai_list:
prop.rect.x -=8
screen.blit(prop.image, (prop.rect.x, prop.rect.y))#渲染
if prop.rect.x <= 0-prop.rect.width:
prop.kill()
#if zhangai.rect.x <= 0-zhangai.rect.width:#障碍物完全消失在窗口
# zhangai = Zhangai(stone,cacti,bush)#刷新
#zhangai.rect.x -=8 #左移
#screen.blit(zhangai.image, (zhangai.rect.x, zhangai.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