猫史档案馆


【Python作品分享】Python代码实现方块移动效果【作品秀】

用户:NanoCreate-LyricEpicNanoCreate-LyricEpic查看:1 回复:1 评论:1 创建时间:2019-12-27T22:22:37


【作品展示】

center_image

 

【作品介绍】

结合了Python3和Pygame

 

【作品源代码】

import pygame
import sys
from pygame.locals import *

pygame.init()
size = width, height = 600, 500
screen = pygame.display.set_mode(size)
color = (0, 0, 0)
color1 = (127,1,7)
color2 = (220,220,220)

N = height
M = width

grid_map = [[0 for i in range(M)] for i in range(N)]
block_list = []


clock = pygame.time.Clock()

class Block(object):
    def __init__(self,x,y,w,h):
        self.rect = Rect(x,y,w,h)
        for i in range(y,y+h):
            for j in range(x,x+w):
                grid_map[i][j]=1
                

def InitMap():
    for i in range(N):
        for j in range(M):
            grid_map[i][j]=0
            
    for i in range(0,M,50):
        b1 = Block(i,0,10,100)
        b2 = Block(i,200,10,100)
        b3 = Block(i,400,10,100)
        block_list.append(b1)
        block_list.append(b2)
        block_list.append(b3)

def DrawMap():
    for b in block_list:
        pygame.draw.rect(screen, color2, b.rect)
            
class Robot(object):
    def __init__(self):
        self.x = N//2
        self.y = M//2      
    def Move(self):
        key_press = pygame.key.get_pressed()
        tx = self.x
        ty = self.y
        if(key_press[K_LEFT]):
            tx -= 1
        elif (key_press[K_RIGHT]):
            tx += 1
        elif (key_press[K_UP]):
            ty -= 1
        elif (key_press[K_DOWN]):
            ty += 1
        if(tx>=0 and tx+20=0 and ty+20

 

【提示】

部分含有Python第三方库相关内容的作品,在海龟编辑器网页端无法运行哦!如遇到这种情况,可以打开下面的链接,下载海龟编辑器客户端:

https://python.codemao.cn


回复

上一页1 页 / 共 1下一页
比比芭比波哩波比比芭比波哩波

代码好像不全面哎

点赞0


评论