pygame 基本试用步骤

@pygame 基本试用步骤

pygame优势

想做一个能仿真自动控制的界面,苦于找不到简单合适的图形界面环境,选来选去,还是选了 pygame作为基本的开发平台,对比labview ,c#,使用起来非常的方便。

使用步骤

首先从pygame的官网,直接下载python2.7对应pygame插件
1.python ide里面,直接添加pygame的开发包
import pygame,sys
from pygame.locals import *
import math
2.搭建pygame基本开发框架,百度就有
pygame.init()
wLength = 1000
wWidth = 600
screen = pygame.display.set_mode((wLength,wWidth))

while True :
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#添加绘图的程序代码
(百度pygame的使用方法)
#刷新界面
pygame.display.flip()

我因为要仿真一个平衡小车,所以直接搞了几张图片,贴在了界面上,
用鼠标控制天平的角度,
小车的数学模型和运动模型还没有实现
仿真界面
附上代码跟图片
import pygame,sys
from pygame.locals import *
import math
pygame.init()
wLength = 1000
wWidth = 600
screen = pygame.display.set_mode((wLength,wWidth))

#surface obeject
surf = pygame.Surface((80,16))#length and weight
surf.fill((0,0,0)) #color
#get the car image
imageCar = pygame.image.load(“car.png”)
imageCarSize = imageCar.get_size()
print imageCarSize
imagePlatform = pygame.image.load(“platform.png”)
imagePlatformSize = imagePlatform.get_size()
print imagePlatformSize
imagePivot = pygame.image.load(“pivot.png”)
imagePivotSize = imagePivot.get_size()
pivotHeight = 10
print imagePivotSize
imageAngle = pygame.image.load(“angle.png”)
imageAngleSize = imageAngle.get_size()
print imageAngleSize

imageStick = pygame.image.load(“stick.png”)
imageStickSize = imageStick.get_size()
print imageStickSize
#set the position
imageCarPosition = ( (wLength - imageCarSize[0])/2 , wWidth - imageCarSize[1] - imagePlatformSize[1] - imagePivotSize[1] - pivotHeight )
imagePlatformPosition = ( (wLength - imagePlatformSize[0])/2 , wWidth - imagePlatformSize[1] - imagePivotSize[1] - pivotHeight )
imagePivotPosition = ( (wLength - imagePivotSize[0])/2 , wWidth - imagePivotSize[1] - pivotHeight)
imageAnglePosition = ( (wLength - imageAngleSize[0]) , ( wWidth - imageAngleSize[1])/2)
imageStickPosition = [(wLength - imageCarSize[0])/2 + imageCarSize[0]/2 - 4 , wWidth - imageCarSize[1] - imagePlatformSize[1] - imagePivotSize[1] - pivotHeight - imageStickSize[1]+2]

imageCarPositionChange = [(wLength - imageCarSize[0])/2 , wWidth - imageCarSize[1] - imagePlatformSize[1] - imagePivotSize[1] - pivotHeight ]
imagePlatformPositionChange = [ (wLength - imagePlatformSize[0])/2 , wWidth - imagePlatformSize[1] - imagePivotSize[1] - pivotHeight ]
imageStickPositionChange = [imageCarPositionChange[0] + imageCarSize[0]/2 -4 , imageCarPositionChange[1] -imageStickSize[1]+2]
#set the limit [ xStart , xStop , yStart , yStop , yMiddle ]
imageAngleLimit = ( (wLength - imageAngleSize[0]) , wLength , ( wWidth - imageAngleSize[1])/2 , ( wWidth - imageAngleSize[1])/2 + imageAngleSize[1] ,wWidth /2 )

imageCarRotate = 0;
imagePlatformRotate = 0;
imagePivotRotate = 0;
imageAngleRotate = 0;
imageStickRotate = 0;

#the platform span angle
spanAngle = 30.0
#get the surface rect , the black rect
rect = surf.get_rect()
#the font display
font = pygame.font.SysFont(‘Times’, 16,1,1)
fontSurf = font.render(‘balance car’, True, (255, 0, 0))
fontPosition = [0,0]
#position font
Angle = 0
AnglePre = 0
fontAngle = font.render(‘Angle:’+str(Angle), True, (255, 0, 0))
fontAnglePosition = [wLength - imageAngleSize[0],( wWidth - imageAngleSize[1])/2 - 30 ]
#the black background

#surface obeject
surfBalance= pygame.Surface((120,16))#length and weight
surfBalance.fill((0,0,0)) #color for the balance font

#surface obeject
surf = pygame.Surface((80,16))#length and weight
surf.fill((0,0,0)) #color for the angle font

surfPlat = pygame.Surface((imagePlatformSize[0],wWidth))#length and weight
surfPlat.fill((0,0,0)) #color for the platform
blackPlatPosition = ( (wLength - imagePlatformSize[0])/2 , 0 )
#the mouse position
mouseStat = 0 #0 up , 1 down
while True :
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_ESCAPE:
pygame.quit()
sys.exit()
#the mouse move
elif event.type == pygame.MOUSEMOTION:
#the mouse is pressed
if( mouseStat == 1 ) :
pos = pygame.mouse.get_pos()
if( ( pos[0] > imageAngleLimit[ 0 ] ) and ( pos[0] < imageAngleLimit[ 1 ] )) :
if( ( pos[1] > imageAngleLimit[ 2 ] ) and ( pos[1] < imageAngleLimit[ 3 ] )) :
AnglePre = Angle
Angle = spanAngle * ( pos[1] - imageAngleLimit[ 4 ] ) / imageAngleSize[1] #get the angle by the mouse
#change the platform position by the angle
imagePlatformRotate = 0 - Angle
if( imagePlatformRotate > 0 ):
imagePlatformPositionChange[ 0 ] = imagePlatformPosition[ 0 ] + (imagePlatformSize[0]/2.0*( 1.0 - math.cos( imagePlatformRotate / 180.0 * math.pi)))
imagePlatformPositionChange[ 1 ] = imagePlatformPosition[ 1 ] - ( imagePlatformSize[0]/2.0math.sin( imagePlatformRotate / 180.0 * math.pi))
elif( imagePlatformRotate < 0 ):
imagePlatformPositionChange[ 0 ] = imagePlatformPosition[ 0 ] + (imagePlatformSize[0]/2.0
( 1.0 - math.cos( imagePlatformRotate / 180.0 * math.pi)))
imagePlatformPositionChange[ 1 ] = imagePlatformPosition[ 1 ] + ( imagePlatformSize[0]/2.0math.sin( imagePlatformRotate / 180.0 * math.pi))
else:
imagePlatformPositionChange[ 0 ] = imagePlatformPosition[ 0 ]
imagePlatformPositionChange[ 1 ] = imagePlatformPosition[ 1 ]
print imagePlatformPositionChange
#change the car position by the angle
imageCarRotate = 0 - Angle
if( imageCarRotate > 0 ):
imageCarPositionChange[ 0 ] = imageCarPosition[ 0 ] - (imageCarSize[0]/2.0
( 1.0 - math.cos( imageCarRotate / 180.0 * math.pi)))
imageCarPositionChange[ 1 ] = imageCarPosition[ 1 ] - (imageCarSize[0]/2.0math.sin( imageCarRotate / 180.0 * math.pi))
#here two lines have errors ,must check
#imageStickPositionChange[0] = imageCarPositionChange[ 0 ] + (imageCarSize[0]/2.0
math.cos( imageCarRotate / 180.0 * math.pi)) - 3
#imageStickPositionChange[1] = imageCarPositionChange[ 1 ] + imageCarSize[0]/2.0math.sin( imageCarRotate / 180.0 * math.pi)- imageStickSize[1]+2
elif( imageCarRotate < 0 ):
imageCarPositionChange[ 0 ] = imageCarPosition[ 0 ] + ( imageCarSize[0]/2.0
( 1.0 - math.cos( imageCarRotate / 180.0 * math.pi)))
imageCarPositionChange[ 1 ] = imageCarPosition[ 1 ] + ( imageCarSize[0]/2.0math.sin( imageCarRotate / 180.0 * math.pi))
#here two lines have errors ,must check
#imageStickPositionChange[0] = imageCarPositionChange[ 0 ] + (imageCarSize[0]/2.0
math.cos( imageCarRotate / 180.0 * math.pi)) - 3
#imageStickPositionChange[1] = imageCarPositionChange[ 1 ] - imageCarSize[0]/2.0*math.sin( imageCarRotate / 180.0 * math.pi)- imageStickSize[1]+2
else:
imageCarPositionChange[ 0 ] = imageCarPosition[ 0 ]
imageCarPositionChange[ 1 ] = imageCarPosition[ 1 ]
print imageCarPositionChange

            print Angle
    #the mouse is down 
    elif event.type == pygame.MOUSEBUTTONDOWN:
        mouseStat = 1 #indicate down
    elif event.type == pygame.MOUSEBUTTONUP:
        mouseStat = 0 #indicate up
#over the black background
screen.blit(surfPlat,blackPlatPosition)
#display the car
imageCarDisplay = pygame.transform.rotate(imageCar,imageCarRotate)
screen.blit(imageCarDisplay,imageCarPositionChange)
#display the platform
imagePlatformDisplay = pygame.transform.rotate(imagePlatform,imagePlatformRotate)
screen.blit(imagePlatformDisplay,imagePlatformPositionChange)
#display the stick
#imageStickDisplay = pygame.transform.rotate(imageStick,imageStickRotate)
#screen.blit(imageStickDisplay,imageStickPositionChange)
#display the pivot
imagePivotDisplay = pygame.transform.rotate(imagePivot,imagePivotRotate)
screen.blit(imagePivotDisplay,imagePivotPosition)
#display the angle
imageAngleDisplay = pygame.transform.rotate(imageAngle,imageAngleRotate)
screen.blit(imageAngleDisplay,imageAnglePosition)
#over the black rect
screen.blit(surfBalance,fontPosition)
screen.blit(fontSurf,fontPosition)
#over the black rect
screen.blit(surf,fontAnglePosition)
#write the angle
fontAngle = font.render('Ag:'+str(int(Angle)), True, (255, 0, 0))
screen.blit(fontAngle,fontAnglePosition)
pygame.display.flip()

图片:
资源图片列表

直接将代码保存成car.py,将图片放到同一个文件夹下面,直接运行程序即可。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值