2016-10-07 26 views
0

私は移動可能なプレーヤーを備えた2Dゲームである私の学校プロジェクトに取り組んできました。私は画面の端に境界線を作りたいと思っています。私は他のいくつかの記事を読んだが、その周りに私の心を包んでいるように見えない。もし誰かが私を助けることができれば素晴らしいだろう。ここに私のプログラムですPygame:境界線を作成

import pygame 
import os 

black = (0,0,0) 
white = (255,255,255) 
blue = (0,0,255) 


class Player(object): 
    def __init__(self): 
     self.image = pygame.image.load("player1.png") 
     self.image2 = pygame.transform.flip(self.image, True, False) 
     self.coffee = pygame.image.load("coffee.png") 
     self.computer = pygame.image.load("computer.png") 
     self.background = pygame.image.load("background.png") 
     self.flipped = False 
     self.x = 0 
     self.y = 0 


    def handle_keys(self): 
     """ Movement keys """ 
     key = pygame.key.get_pressed() 
     dist = 5 
     if key[pygame.K_DOWN]: 
      self.y += dist 
     elif key[pygame.K_UP]: 
      self.y -= dist 
     if key[pygame.K_RIGHT]: 
      self.x += dist 
      self.flipped = False 
     elif key[pygame.K_LEFT]: 
      self.x -= dist 
      self.flipped = True 

    def draw(self, surface): 
     if self.flipped: 
      image = self.image2 
     else: 
      image = self.image 
     surface.blit(self.background,(0,0)) 
     surface.blit(self.coffee, (725,500)) 
     surface.blit(self.computer,(15,500)) 
     surface.blit(image, (self.x, self.y)) 


pygame.init() 

screen = pygame.display.set_mode((800, 600)) #creates the screen 

player = Player() 
clock = pygame.time.Clock() 

running = True 
while running: 

    for event in pygame.event.get(): 
     if event.type == pygame.QUIT: 
      pygame.quit()  # quit the screen 
      running = False 

    player.handle_keys()  # movement keys 
    screen.fill((255,255,255)) # fill the screen with white 
    player.draw(screen)  # draw the player to the screen 
    pygame.display.update() # update the screen 

    clock.tick(60)    # Limits Frames Per Second to 60 or less 

答えて

1

できることは、各幅と高さに20ピクセルを加え、一連の四角形をすべて描くことです。 player.draw(screen)前に、あなたのメインの部分にこれを追加すること

screen = pygame.display.set_mode((800 + 20, 600 + 20)) 


for x in range(0, 810, 10) 
    pygame.draw.rect(surface, BLACK, [x, 0, 10, 10]) 
    pygame.draw.rect(surface, BLACK, [x, 610, 10, 10]) 

for x in range(0, 610, 10) 
    pygame.draw.rect(screen, BLACK, [0, x, 10, 10]) 
    pygame.draw.rect(screen, BLACK, [810, x, 10, 10]) 
+0

なぜ 'pygame.draw.rect(表面、BLACK、[0、0、820、10]);'など? –

+0

それは動作していないようです。どこで私のソースコードで構文を追加する必要がありますか? – meowmar

+0

私はこれをwhileループのplayer.draw(screen)の前に配置しました。矩形は完全に描画されますが、境界としては機能しません。助言がありますか? – meowmar

0

試してみてください。

pygame.init() 

line_width = 10 
width = 800 
height = 600 
#creates the screen 
screen = pygame.display.set_mode((width+line_width, height+line_width),0,32)  

screen.fill(white) # fill the screen with white 
# top line 
pygame.draw.rect(screen, black, [0,0,width,line_width]) 
# bottom line 
pygame.draw.rect(screen, black, [0,height,width,line_width]) 
# left line 
pygame.draw.rect(screen, black, [0,0,line_width, height]) 
# right line 
pygame.draw.rect(screen, black, [width,0,line_width, height+line_width]) 
+0

私はそこに配置しました。プログラムは境界矩形を描画しますが、矢印キーを押したまま移動しないと、矢印キーをタップして移動する必要があります。私はまだ画面の外に出ることができるので、境界も機能しません。 ループの外側にコードを配置しようとしましたが、それも機能しませんでした。助けてください。 – meowmar

+0

あなたは全く別の問題について話しています。描画はモーションを制限していません。 – mengg

関連する問題