2013-09-30 101 views
0

好的。所以我到目前爲止在python上找到了一個蛇遊戲的代碼。到目前爲止,遊戲正在運作,但現在我已經開始將BNP從正方形改爲圓形物體,而我想知道的是如何在輸入處旋轉身體和頭部,讓箭頭鍵讓身體部位發生變化。我不知道這是否可能,好吧,但不是我想要的方式。 我發佈了下面的代碼。如何在PyGame中旋轉對象

import pygame 
import sys 
from pygame.locals import * 
import random 
import time 

left = -20 
right = 20 
up = 10 
down = -10 

size = width, height = 640, 480 
block_size = 20 

class Food: 
    def __init__(self, screen, snake): 
     self.snake = snake 
     self.screen = screen 
     self.image = pygame.image.load('food.bmp').convert() 
     self.spawn() 

    def spawn(self): 
     collision = True 

     while collision: 
      random_x = random.randrange(0, width, block_size) 
      random_y = random.randrange(0, height, block_size) 

      collision = False 

      for each in snake.parts: 
       if each.position.x == random_x and each.position.y == random_y: 
        collision = True 
        break 

     self.position = self.image.get_rect().move(random_x, random_y) 

     self.blit() 

    def blit(self): 
     self.screen.blit(self.image, self.position) 

class Part: 
    def __init__(self, x=0, y=0, direction=right): 
     self.direction = direction 
    self.image = pygame.image.load('part.bmp').convert() 
    self.position = self.image.get_rect().move(x, y) 
    self.speed = block_size 

def change_direction(self, direction): 
    if self.direction + direction == 0: 
      return 

     self.direction = direction 

    def move(self): 
     if self.position.x >= width - block_size and self.direction == right: 
      return False 

     if self.position.y >= height - block_size and self.direction == down: 
      return False 

     if self.position.x <= 0 and self.direction == left: 
      return False 

    if self.position.y <= 0 and self.direction == up: 
     return False 

     if self.direction == up: 
      self.position = self.position.move(0, -self.speed) 
     elif self.direction == down: 
      self.position = self.position.move(0, self.speed) 
     elif self.direction == right: 
      self.position = self.position.move(self.speed, 0) 
     elif self.direction == left: 
      self.position = self.position.move(-self.speed, 0) 

     return True 

class Parthead: 
    def __init__(self, x=0, y=0, direction=right): 
     self.direction = direction 
     self.image = pygame.image.load('parthead.bmp').convert() 
     self.position = self.image.get_rect().move(x, y) 
     self.speed = block_size 

    def change_direction(self, direction): 
     if self.direction + direction == 0: 
      return 

     self.direction = direction 

    def move(self): 
     if self.position.x >= width - block_size and self.direction == right: 
      return False 

     if self.position.y >= height - block_size and self.direction == down: 
      return False 

     if self.position.x <= 0 and self.direction == left: 
      return False 

     if self.position.y <= 0 and self.direction == up: 
      return False 

     if self.direction == up: 
      self.position = self.position.move(0, -self.speed) 
     elif self.direction == down: 
      self.position = self.position.move(0, self.speed) 
     elif self.direction == right: 
     self.position = self.position.move(self.speed, 0) 
     elif self.direction == left: 
      self.position = self.position.move(-self.speed, 0) 

     return True 

class Snake: 

    def __init__(self, screen, x=0, y=0): 
     self.screen = screen 
     self.head = Parthead(x, y) 
     self.direction = right 
     self.length = 1 
     self.parts = [] 
     self.parts.append(self.head) 
     self.extend_flag = False 

    def change_direction(self, direction): 
     self.direction = direction 

    def move(self, food): 
     new_direction = self.direction 
     old_direction = None 
     new_part = None 

     if self.extend_flag: 
      last_part = self.parts[-1] 
      new_part = Part(last_part.position.x, last_part.position.y, last_part.direction) 

     for each in self.parts: 
      old_direction = each.direction 
      each.change_direction(new_direction) 

      if not each.move(): 
       return False 

      new_direction = old_direction 

     if self.extend_flag: 
      self.extend(new_part) 

     for each in self.parts[1:]: 
      if each.position.x == self.head.position.x and each.position.y == self.head.position.y: 
       return False 

     if food.position.x == self.head.position.x and food.position.y == self.head.position.y: 
      food.spawn() 
      self.extend_flag = True 



     return True 



    def extend(self, part): 
     self.parts.append(part) 
     self.length += 1 
     self.extend_flag = False 

    def blit(self): 
     for each in self.parts: 
      self.screen.blit(each.image, each.position) 


black = 0, 0, 0 

pygame.init() 
pygame.display.set_caption('Snake by Jonathan Dring') 
screen = pygame.display.set_mode(size) 

game = True 

while True: 
    snake = Snake(screen) 
    food = Food(screen, snake) 

    while True: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: sys.exit() 

      if event.type == KEYDOWN: 
       if (event.key == K_RIGHT): 
        snake.change_direction(right) 
       elif (event.key == K_LEFT): 
        snake.change_direction(left) 
       elif (event.key == K_UP): 
        snake.change_direction(up) 
       elif (event.key == K_DOWN): 
        snake.change_direction(down) 
       elif (event.key == K_SPACE): 
        snake.extend_flag = True 

     if not snake.move(food): 
      game = False 
      break 

     screen.fill(black) 
     print ("Snake - The Game") 
     snake.blit() 
     food.blit() 
     pygame.display.update() 
     pygame.time.delay(100) 

    while not game: 
     for event in pygame.event.get(): 
      if event.type == pygame.QUIT: sys.exit() 

      if event.type == KEYDOWN: 
       if (event.key == K_SPACE): 
        game = True 
       elif (event.key == K_RETURN): 
        game = True 
       elif event.key == K_ESCAPE: 
        pygame.quit() 






     background = pygame.image.load('gameover.bmp').convert() 
     screen.blit(background, (0, 0)) 
     pygame.display.flip() 
     pygame.time.delay(100) 

如果您知道如何操作,請回復!

+4

歡迎來到StackOverflow!以下是一些可以改善問題的方法:1)嘗試限制問題的範圍。你現在要求'在箭頭鍵的輸入處旋轉身體和頭部'。這實際上是幾個問題:如何呈現旋轉的圖像?如何跟蹤輪換?如何收集輸入?集中精力於這些事情之一。然後,只顯示你認爲能解決這個問題的代碼,並解釋爲什麼它不起作用。如果你遇到一個特定的問題,StackOverflow的效果最好,不要求編寫新功能的一般幫助。 –

+0

這個問題是你可以在文檔站點上很容易找到的 –

回答

1

鑑於目前還沒有答案,下面是如何旋轉精靈。當然,你只能旋轉精靈的矩形。當添加到精靈時,此功能會在您向其發送角度時使其旋轉:

def rotate(self,angle): 
    self.rect = pygame.transform.rotate(self.rect, angle) 

這應該起作用。