#2020.08.12 13:30班 讲到判断游戏结束,下节课讲创建字体对象,把游戏结束写到屏幕上
import pygame,sys
pygame.init() #初始化,把所有的小库加载到内存中
screen=pygame.display.set_mode([800,600])
c=pygame.time.Clock() #建立游戏时钟
ball=pygame.image.load('d:\\pygame\\ball1.png')
ballrect=ball.get_rect() #获取图片的rect框
wood=pygame.image.load('d:\\pygame\\wood.png')
woodrect=wood.get_rect() #获取图片的rect框
speedy=7
speedx=12
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit() #卸载各种小库,释放内存
sys.exit() #关闭当前窗口
if event.type == pygame.MOUSEMOTION:
woodrect.centerx=event.pos[0]
woodrect.bottom=600
ballrect.y+=speedy #移动rect框到新位置
ballrect.x+=speedx #移动rect框到新位置
if ballrect.top<0:
speedy=-speedy
if ballrect.left<0 or ballrect.right>800:
speedx=-speedx
if pygame.Rect.colliderect(ballrect,woodrect)==True:
speedy = -speedy
if ballrect.bottom>600:
speedx,speedy=0,0
screen.fill((0,0,0))
screen.blit(ball,ballrect) #把图片绘制到新rect框中
screen.blit(wood,woodrect) #把图片绘制到新rect框中
pygame.display.update() #刷新到屏幕上
c.tick(60)
2020.08.12 13:30班 pygame 第1节
最新推荐文章于 2023-08-17 02:24:43 发布