圖像= https://www.dropbox.com/s/nx6yzx8ddhu36m7/car.png?dl=0,當我轉動(按左右鍵),而我加速(按了鍵)我在陌生的路上車移動汽車遊戲不pygame的正常工作
。
也有一些錯誤的加速
速度不會增加我希望它是的方式。我認爲速度應該隨着時間的推移而增加,但它不會...
任何人都可以通過嘗試代碼來幫助我嗎?
謝謝
這裏是我的代碼:
import pygame,math
pygame.init()
display_width = 1200
display_height = 800
white = (255,255,255)
black = (0,0,0)
car_image = pygame.image.load('car.png')
role_model = pygame.image.load('role_model.png')
clock = pygame.time.Clock()
FPS = 30
screen = pygame.display.set_mode([display_width,display_height])
car_width = 76
car_height = 154
def rotate(image, rect, angle):
rot_image = pygame.transform.rotate(image, angle)
rot_rect = rot_image.get_rect(center=rect.center)
return rot_image,rot_rect
def carRotationPos(angle):
x=1*math.cos(math.radians(angle-90))
y=1*math.sin(math.radians(angle-90))
return x,y
def gameloop():
running = True
angle = 0
angle_change = 0
changeX = 0
changeY=0
x=0
y=0
change_x=0
change_y=0
speed = 1
speed_change = 1
rect = role_model.get_rect()
while running == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
angle_change = 5
#changeX=0
#changeY=0
if event.key == pygame.K_RIGHT:
angle_change = -5
#changeX=0
#changeY=0
if event.key == pygame.K_UP:
#angle_change =0
changeX=-x
changeY=y
speed_change = speed_change**2 +1
if event.key == pygame.K_DOWN:
#angle_change =0
changeX=x
changeY=-y
speed_change = -(speed_change**2 + 1)
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
angle_change = 0
if event.key == pygame.K_RIGHT:
angle_change = 0
if event.key == pygame.K_UP:
changeX=0
changeY=0
speed = 1
speed_change=1
if event.key == pygame.K_DOWN:
changeX=0
changeY=0
speed = 1
speed_change=1
if angle == -360 or angle == 360:
angle = 0
angle+=angle_change
change_x+=changeX
change_y+=changeY
speed+=speed_change
if speed > 20:
speed = 20
screen.fill(white)
x,y=carRotationPos(angle)
x=round(x,5)*speed
y=round(y,5)*speed
rot_image,rot_rect=rotate(car_image,rect,angle)
rot_rect=list(rot_rect)
rot_rect1=rot_rect[0]+display_width/2-car_width/2
rot_rect2=rot_rect[1]+display_height/2-car_height/2
rot_rect1+=change_x
rot_rect2+=change_y
del rot_rect[0]
del rot_rect[1]
rot_rect.insert(0,rot_rect1)
rot_rect.insert(1,rot_rect2)
screen.blit(rot_image,rot_rect)
pygame.display.update()
clock.tick(FPS)
gameloop()
pygame.quit()
「誰能請修正錯誤,」我們將不會解決它,只是幫助它 – Raskayu
*「還有加速的問題」* - 意思是什麼? – UnholySheep
歡迎來到SO!你可以閱讀指南來問問題[這裏](http://stackoverflow.com/help/how-to-ask)。請提供關於您的程序如何發生故障的更多具體細節。 – Jerrybibo