Pygame新手,尝试旋转png图像,但它冻结了窗口

时间:2016-11-30 15:05:52

标签: python-3.x rotation pygame

这是我的程序,我一直在研究pygame,它是一个类似蛇的游戏,你收集苹果来扩展你的蛇并获得分数,简单。但我的问题是我正在尝试旋转我的.png文件蛇头,以便它随着它移动而随着蛇一起旋转,但每当我尝试将窗口冻结时。冻结窗口的代码只是(python)第112行的一行代码,代码行很简单:

return direction

这是下面的工作代码:

import pygame
import time
import random

pygame.init()

display_width = 800
display_height = 600

gameDisplay = pygame.display.set_mode((display_width,display_height))
pygame.display.set_caption('Snake')

img_snake_head = pygame.image.load('C:/Users/Pc/Desktop/School Stuff/Programs/Python Programs/Large Projects & Home Projects/Snake Pygame/snake_head.png')

clock = pygame.time.Clock()
direction = "right"

font = pygame.font.SysFont(None, 45)

def snake(block_size, snakelist):
    if direction == "right":
        head = pygame.transform.rotate(img_snake_head, 270)
    if direction == "left":
        head = pygame.transform.rotate(img_snake_head, 90)
    if direction == "up":
        head = img_snake_head
    if direction == "down":
        head = pygame.transform.rotate(img_snake_head, 180)

    gameDisplay.blit(img_snake_head, (snakelist[-1][0],snakelist[-1][1]))
    for XnY in snakelist[:-1]:
        pygame.draw.rect(gameDisplay, green, [XnY[0], XnY[1], block_size, block_size])

def message_to_screen(msg,colour):
    screen_text = font.render(msg, True, colour)
    gameDisplay.blit(screen_text, [display_width/2 - screen_text.get_width()/2, display_height/2 - screen_text.get_height()/2])

def gameLoop():
    global direction

    PlayerScore = 0

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                gameExit = True
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_LEFT:
                    direction = "left"
                    lead_x_change = -block_size
                    lead_y_change = 0                
                elif event.key == pygame.K_RIGHT:
                    direction = "right"
                    lead_x_change = block_size
                    lead_y_change = 0                
                elif event.key == pygame.K_UP:
                    direction = "up"
                    lead_y_change = -block_size
                    lead_x_change = 0
                elif event.key == pygame.K_DOWN:
                    direction = "down"
                    lead_y_change = block_size
                    lead_x_change = 0
#This is where the 'return direction' goes, as i mentioned which was the code freezing the window...


        if lead_x >= display_width or lead_x <= 0 or lead_y >= display_height or lead_y <= 0:
            gameOver = True

        lead_x += lead_x_change
        lead_y += lead_y_change

        gameDisplay.fill(blue)

        pygame.draw.rect(gameDisplay, red, [randAppleX,randAppleY,AppleThickness,AppleThickness])

        snakehead = []
        snakehead.append(lead_x)
        snakehead.append(lead_y)
        snakelist.append(snakehead)
        snake(block_size, snakelist)

        if len(snakelist) > snakelength:
            del snakelist[0]

        if snakeMoving == True:
            if snakehead in snakelist[:-1]:
                gameOver = True

        snake(block_size, snakelist)

        scoretext = font.render("Score: "+str(PlayerScore), 0, (10,10,10))
        gameDisplay.blit(scoretext, (5, 10))

        pygame.display.update()

        if lead_x >= randAppleX and lead_x < randAppleX + AppleThickness or lead_x + block_size > randAppleX and lead_x + block_size < randAppleX + AppleThickness:
            if lead_y >= randAppleY and lead_y < randAppleY + AppleThickness or lead_y + block_size > randAppleY and lead_y + block_size < randAppleY + AppleThickness:
                randAppleX = round(random.randrange(10, display_width-AppleThickness)/10.0)*10.0
                randAppleY = round(random.randrange(10, display_height-AppleThickness)/10.0)*10.0
                snakelength += 1
                PlayerScore += 10

        clock.tick(FPS)

    pygame.quit()
    quit()

gameLoop()

我希望有人能告诉我为什么这会尽快冻结!提前谢谢!

1 个答案:

答案 0 :(得分:0)

问题在于您已将旋转后的图片分配给变量head,但是已将blitted img_snake_head分配。在snake功能更改中:

gameDisplay.blit(img_snake_head, (snakelist[-1][0],snakelist[-1][1]))

gameDisplay.blit(head, (snakelist[-1][0],snakelist[-1][1]))

它应该解决问题。