zoukankan      html  css  js  c++  java
  • pygame --- 可怜的小乌龟

    来于----@小甲鱼工作室

    import pygame
    import sys
    from pygame.locals import *
    #初始化
    pygame.init()
    
    size = width,height=600,400 #设置背景的大小
    speed = [-2,1]      #设置图片移动的速度
    bg=(255,255,255)
    fullscreen = False  #初始化全屏为False
    screen = pygame.display.set_mode(size,RESIZABLE)    #屏幕大小为最初设置的大小,并且屏幕的大小可以调整
    
    pygame.display.set_caption("Hello")                 #屏幕的名称
    
    turtle = pygame.image.load("1.jpg")                 #本地导入的图片名称
    
    position = turtle.get_rect()                        #图片的位置
    l_head = turtle
    r_head = pygame.transform.flip(turtle,True,False)   #图片可以水平翻转不可垂直翻转
    while True:
        for event in pygame.event.get():                #退出程序
            if event.type == pygame.QUIT:
                sys.exit()
    
            if event.type == KEYDOWN:                   #键盘输入对现实的屏幕和里面的图片进行操作
                if event.key == K_LEFT:                 #图片的左右上下移动   我用的是上下左右,是米字形并且左右移动转换方向
                    turtle = l_head
                    speed = [-1,0]
                    speed = [-2,-1]
                if event.key == K_RIGHT:
                    turtle = r_head
                    speed = [1,0]
                    speed = [2,1]
                if event.key == K_UP:
                    #speed = [0,-1]
                    speed = [2,-1]
                if event.key == K_DOWN:
                    #speed = [0,1]
                    speed = [-2,1]
                if event.key == K_F11:                  #F11为设置屏幕全屏模式
                    fullscreen = not fullscreen
                    if fullscreen:
                        screen = pygame.display.set_mode((1024,768),FULLSCREEN|HWSURFACE)
                        width,height = 1024,768
                    else:
                        screen = pygame.display.set_mode(size)
    
            if event.type == VIDEORESIZE:               #对屏幕进行大小的更改,并且输出当前调整的屏幕的大小,并且更新当前的screen的大小
                size = event.size
                width,height = size
                print(size)
                screen = pygame.display.set_mode(size,RESIZABLE)
                        
        if position.bottom > height:                    #每当屏幕大小改变时对乌龟的位置进行修改,防止当屏幕缩小时,覆盖乌龟,乌龟不能动的时候
            position.bottom = height
        if position.right > 
            position.right = width
        if position.top < 0:
            position.top = 0
        if position.left < 0:
            position.left = 0
        position = position.move(speed)
        if position.left < 0 or position.right >  #防止乌龟跑出屏幕
    
            turtle = pygame.transform.flip(turtle,True,False)
            speed[0] = -speed[0]
        if position.top < 0 or position.bottom > height:
    
            speed[1] = -speed[1]
        screen.fill(bg)                                 #设置当前的屏幕为初始的默认的bg的颜色  在内存中进行,当更新完毕之后通过更新显示
        screen.blit(turtle,position)                    #将移动的Turtle镶嵌到屏幕中   在内存中进行,当更新完毕之后通过更新显示
        pygame.display.flip()                           #屏幕进行更新
        pygame.time.delay(10)                           #时延为10毫秒
       # clock.tick(200)

         

  • 相关阅读:
    asp.net IIS session丢失 session Key丢失
    本地时间和GMT(UTC)时间的转换(C#)
    <img/>标签onerror事件在IE下的bug和解决方法
    ASP.NET 页面传值方法
    asp.net中URL参数加密解密过程
    Windows 7 MVC2.0部署到IIS7【原创】
    WHENCREATERECORD的三大定律一大推论
    OAF页面查找数据源
    查询用户客户化的文件配置
    EBS AP、AR所有含CCID的业务表
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4915409.html
Copyright © 2011-2022 走看看