Python 3.x 为什么没有';你不转一圈吗?如何修复它?
我不确定正在创建的形状的名称,但我认为它是圆形和方形的组合,或者可能类似于圆柱体。您可以运行代码来查看它的形状。你能推荐一个我可以学习编写游戏代码的网站吗(基本游戏背后的算法)?我希望你明白我的意思,因为我英语不好Python 3.x 为什么没有';你不转一圈吗?如何修复它?,python-3.x,pygame,Python 3.x,Pygame,我不确定正在创建的形状的名称,但我认为它是圆形和方形的组合,或者可能类似于圆柱体。您可以运行代码来查看它的形状。你能推荐一个我可以学习编写游戏代码的网站吗(基本游戏背后的算法)?我希望你明白我的意思,因为我英语不好 import pygame import sys import math pygame.init() width = 640 height = 480 screen = pygame.display.set_mode((width,height)) img = pygame.S
import pygame
import sys
import math
pygame.init()
width = 640
height = 480
screen = pygame.display.set_mode((width,height))
img = pygame.Surface((50,50))
img.set_colorkey((255,0,0))
angle = 0
clock = pygame.time.Clock()
c_list = []
x = 100
y = 100
vel = 5
def draw_line(surface, color, pos1, pos2):
pygame.draw.line(surface, color, pos1, pos2)
while True:
screen.fill((122,122,122))
keys = pygame.key.get_pressed()
angle -= 3
if angle % 360 < 90:
x -= math.sin(angle/180*math.pi)**2*vel
y -= math.cos(angle/180*math.pi)**2*vel
elif angle % 360 < 180:
x -= math.sin(angle/180*math.pi)**2*vel
y += math.cos(angle/180*math.pi)**2*vel
elif angle % 360 < 270:
x += math.sin(angle/180*math.pi)**2*vel
y += math.cos(angle/180*math.pi)**2*vel
else:
x += math.sin(angle/180*math.pi)**2*vel
y -= math.cos(angle/180*math.pi)**2*vel
if (x,y) not in c_list:
c_list.append((x,y))
for i in range(len(c_list)-1):
draw_line(screen,(0,0,0),c_list[i],c_list[i+1])
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit(-1)
img_copy = pygame.transform.rotate(img, angle)
screen.blit(img_copy,(x-int(img_copy.get_width()/2),y-int(img_copy.get_width()/2)))
pygame.display.flip()
导入pygame
导入系统
输入数学
pygame.init()
宽度=640
高度=480
screen=pygame.display.set_模式((宽度、高度))
img=pygame.Surface((50,50))
设置颜色键((255,0,0))
角度=0
clock=pygame.time.clock()
c_列表=[]
x=100
y=100
水平=5
def绘制线(表面、颜色、位置1、位置2):
pygame.draw.line(表面、颜色、位置1、位置2)
尽管如此:
屏幕填充((122122))
keys=pygame.key.get_pressed()
角度-=3
如果角度%360<90:
x-=数学正弦(角度/180*数学π)**2*电平
y-=数学cos(角度/180*math.pi)**2*vel
elif角度%360<180:
x-=数学正弦(角度/180*数学π)**2*电平
y+=数学cos(角度/180*math.pi)**2*vel
elif角度%360<270:
x+=数学正弦(角度/180*数学pi)**2*电平
y+=数学cos(角度/180*math.pi)**2*vel
其他:
x+=数学正弦(角度/180*数学pi)**2*电平
y-=数学cos(角度/180*math.pi)**2*vel
如果(x,y)不在c_列表中:
c_列表追加((x,y))
对于范围内的i(len(c_列表)-1):
绘制线(屏幕,(0,0,0),c_列表[i],c_列表[i+1])
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
系统出口(-1)
img_copy=pygame.transform.rotate(img,角度)
blit(img\u copy,(x-int(img\u copy.get\u width()/2),y-int(img\u copy.get\u width()/2)))
pygame.display.flip()
您必须找到圆的切线。如果有角度
,则指向圆上点的向量为:
vx,vy=cos(角度),sin(角度)
与圆相切的向量旋转90°:
tx,ty=-vy,vy
将切线乘以速度添加到每帧中的点(x,y):
x-=math.sin(角度*math.pi/180)*vel
y+=数学cos(角度*数学pi/180)*vel
角度+=3
另见
最简单的例子:
导入pygame
导入系统
输入数学
pygame.init()
宽度=640
高度=480
screen=pygame.display.set_模式((宽度、高度))
clock=pygame.time.clock()
img=pygame.Surface((50,50))
设置颜色键((255,0,0))
角度=0
c_列表=[]
x、 y=300200
水平=5
def绘制线(表面、颜色、位置1、位置2):
pygame.draw.line(表面、颜色、位置1、位置2)
开始=错误
尽管如此:
屏幕填充((122122))
keys=pygame.key.get_pressed()
x-=数学sin(角度*数学pi/180)*vel
y+=数学cos(角度*数学pi/180)*vel
角度+=3
如果(x,y)不在c_列表中:
c_列表追加((x,y))
对于范围内的i(len(c_列表)-1):
绘制线(屏幕,(0,0,0),c_列表[i],c_列表[i+1])
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
系统出口(-1)
img_copy=pygame.transform.rotate(img,-角度)
blit(img\u copy,(x-int(img\u copy.get\u width()/2),y-int(img\u copy.get\u width()/2)))
pygame.display.flip()