import pygame pygame.init() canvas = pygame.display.set_mode((800,450)) #生成一个长宽800/450的窗口 pygame.display.set_caption('小球弹弹弹!') bg = pygame.image.load("images/bg.jpg") #背景 ball = pygame.image.load("images/ball.png") #球的图片 speed = [1,0] #方向的增量speed[0]是左右 speed[1]是上下 position = ball.get_rect() while True: for event in pygame.event.get(): #遍历所有事件:event.get() if event.type == pygame.QUIT: #如果事件是退出事件quit exit()#退出 #请在下方书写你的代码 if event.type == pygame.KEYDOWN: #如果是按下键盘 if event.key == pygame.K_LEFT: #如果按下的是方向左键 speed = [-2,0] if event.key == pygame.K_RIGHT: #按下右键 speed = [2,0] if event.key == pygame.K_UP: #按下上键 speed = [0,-2] if event.key == pygame.K_DOWN: #按下下键 speed = [-2,2] position = position.move(speed) #请在下方书写你的代码 if position.left<0 or position.right>800:#如果小球碰到左右边界,就反弹 speed[0] = -speed[0] if position.top<0 or position.bottom>450:#如果小球碰到上下边界,就反弹 speed[1] = -speed[1] canvas.blit(bg,(0,0)) canvas.blit(ball,position) pygame.display.update()
标签:ball,小球,1610,弹弹,pygame,position,speed,event From: https://www.cnblogs.com/jyssh/p/16818902.html