用户:糖果PB查看:0 回复:0 评论:0 创建时间:2022-09-12T09:53:02
【作品展示】

【作品介绍】
Let's Play
【作品源代码】
from random import randint
import pgzrun
WIDTH = 800
HEIGHT = 600
score = 0
game_state = 'b'
box_width = [randint(40, 120), randint(40, 120),
randint(40, 120), randint(40, 120)]
box_height = [randint(20, 130), randint(20, 130),
randint(20, 130), randint(20, 130)]
box = [Rect(WIDTH + 100, randint(0, HEIGHT - box_height[0]), box_width[0], box_height[0]),
Rect(WIDTH + 250, randint(0, HEIGHT - box_height[1]), box_width[1], box_height[1]),
Rect(WIDTH + 300, randint(0, HEIGHT - box_height[2]), box_width[2], box_height[2]),
Rect(WIDTH + 350, randint(0, HEIGHT - box_height[3]), box_width[3], box_height[3])]
box2 = Rect(50, 360, 60, 60)
black_box = [Rect(WIDTH / 4, 0, 10, HEIGHT / 2 - HEIGHT / 8),
Rect(WIDTH / 4, HEIGHT - 225, 10, HEIGHT - HEIGHT / 2 - HEIGHT / 8)]
time_left = 60
state = ''
button1 = Actor('button1', (300, 300))
button2 = Actor('button2', (500, 300))
level = 1
TITLE = 'Play Box v4.2.3'
def box_value(value):
box[value].x = WIDTH + randint(20, 400)
box[value].y = randint(20, 400)
box_width[value] = randint(40, 120)
box_height[value] = randint(20, 130)
def black_box_y(y):
black_box[0].y -= y
black_box[1].y -= y
def draw():
global game_state, score, state, level, time_left
if game_state == 'b':
screen.fill('yellow')
button1.draw()
button2.draw()
elif game_state == 's':
screen.fill('yellow')
if state == '1':
screen.draw.text('Dodge Blocks', center=(400, 300),
color='dodgerblue', fontsize=100)
else:
screen.draw.text('Eliminate Blocks', center=(400, 300),
color='dodgerblue', fontsize=100)
if keyboard.space:
game_state = 'p'
music.play('bg_music3')
elif game_state == 'p':
screen.fill('gold')
screen.draw.filled_rect(box2, 'red')
screen.draw.text('Score:' + str(score), center=(75, 20),
color='dodgerblue', fontsize=50)
if state == '2':
screen.draw.text('Time Left:' + str(time_left), center=(95, 55),
color='dodgerblue', fontsize=50)
for i in range(len(box)):
screen.draw.filled_rect(box[i], 'orange')
if i == 0 or i == 1:
if level == 2:
screen.draw.filled_rect(black_box[i], 'black')
elif game_state == 'lu':
level += 1
game_state = 'p'
box2.y = 280
score = 0
time_left = 60
elif game_state == 'go':
screen.fill('gold')
screen.draw.text('Game Over', center=(400, 300),
color='dodgerblue', fontsize=100)
music.pause()
if score > 40 and level == 3:
screen.fill('gold')
screen.draw.text('You Win!', center=(400, 300),
color='dodgerblue', fontsize=100)
music.pause()
music.play_once('success')
score = 0
game_state = 'Win'
def update_time_left():
global game_state, time_left
if game_state == 'p':
if time_left > 0:
time_left -= 1
else:
game_state = 'go'
def update():
global game_state, score, state, level
if game_state == 'p':
# 躲避模式
if state == '1':
if level == 1:
for i in range(len(box)):
box[i].x -= 5 + score // 10
if box[i].x < 0 - box_height[i]:
box_value(i)
score += 1
elif box[i].colliderect(box2) or \
box2.y > 600 or \
box2.y < -60:
game_state = 'go'
if keyboard.w:
box2.y -= 5
elif keyboard.s:
box2.y += 5
elif score >= 40:
game_state = 'lu'
else:
for i in range(len(box)):
box[i].x -= 5 + score // 10
if box[i].x < 0 - box_height[i] or \
box[i].colliderect(black_box[0]) or \
box[i].colliderect(black_box[1]):
box_value(i)
score += 1
elif box[i].colliderect(box2) or \
box2.y > 600 or \
box2.y < -60:
game_state = 'go'
if keyboard.w:
black_box_y(5)
elif keyboard.s:
black_box_y(-5)
elif score >= 40:
game_state = 'lu'
elif state == '2':
if level == 1:
for i in range(len(box)):
box[i].x -= 5 + score // 10
if box[i].x < 0 - box_height[i]:
box_value(i)
elif box2.colliderect(box[i]) or \
box[i].colliderect(black_box[1]):
box_value(i)
score += 1
if keyboard.w:
box2.y -= 5
elif keyboard.s:
box2.y += 5
elif score >= 40:
game_state = 'lu'
else:
for i in range(len(box)):
box[i].x -= 5 + score // 10
if box[i].x < 0 - box_height[i]:
box_value(i)
elif box2.colliderect(box[i]) or \
box[i].colliderect(black_box[1]):
box_value(i)
score += 1
if keyboard.w:
black_box_y(5)
elif keyboard.s:
black_box_y(-5)
elif score >= 40:
game_state = 'lu'
def on_mouse_down(pos):
global state, game_state
if game_state == 'b':
if button1.collidepoint(pos):
a = raw_input()
state = '1'
game_state = 's'
elif button2.collidepoint(pos):
state = '2'
game_state = 's'
clock.schedule_interval(update_time_left, 1)
pgzrun.go()
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn