python 小白之路(跳动的球)
Posted 逐梦武威
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 小白之路(跳动的球)相关的知识,希望对你有一定的参考价值。
# -*- coding: utf-8 -*-
""" Spyder Editor This is a temporary script file. """ import pygame,sys pygame.init() size = width,height = 600, 400 speend = [1,1] WHITE = 255,255,255 screen =pygame.display.set_mode(size) pygame.display.set_caption("dj") ball = pygame.image.load("dj.gif") ballrect =ball.get_rect() while True: for event in pygame.event.get(): if event.type == pygame.QUIT: sys.exit() ballrect = ballrect.move(speend[0],speend[1]) if ballrect.left < 0 or ballrect.right >width: speend[0] = -speend[0] if ballrect.top < 0 or ballrect.bottom > height: speend[1] = -speend[1] screen.fill(WHITE) screen.blit(ball,ballrect) pygame.display.update()
—————————————————————————————————————————————————————
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import pygame,sys
pygame.init()
size = width,height = 600, 400
speend = [1,1]
WHITE = 255,255,255
screen =pygame.display.set_mode(size)
pygame.display.set_caption("dj")
ball = pygame.image.load("dj.gif")
ballrect =ball.get_rect()
fps = 300
fclock =pygame.time.Clock()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
ballrect = ballrect.move(speend[0],speend[1])
if ballrect.left < 0 or ballrect.right >width:
speend[0] = -speend[0]
if ballrect.top < 0 or ballrect.bottom > height:
speend[1] = -speend[1]
screen.fill(WHITE)
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
—————————————————————————————————————————————————————
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
用上下左右控制小球方向
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import pygame,sys# 加载pygame,用 pip install pygame
pygame.init()
size = width,height = 600, 400
speend = [1,1]
WHITE = 255,255,255# 引用RGB颜色白色的值
screen =pygame.display.set_mode(size)
pygame.display.set_caption("dj")
ball = pygame.image.load("dj.gif")
ballrect =ball.get_rect()
fps = 300
fclock =pygame.time.Clock() # 调用clock时钟
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
elif event.type == pygame.KEYDOWN:# 引用pygame.KEYDOWN,
if event.key == pygame.K_LEFT:# 向下
speed[0] = speed[0] if speed[0] == 0 else(abs(speed[0])-1)*int(speed[0]/abs(speed[0]))
elif event.key == pygame.K_RIGHT:# 向右
speed[0] = speed[0] + 1 if speed [0] > 0 else speed[0] -1
elif event.key == pygame.K_UP: # 向上
speed[1] = speed[1] + 1 if speed [1] > 0 else speed[0] -1
elif event.key == pygame.K_DOWN:# 向左
speed[1] = speed[1] if speed[1] == 0 else(abs(speed[1])-1)*int(speed[1]/abs(speed[1]))
ballrect = ballrect.move(speend)
if ballrect.left < 0 or ballrect.right >width:
speend[0] = -speend[0]
if ballrect.top < 0 or ballrect.bottom > height:
speend[1] = -speend[1]
screen.fill(WHITE)# 调用刷新背景色白色
screen.blit(ball,ballrect)
pygame.display.update()
fclock.tick(fps)
以上是关于python 小白之路(跳动的球)的主要内容,如果未能解决你的问题,请参考以下文章