用python实现俄罗斯方块第一步
1.代码
import pygame, sys
from pygame.locals import *
import random
pygame.init()
#初始化函数
DISPLAYSURF =pygame.display.set_mode((800,600))#构建一个框,,就是运行窗口
Image = pygame.image.load("/code/pic/yellow.png")#设置图片其中load指定是存放图片的位置
#load一张图片存储在Image中
Rect = Image.get_rect()
Rect.center = (400,300)#图像显示位置
while True:
for event in pygame.event.get():
if event.type == QUIT:#QYIT是pygame的变量
pygame.quit()#这个是干什么的?
sys.exit()
pressed=pygame.key.get_pressed()#这个是什么?
#定义移动
if pressed[K_LEFT]:#K_LEFT代表键盘的左键
Rect.move_ip(-1,0)
elif pressed[K_RIGHT]:#K_RIGHT代表键盘的右键
Rect.move_ip(1,0)#ip是 in place
elif pressed[K_UP]:
Rect.move_ip(0,-1)
elif pressed[K_DOWN]:
Rect.move_ip(0,1)#move()与move_ip()有什么区别?
## Rect.centerx += random.randint(-1, 1) # 增加位移自动移动
DISPLAYSURF.fill((0, 0, 0)) # 每次渲染把屏幕变黑,即去掉残影(255,255,255)是白色
# (255,0,0)红色
DISPLAYSURF.blit(Image, Rect) # 调用blit将图片显示出来
pygame.display.update()
这个是python实现俄罗斯方块的第一步>

浙公网安备 33010602011771号