当用户按下 q 发射子弹时,就没有平稳的运动。他们需要在屏幕上移动鼠标才能使子弹移动。
我尝试在youtube上环顾StackOverflow,重新组织代码。
def tank(x,y,turretpos):
x = int(x)
y = int(y)
possibleturrets = [(x-25, y-2), (x-25, y-4), (x-25, y-6), (x-25, y-8), (x-24, y-10),
(x-24, y-12), (x-23, y-14), (x-20, y-16), (x-18, y-18), (x-16, y-20)]
pygame.draw.circle(screen,black,(x,y),10)
pygame.draw.rect(screen,black,(x-tankheight,y,tankwidth,tankheight))
pygame.draw.line(screen,black,(x,y),possibleturrets[turretpos], turretwidth)
pygame.draw.line(screen,black,(x,y),possibleturrets[turretpos], turretwidth)
startx = 15
for i in range(7):
pygame.draw.circle(screen,black,(x-startx,y+20),wheelwidth)
startx -= 5
return possibleturrets
def fire(gun,tankx,tanky,turretpos):
fire = True
startingshellx = gun[0][0]
startingshelly = gun[0][1]
while fire:
for event in pygame.event.get():
startingshellx -= (12-turretpos)*2
startingshelly += int((((startingshellx - (gun[0][0]))*0.015)**2) - (turretpos+turretpos/(12-turretpos)))
pygame.draw.circle(screen,red,(startingshellx, startingshelly-turretpos),5)
if startingshelly > height-100:
fire = False
pygame.display.update()
我希望用户敲击 q ,子弹应以抛物线形状顺利发射。
答案 0 :(得分:0)
您必须更新主循环而不是事件循环中的位置。注意,仅当事件发生时才执行事件循环。这意味着在按下按钮时执行一次,在释放按钮时执行第二次。鼠标的移动也是一个事件,并导致事件循环被执行。
以下可能有效。内部事件处理由pygame.event.pump()
完成,而不是事件循环:
Sub Trial_RUN()
For t = 0 To 5
TestValueMethod (True)
TestValueMethod (False)
Next t
End Sub
Sub TestValueMethod(useValue2 As Boolean)
Dim beginTime As Date, aCell As Range, rngAddress As String, ResultsColumn As Long
ResultsColumn = 5
'have some values in your RngAddress. in my case i put =Rand() in the cells, and then set to values
rngAddress = "A2:A399999" 'I changed this around on my sets.
With ThisWorkbook.Sheets(1)
.Range(rngAddress).Offset(0, 1).ClearContents
beginTime = Now
For Each aCell In .Range(rngAddress).Cells
If useValue2 Then
aCell.Offset(0, 1).Value2 = aCell.Value2 + aCell.Offset(-1, 1).Value2
Else
aCell.Offset(0, 1).Value = aCell.Value + aCell.Offset(-1, 1).Value
End If
Next aCell
Dim Answer As String
If useValue2 Then Answer = " using Value2"
.Cells(Rows.Count, ResultsColumn).End(xlUp).Offset(1, 0) = DateDiff("S", beginTime, Now) & _
" seconds. For " & .Range(rngAddress).Cells.Count & " cells, at " & Now & Answer
End With
End Sub
但是我建议更改设计。避免使用相同名称的函数和变量。不要在主循环内进行额外的循环显示更新:
def fire(gun,tankx,tanky,turretpos):
firebullet = True
startingshellx = gun[0][0]
startingshelly = gun[0][1]
while firebullet:
pygame.event.pump()
startingshellx -= (12-turretpos)*2
startingshelly += int((((startingshellx - (gun[0][0]))*0.015)**2) - (turretpos+turretpos/(12-turretpos)))
pygame.draw.circle(screen,red,(startingshellx, startingshelly-turretpos),5)
if startingshelly > height-100:
firebullet = False
pygame.display.update()
如果要生成平滑运动,则必须使用浮动点值进行计算:
def fire(shellpos, tankx, tanky, turretpos):
shellpos[0] -= (12-turretpos)*2
shellpos[1] += (((shellpos[0] - (gun[0][0]))*0.015)**2) - (turretpos+turretpos/(12-turretpos))
pygame.draw.circle(screen,red,(round(shellpos[0]), round(shellpos[1]-turretpos)),5)
return (shellpos[1] <= height-100), shellpos
run = True
firebullet = False
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_q:
firebullet, shellpos = True, gun[0]
# [...]
if firebullet:
firebullet, shellpos = fire(shellpos, tankx, tanky, turretpos)
# [...]
pygame.display.update()
在绘制对象(圆形)时,将坐标坐标{int()
或round
转换为整数值:
startingshellx -= (12-turretpos)*2
startingshelly += (((startingshellx - (gun[0][0]))*0.015)**2) - (turretpos+turretpos/(12-turretpos))
请注意,如果将值截断为整数值,则在添加之前,将丢失小数位:
pygame.draw.circle(screen,red,(round(startingshellx), round(startingshelly-turretpos)),5)
如果稍后执行转换操作,则结果的唯一小数位将丢失:
>>> int(0.3) + int(0.4) + int(0.5) + int(0.6)
0
round()
如果小数位数> = 0.5,则给出下一个刨丝器的整数值:
>>> int(0.3 + 0.4 + 0.5 + 0.6)
1