用户:BCM墨水瓶查看:1 回复:1 评论:1 创建时间:2022-07-20T20:50:56
【作品展示】

【作品介绍】
由方向键控制方块上下左右旋转移动。由我老师指导完成
【作品源代码】
import sys
import random
import numpy as np
import pygame
from pygame.locals import *
def initial():
global box_speed_clock, Display, Display_font, Direction, box_num, box_num_mask, Score
global Cell_Size, Window_Width, Window_Height, Dark, Red, Green, Yellow, Black
global box_food, box_speed, over
Cell_Size = 20
Window_Width = 400
Window_Height = 700
Dark = (80, 80, 80)
Red = (255, 0, 0)
Green = (0, 255, 0)
Yellow = (255, 255, 0)
Black = (0, 0, 0)
box_speed = 7
over = 0
pygame.init()
box_speed_clock = pygame.time.Clock()
Display = pygame.display.set_mode((Window_Width, Window_Height))
pygame.display.set_caption('Box-Play')
Display_font = pygame.font.Font('freesansbold.ttf', 20)
Score = 100
Direction = ''
box_num = []
box_num_mask = []
for i in range(int(Window_Width / Cell_Size)):
box_num.append({'x': i, 'y': int(Window_Height / Cell_Size)})
draw_grid()
box_food = shape_num(make_food())
draw_food(box_food)
draw_box(box_num)
pygame.display.update()
def check_Key():
if len(pygame.event.get(QUIT)) > 0:
terminate()
keyUpEvents = pygame.event.get(KEYUP)
if len(keyUpEvents) == 0:
return None
if keyUpEvents[0].key == K_ESCAPE:
terminate()
return keyUpEvents[0].key
def get_direction(key):
if (key == K_UP):
Direction = 'UP'
elif (key == K_LEFT):
Direction = 'LEFT'
elif (key == K_RIGHT):
Direction = 'RIGHT'
elif (key == K_DOWN):
Direction = 'DOWN'
elif (key == K_SPACE):
Direction = 'SPACE'
return Direction
def run_direction():
global Direction, box_num, box_food, Score, over
for i in range(len(box_num)):
if box_num[i]['y'] == 2:
terminate()
for event in pygame.event.get():
if event.type == QUIT | K_ESCAPE:
terminate()
elif event.type == K_ESCAPE:
terminate()
elif event.type == KEYDOWN:
Direction = get_direction(event.key)
if Direction == 'RIGHT':
Direction = ''
max_x = 0
max_i = 0
for i in range(len(box_food)):
if box_food[i]['x'] > max_x:
max_x = box_food[i]['x']
max_i = i
if box_food[max_i]['x'] < 19:
for i in range(len(box_food)):
box_food[i]['x'] = box_food[i]['x'] + 1
if Direction == 'SPACE':
Direction = ''
if over == 1:
over = 0
else:
over = 1
if Direction == 'LEFT':
Direction = ''
min_x = 20
min_i = 0
for i in range(len(box_food)):
if box_food[i]['x'] < min_x:
min_x = box_food[i]['x']
min_i = i
if box_food[min_i]['x'] > 0:
for i in range(len(box_food)):
box_food[i]['x'] = box_food[i]['x'] - 1
if Direction == 'DOWN':
Direction = ''
for i in range(5):
run_down()
if Direction == 'UP':
Direction = ''
min_x = 20
min_y = 10
min_i = 0
for i in range(len(box_food)):
if box_food[i]['x'] < min_x:
min_x = box_food[i]['x']
min_i = i
if box_food[i]['x'] == min_x:
if box_food[i]['y'] < min_y:
min_y = box_food[i]['y']
min_i = i
for i in range(len(box_food)):
ds_xd = box_food[i]['x'] - box_food[min_i]['x']
ds_yd = box_food[i]['y'] + 1 - box_food[min_i]['y']
box_food[i]['x'] = box_food[min_i]['x'] - ds_yd + 1
box_food[i]['y'] = box_food[min_i]['y'] - ds_xd
def run_down():
global box_food, Score, box_num_mask, box_num, over
if over == 0:
flag = 0
box_num_mask = box_num
for i in range(len(box_food)):
for j in range(len(box_num)):
if box_food[i]['x'] == box_num[j]['x']:
if box_food[i]['y'] + 1 == box_num[j]['y']:
flag = 1
if flag == 0:
for i in range(len(box_food)):
box_food[i]['y'] = box_food[i]['y'] + 1
else:
for i in range(len(box_food)):
box_num.append(box_food[i])
box_food = shape_num(make_food())
min_y = 0
min_i = 0
for i in range(len(box_num)):
if box_num[i]['y'] < min_y:
min_y = box_num[i]['y']
min_i = i
row_count = 0
for i in range(min_y, int(Window_Height / Cell_Size)):
for j in range(len(box_num)):
if box_num[j]['y'] == i:
row_count = row_count + 1
if row_count == 20:
Score = Score + 100
m_mask = []
for i in range(int(Window_Width / Cell_Size)):
m_mask.append(
{'x': i, 'y': int(Window_Height / Cell_Size)})
box_num_mask = m_mask + \
[snake for snake in box_num_mask if snake['y'] == i]
row_count = 0
def make_food():
box_food = {'x': random.randint(
0, int(Window_Width / Cell_Size) - 1), 'y': 1}
return box_food
def shape_num(box_food):
shape_num = []
c = random.randint(1, 8)
if c == 1:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y'] + 1})
if c == 2:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 2, 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'] + 3, 'y': box_food['y'] + 1})
if c == 3:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 2, 'y': box_food['y'] - 1})
shape_num.append({'x': box_food['x'] + 3, 'y': box_food['y'] - 1})
if c == 4:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'] + 2, 'y': box_food['y']})
if c == 5:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y'] - 1})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'] + 2, 'y': box_food['y']})
if c == 6:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 2})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 3})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y'] + 3})
shape_num.append({'x': box_food['x'] + 2, 'y': box_food['y'] + 3})
if c == 7:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 2})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 3})
shape_num.append({'x': box_food['x'] + 1, 'y': box_food['y']})
shape_num.append({'x': box_food['x'] + 2, 'y': box_food['y']})
if c == 8:
shape_num.append({'x': box_food['x'], 'y': box_food['y']})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 1})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 2})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 3})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 4})
shape_num.append({'x': box_food['x'], 'y': box_food['y'] + 5})
return shape_num
def draw_food(box_food):
for i in range(len(box_food)):
x = box_food[i]['x'] * Cell_Size
y = box_food[i]['y'] * Cell_Size
appleRect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Display, Red, appleRect)
draw_grid()
pygame.display.update()
def draw_box(box_num):
for i in range(len(box_num)):
x = box_num[i]['x'] * Cell_Size
y = box_num[i]['y'] * Cell_Size
appleRect = pygame.Rect(x, y, Cell_Size, Cell_Size)
pygame.draw.rect(Display, Green, appleRect)
draw_grid()
pygame.display.update()
def draw_score(score):
Score_Font = pygame.font.Font('freesansbold.ttf', 40)
Score_Surf = Score_Font.render('Score : ' + str(score), True, Yellow)
Score_Rect = pygame.Rect(20, 20, Cell_Size, Cell_Size)
Display.blit(Score_Surf, Score_Rect)
def draw_grid():
for x in range(0, Window_Width, Cell_Size):
pygame.draw.line(Display, Dark, (x, 0), (x, Window_Height))
for y in range(0, Window_Height, Cell_Size):
pygame.draw.line(Display, Dark, (0, y), (Window_Width, y))
def terminate():
pygame.quit()
sys.exit()
if __name__ == '__main__':
initial()
while True:
Display.fill(Black)
run_down()
box_num = box_num_mask
run_direction()
draw_score(Score)
draw_food(box_food)
draw_box(box_num)
pygame.display.update()
box_speed_clock.tick(box_speed)
box_speed_clock.tick(box_speed)
【提示】
部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:
https://python.codemao.cn