python方波绘制_python - 在Pygame中绘制方波 - 堆栈内存溢出

我进行了一些修改以添加平方波。

在### HERE查看地点。

import sys, pygame, math

from pygame.locals import *

# set up a bunch of constants

WHITE = (255, 255, 255)

DARKRED = (128, 0, 0)

RED = (255, 0, 0)

BLACK = ( 0, 0, 0)

GREEN = ( 0, 255, 0) ### HERE

BLUE = ( 0, 0, 255) ### HERE

BGCOLOR = WHITE

WINDOWWIDTH = 640 # width of the program's window, in pixels

WINDOWHEIGHT = 480 # height in pixels

WIN_CENTERX = int(WINDOWWIDTH / 2) # the midpoint for the width of the window

WIN_CENTERY = int(WINDOWHEIGHT / 2) # the midpoint for the height of the window

FPS = 160 # frames per second to run at

AMPLITUDE = 80 # how many pixels tall the waves with rise/fall.

# standard pygame setup code

pygame.init()

FPSCLOCK = pygame.time.Clock()

DISPLAYSURF = pygame.display.set_mode((WINDOWWIDTH, WINDOWHEIGHT))

pygame.display.set_caption('Trig Waves')

fontObj = pygame.font.Font('freesansbold.ttf', 16)

# variables that track visibility modes

showSine = True

showSquare = True ### HERE

pause = False

xPos = 0

step = 0 # the current input f

### HERE

posRecord = {'sin': [], 'square': []} # keeps track of the ball positions for drawing the waves

# making text Surface and Rect objects for various labels

### HERE

squareLabelSurf = fontObj.render('square', True, BLUE, BGCOLOR)

squareLabelRect = squareLabelSurf.get_rect()

sinLabelSurf = fontObj.render('sine', True, RED, BGCOLOR)

sinLabelRect = sinLabelSurf.get_rect()

instructionsSurf = fontObj.render('Press Q to toggle wave. P to pause.', True, BLACK, BGCOLOR)

instructionsRect = instructionsSurf.get_rect()

instructionsRect.left = 10

instructionsRect.bottom = WINDOWHEIGHT - 10

### HERE

yPosSquare = AMPLITUDE # starting position

# main application loop

while True:

# event handling loop for quit events

for event in pygame.event.get():

if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):

pygame.quit()

sys.exit()

# check for key presses that toggle pausing and wave visibility

if event.type == KEYUP:

if event.key == K_q:

showSine = not showSine

elif event.key == K_p:

pause = not pause

# fill the screen to draw from a blank state

DISPLAYSURF.fill(BGCOLOR)

# draw instructions

DISPLAYSURF.blit(instructionsSurf, instructionsRect)

# sine wave

yPos = -1 * math.sin(step) * AMPLITUDE

posRecord['sin'].append((int(xPos), int(yPos) + WIN_CENTERY))

if showSine:

# draw the sine ball and label

pygame.draw.circle(DISPLAYSURF, RED, (int(xPos), int(yPos) + WIN_CENTERY), 10)

sinLabelRect.center = (int(xPos), int(yPos) + WIN_CENTERY + 20)

DISPLAYSURF.blit(sinLabelSurf, sinLabelRect)

# draw the waves from the previously recorded ball positions

if showSine:

for x, y in posRecord['sin']:

pygame.draw.circle(DISPLAYSURF, DARKRED, (x, y), 4)

### HERE - drawing horizontal lines

# square

posRecord['square'].append((int(xPos), int(yPosSquare) + WIN_CENTERY))

if showSquare:

# draw the sine ball and label

pygame.draw.circle(DISPLAYSURF, GREEN, (int(xPos), int(yPosSquare) + WIN_CENTERY), 10)

squareLabelRect.center = (int(xPos), int(yPosSquare) + WIN_CENTERY + 20)

DISPLAYSURF.blit(squareLabelSurf, squareLabelRect)

# draw the waves from the previously recorded ball positions

if showSquare:

for x, y in posRecord['square']:

pygame.draw.circle(DISPLAYSURF, BLUE, (x, y), 4)

# draw the border

pygame.draw.rect(DISPLAYSURF, BLACK, (0, 0, WINDOWWIDTH, WINDOWHEIGHT), 1)

pygame.display.update()

FPSCLOCK.tick(FPS)

if not pause:

xPos += 0.5

if xPos > WINDOWWIDTH:

#sine ### HERE

xPos = 0

posRecord['sin'] = []

step = 0

# square ### HERE

yPosSquare = AMPLITUDE

posRecord['square'] = []

else:

#sine ### HERE

step += 0.008

#step %= 2 * math.pi

# square ### HERE

# jump top and bottom every 100 pixels

if xPos % 100 == 0:

yPosSquare *= -1

# add vertical line

for x in range(-AMPLITUDE, AMPLITUDE):

posRecord['square'].append((int(xPos), int(x) + WIN_CENTERY))

aHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9pNDJNeS5wbmc=

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值