zoukankan      html  css  js  c++  java
  • 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 >
               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 >
    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 >
    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)

  • 相关阅读:
    java爬虫系列目录
    java爬虫系列第五讲-如何使用代理防止爬虫被屏蔽?
    java爬虫系列第三讲-获取页面中绝对路径的各种方法
    java爬虫系列第二讲-爬取最新动作电影《海王》迅雷下载地址
    java爬虫系列第一讲-爬虫入门
    spring aop中pointcut表达式完整版
    wkhtmltox实现网页转换成图片或pdf
    spring通知执行的顺序
    nginx代理(正向代理和反向代理)
    nginx热加载、热升级、回滚
  • 原文地址:https://www.cnblogs.com/llhhcc/p/10230759.html
Copyright © 2011-2022 走看看