简单实现
import pygame
import math
import random
if __name__ == '__main__':
# 初始化pygame组件
pygame.init()
# 设置pygame的显示窗口尺寸
screen = pygame.display.set_mode((1000, 600))
# 初始小球圆心
sumadd_m = 20
sumadd_n = 20
# 速度增加量
add_m = 0
add_n = 0
while True:
if sumadd_m > 980 or sumadd_m < 20 or sumadd_n > 580 or sumadd_n < 20:
break
else:
screen.fill((255, 255, 255))
pygame.draw.circle(screen, (0,0,0), (sumadd_m, sumadd_n),20, 0)
# 刷新窗口
pygame.display.flip()
# 监测窗口事件
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RIGHT:
add_m = 2
add_n = 0
elif event.key == pygame.K_LEFT:
add_m = -2
add_n = 0
elif event.key == pygame.K_UP:
add_n = -2
add_m = 0
elif event.key == pygame.K_DOWN:
add_n = 2
add_m = 0
# elif event.type == pygame.KEYUP:
# if event.key == pygame.K_RIGHT:
# add_m = 0
# elif event.key == pygame.K_LEFT:
# add_m = 0
# elif event.key == pygame.K_UP:
# add_n = 0
# elif event.key == pygame.K_DOWN:
# add_n = 0
sumadd_m += add_m
sumadd_n += add_n
# pygame.time.wait(25) # 控制程序单次循环运行时间,作用使程序每次循环暂停10毫秒
网友评论