python小游戏跳跃的方块
#coding=utf-8
#coding=gbk
import sys
import pygame
pygame.init()
sizi=width,heigth=640,480
screen=pygame.display.set_mode(sizi)
color=(0,0,0)#颜色
speed=[5,1]#设置移动的x轴和y轴
clock=pygame.time.Clock()
ball=pygame.image.load("konglong2.png")
ballrect=ball.get_rect()#获取矩形区域
pygame.display.set_caption("跳跃的方块")
while True:
clock.tick(110)#每秒执行60次
for enevt in pygame.event.get():#遍历所有时间
if enevt.type==pygame.QUIT:#单机就退出
sys.exit()
ballrect=ballrect.move(speed)#移动小球
#触碰左右边缘
if ballrect.left < 0 or ballrect.right > width:
speed[0]=-speed[0]
#触碰上下
if ballrect.top < 0 or ballrect.bottom > heigth:
speed[1]=-speed[1]
screen.fill(color)#填充颜色
screen.blit(ball,ballrect)#将图片划到窗口上
pygame.display.flip()#更新全部展示
pygame.quit()#退出pygame