为什么说左上角没有在函数pygame中定义?
Why does it say that top left is not defined in a function pygame?
我正在使用 Python 2.7,我也在使用 Pygame。我一直想旋转图像,这就是我到目前为止所得到的。
import pygame, random
from random import randint
# add parking lot, hunger bar, water bar, shops, drive-thrus, etc...
# add AI car/cars, police cars when going over 30 speed at the road(5 speed in parking lot/drive thru), does not crash when hitting building/other cars(only gets hit back)
# make turning animation
pygame.init()
done = False
x = 465
when = 0
road_stripe_y = 0
road_stripe_y2 = 250
road_stripe_y3 = 500
road_stripe_y4 = 700
speed = 0
gear = 1
ai_car_y = 0
parking_lot_y = -400
player_angle = 0
ground = (50, 100, 0)
road = (64, 64, 64)
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode((1000, 800))
screen.fill(ground)
player = pygame.image.load('racing car copy.png')
ai_car = pygame.image.load('ai_racing car.png')
road_stripe = pygame.image.load('costume1 copy.png')
font = pygame.font.Font('freesansbold.ttf', 40)
text = font.render('Gear', True, black, ground)
text2 = font.render(str(gear), True, black, ground)
text2_rect = text2.get_rect()
text_rect = text.get_rect()
text_rect.center = (100, 100)
text2_rect.center = (100, 150)
screen.blit(player, (x, 600))
clock = pygame.time.Clock()
def rotate(screen, player, topleft, player_angle):
rotated_player = pygame.transform.rotate(player, player_angle)
new_player_rect = rotated_player.get_rect(center = player.get_rect(topleft = topleft).center)
screen.blit(rotated_player, new_player_rect.topleft)
while not done == True:
global gear
global left
global right
pygame.event.get()
pygame.display.flip()
pressed = pygame.key.get_pressed()
if pressed[pygame.K_ESCAPE]:
done = True
if pressed[pygame.K_a]:
if not x >= 670 and not speed == 0:
x += speed/4
if pressed[pygame.K_d] and not speed == 0:
if not x <= 280:
x -= speed/4
rotate(screen, player, topleft, player_angle)
player_collison_box = player.get_rect(topleft=(x, 600))
if pressed[pygame.K_w]:
if not speed >= 5 and gear == 1:
speed += 1
elif not speed >= 15 and gear == 2:
speed += 1
elif not speed >= 30 and gear == 3:
speed += 1
elif not speed >= 40 and gear == 4:
speed += 1
elif not speed >= 80 and gear == 5:
speed += 1
else:
if not speed <= 0:
speed -= 1
if speed >= 15 and gear == 2:
speed -= 1
if speed >= 5 and gear == 1:
speed -= 1
if speed >= 30 and gear == 3:
speed -= 1
if speed >= 40 and gear == 4:
speed -= 1
if speed >= 80 and gear == 5:
speed -= 1
if pressed[pygame.K_1]:
gear = 1
elif pressed[pygame.K_2]:
gear = 2
elif pressed[pygame.K_3]:
gear = 3
elif pressed[pygame.K_4]:
gear = 4
elif pressed[pygame.K_5]:
gear = 5
screen.fill(ground)
pygame.draw.rect(screen, road, (240 + x - 465, 0, 500, 1000))
parking_lot_y += speed
pygame.draw.rect(screen, road, (400, parking_lot_y, 600, 800))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y2))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y3))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y4))
road_stripe_y += speed
road_stripe_y2 += speed
road_stripe_y3 += speed
road_stripe_y4 += speed
if road_stripe_y >= 850:
road_stripe_y = -100
if road_stripe_y2 >= 850:
road_stripe_y2 = -100
if road_stripe_y3 >= 850:
road_stripe_y3 = -100
if road_stripe_y4 >= 850:
road_stripe_y4 = -100
ai_car_y += speed + 30
if ai_car_y >= 3000:
ai_car_y = -100
screen.blit(player, (465, 600))
text2 = font.render(str(gear), True, black, ground)
screen.blit(text, text_rect)
screen.blit(text2, text2_rect)
screen.blit(ai_car, (325 + x - 465, ai_car_y))
clock.tick(40)
pygame.quit()
这是调用函数的地方:
rotate(screen, player, topleft, player_angle)
我不知道问题是什么,但它说 'topleft' 未定义。这真的很令人困惑,因为当我导入 Pygame 时,不应该单独定义 topleft 吗?我在互联网上找不到任何有用的答案,你能帮帮我吗?
将参数 topleft
添加到函数 rotate
。将 (x, 600)
传递给参数:
def rotate(screen, player, topleft, player_angle):
rect = player.get_rect(topleft = topleft)
rotated_player = pygame.transform.rotate(player, player_angle)
new_player_rect = rotated_player.get_rect(center = rect.center)
screen.blit(rotated_player, new_player_rect.topleft)
rotate(screen, player, (x, 600), player_angle)
player_collison_box = player.get_rect(topleft=(x, 600))
我推荐 return 函数 rotate
:
中的碰撞框
def rotate(screen, player, topleft, player_angle):
rect = player.get_rect(topleft = topleft)
rotated_player = pygame.transform.rotate(player, player_angle)
new_player_rect = rotated_player.get_rect(center = rect.center)
screen.blit(rotated_player, new_player_rect.topleft)
return new_player_rect
player_collison_box = rotate(screen, player, (x, 600), player_angle)
见How do I rotate an image around its center using PyGame?
我正在使用 Python 2.7,我也在使用 Pygame。我一直想旋转图像,这就是我到目前为止所得到的。
import pygame, random
from random import randint
# add parking lot, hunger bar, water bar, shops, drive-thrus, etc...
# add AI car/cars, police cars when going over 30 speed at the road(5 speed in parking lot/drive thru), does not crash when hitting building/other cars(only gets hit back)
# make turning animation
pygame.init()
done = False
x = 465
when = 0
road_stripe_y = 0
road_stripe_y2 = 250
road_stripe_y3 = 500
road_stripe_y4 = 700
speed = 0
gear = 1
ai_car_y = 0
parking_lot_y = -400
player_angle = 0
ground = (50, 100, 0)
road = (64, 64, 64)
black = (0, 0, 0)
white = (255, 255, 255)
screen = pygame.display.set_mode((1000, 800))
screen.fill(ground)
player = pygame.image.load('racing car copy.png')
ai_car = pygame.image.load('ai_racing car.png')
road_stripe = pygame.image.load('costume1 copy.png')
font = pygame.font.Font('freesansbold.ttf', 40)
text = font.render('Gear', True, black, ground)
text2 = font.render(str(gear), True, black, ground)
text2_rect = text2.get_rect()
text_rect = text.get_rect()
text_rect.center = (100, 100)
text2_rect.center = (100, 150)
screen.blit(player, (x, 600))
clock = pygame.time.Clock()
def rotate(screen, player, topleft, player_angle):
rotated_player = pygame.transform.rotate(player, player_angle)
new_player_rect = rotated_player.get_rect(center = player.get_rect(topleft = topleft).center)
screen.blit(rotated_player, new_player_rect.topleft)
while not done == True:
global gear
global left
global right
pygame.event.get()
pygame.display.flip()
pressed = pygame.key.get_pressed()
if pressed[pygame.K_ESCAPE]:
done = True
if pressed[pygame.K_a]:
if not x >= 670 and not speed == 0:
x += speed/4
if pressed[pygame.K_d] and not speed == 0:
if not x <= 280:
x -= speed/4
rotate(screen, player, topleft, player_angle)
player_collison_box = player.get_rect(topleft=(x, 600))
if pressed[pygame.K_w]:
if not speed >= 5 and gear == 1:
speed += 1
elif not speed >= 15 and gear == 2:
speed += 1
elif not speed >= 30 and gear == 3:
speed += 1
elif not speed >= 40 and gear == 4:
speed += 1
elif not speed >= 80 and gear == 5:
speed += 1
else:
if not speed <= 0:
speed -= 1
if speed >= 15 and gear == 2:
speed -= 1
if speed >= 5 and gear == 1:
speed -= 1
if speed >= 30 and gear == 3:
speed -= 1
if speed >= 40 and gear == 4:
speed -= 1
if speed >= 80 and gear == 5:
speed -= 1
if pressed[pygame.K_1]:
gear = 1
elif pressed[pygame.K_2]:
gear = 2
elif pressed[pygame.K_3]:
gear = 3
elif pressed[pygame.K_4]:
gear = 4
elif pressed[pygame.K_5]:
gear = 5
screen.fill(ground)
pygame.draw.rect(screen, road, (240 + x - 465, 0, 500, 1000))
parking_lot_y += speed
pygame.draw.rect(screen, road, (400, parking_lot_y, 600, 800))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y2))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y3))
screen.blit(road_stripe, (490 + x - 465, road_stripe_y4))
road_stripe_y += speed
road_stripe_y2 += speed
road_stripe_y3 += speed
road_stripe_y4 += speed
if road_stripe_y >= 850:
road_stripe_y = -100
if road_stripe_y2 >= 850:
road_stripe_y2 = -100
if road_stripe_y3 >= 850:
road_stripe_y3 = -100
if road_stripe_y4 >= 850:
road_stripe_y4 = -100
ai_car_y += speed + 30
if ai_car_y >= 3000:
ai_car_y = -100
screen.blit(player, (465, 600))
text2 = font.render(str(gear), True, black, ground)
screen.blit(text, text_rect)
screen.blit(text2, text2_rect)
screen.blit(ai_car, (325 + x - 465, ai_car_y))
clock.tick(40)
pygame.quit()
这是调用函数的地方:
rotate(screen, player, topleft, player_angle)
我不知道问题是什么,但它说 'topleft' 未定义。这真的很令人困惑,因为当我导入 Pygame 时,不应该单独定义 topleft 吗?我在互联网上找不到任何有用的答案,你能帮帮我吗?
将参数 topleft
添加到函数 rotate
。将 (x, 600)
传递给参数:
def rotate(screen, player, topleft, player_angle):
rect = player.get_rect(topleft = topleft)
rotated_player = pygame.transform.rotate(player, player_angle)
new_player_rect = rotated_player.get_rect(center = rect.center)
screen.blit(rotated_player, new_player_rect.topleft)
rotate(screen, player, (x, 600), player_angle)
player_collison_box = player.get_rect(topleft=(x, 600))
我推荐 return 函数 rotate
:
def rotate(screen, player, topleft, player_angle):
rect = player.get_rect(topleft = topleft)
rotated_player = pygame.transform.rotate(player, player_angle)
new_player_rect = rotated_player.get_rect(center = rect.center)
screen.blit(rotated_player, new_player_rect.topleft)
return new_player_rect
player_collison_box = rotate(screen, player, (x, 600), player_angle)
见How do I rotate an image around its center using PyGame?