私は移動可能なプレーヤーを備えた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
なぜ 'pygame.draw.rect(表面、BLACK、[0、0、820、10]);'など? –
それは動作していないようです。どこで私のソースコードで構文を追加する必要がありますか? – meowmar
私はこれをwhileループのplayer.draw(screen)の前に配置しました。矩形は完全に描画されますが、境界としては機能しません。助言がありますか? – meowmar