为什么不转一圈?以及如何修复它? [英] Why it doesn't spin in a circle? And how to fix it?
问题描述
我不确定正在创建的形状的名称是什么,但我认为是圆形和方形的组合,或者可能类似于圆柱体.您可以运行代码以查看它的形状.你能推荐一个我可以学习编写游戏代码的网站吗(基本游戏背后的算法)?我希望你明白我的意思,因为我不擅长英语.
导入pygame导入系统导入数学pygame.init()宽度 = 640高度 = 480screen = pygame.display.set_mode((width,height))img = pygame.Surface((50,50))img.set_colorkey((255,0,0))角度 = 0时钟 = pygame.time.Clock()c_list = []x = 100y = 100vel = 5def draw_line(表面,颜色,pos1,pos2):pygame.draw.line(表面,颜色,pos1,pos2)为真:screen.fill((122,122,122))键 = pygame.key.get_pressed()角度 -= 3如果角度 % 360 <90:x -= math.sin(angle/180*math.pi)**2*vely -= math.cos(angle/180*math.pi)**2*velelif 角度 % 360 <180:x -= math.sin(angle/180*math.pi)**2*vely += math.cos(angle/180*math.pi)**2*velelif 角度 % 360 <270:x += math.sin(angle/180*math.pi)**2*vely += math.cos(angle/180*math.pi)**2*vel别的:x += math.sin(angle/180*math.pi)**2*vely -= math.cos(angle/180*math.pi)**2*vel如果 (x,y) 不在 c_list 中:c_list.append((x,y))对于范围内的 i(len(c_list)-1):draw_line(screen,(0,0,0),c_list[i],c_list[i+1])时钟滴答(60)对于 pygame.event.get() 中的事件:如果 event.type == pygame.QUIT:sys.exit(-1)img_copy = pygame.transform.rotate(img, 角度)screen.blit(img_copy,(x-int(img_copy.get_width()/2),y-int(img_copy.get_width()/2)))pygame.display.flip()
你必须找到圆的切线.如果你有一个 angle
那么到圆上点的向量是:
vx, vy = cos(angle), sin(angle)
圆的切线是旋转 90° 的向量:
tx, ty = -vy, vy
在每一帧中将乘以速度的切线与点 (x, y) 相加:
x -= math.sin(angle*math.pi/180)*vely += math.cos(angle*math.pi/180)*vel角度 += 3
另见
导入pygame导入系统导入数学pygame.init()宽度 = 640高度 = 480screen = pygame.display.set_mode((width,height))时钟 = pygame.time.Clock()img = pygame.Surface((50,50))img.set_colorkey((255,0,0))角度 = 0c_list = []x, y = 300, 200vel = 5def draw_line(表面,颜色,pos1,pos2):pygame.draw.line(表面,颜色,pos1,pos2)开始=假为真:screen.fill((122,122,122))键 = pygame.key.get_pressed()x -= math.sin(angle*math.pi/180)*vely += math.cos(angle*math.pi/180)*vel角度 += 3如果 (x,y) 不在 c_list 中:c_list.append((x,y))对于范围内的 i(len(c_list)-1):draw_line(screen,(0,0,0),c_list[i],c_list[i+1])时钟滴答(60)对于 pygame.event.get() 中的事件:如果 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()
I'm not sure what the name of the shape that is being created, but I think is a combination of circle and square or maybe is similar to a cylinder. You can run the code to see what shape it makes. And could you suggest a site where I can learn to code a game (algorithms behind the basic games)? I hope you understand what I mean because I'm not good at English.
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()
You have to find the tangent of the circle. If you have an angle
then the vector to the point on the circle is:
vx, vy = cos(angle), sin(angle)
The tangent to the circle is the vector rotated by 90°:
tx, ty = -vy, vy
Add the tangent multiplied by the velocity to the point (x, y) in every frame:
x -= math.sin(angle*math.pi/180)*vel
y += math.cos(angle*math.pi/180)*vel
angle += 3
See also Move and rotate.
Minimal example:
import pygame
import sys
import math
pygame.init()
width = 640
height = 480
screen = pygame.display.set_mode((width,height))
clock = pygame.time.Clock()
img = pygame.Surface((50,50))
img.set_colorkey((255,0,0))
angle = 0
c_list = []
x, y = 300, 200
vel = 5
def draw_line(surface, color, pos1, pos2):
pygame.draw.line(surface, color, pos1, pos2)
start = False
while True:
screen.fill((122,122,122))
keys = pygame.key.get_pressed()
x -= math.sin(angle*math.pi/180)*vel
y += math.cos(angle*math.pi/180)*vel
angle += 3
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()
这篇关于为什么不转一圈?以及如何修复它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!