0
私は2つのスプライト1つを私のキャラクターにし、もう1つは私の敵です。私はそれらを両方ともrect属性にすると、私はpygame.Anythingでrectコリジョンを使うことができます助けて。おかげさまで ここでPygameスプライトにRect属性を持たせる方法
は私のコードです:
import pygame,sys
from pygame.locals import *
pygame.init()
#WINDOW
WIDTH = 352
HEIGHT = 122
pygame.display.set_caption('My First Python Game')
screen=pygame.display.set_mode((WIDTH,HEIGHT))
#LOADING IMAGES
pg = "player.gif"
ey = "rat.gif"
bg = "y.gif"
enemy = pygame.image.load(ey)
player = pygame.image.load(pg)
spriteWidth, spriteHeight = player.get_rect().size
background = pygame.image.load(bg)
#MUSIC
pygame.mixer.music.load("music.mp3")
pygame.mixer.music.play(-1,0.0)
#X & Y AXIS
x = 0
y = 0
#Main Game Loop
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit
x = min(max(x, 0), WIDTH - spriteWidth)
y = min(max(y, 0), HEIGHT - spriteHeight)
screen.blit(background,[0,0])
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_s:
x += 45
screen.blit(player, (y,x))
pygame.display.update()
if x <= WIDTH:
x = 0
if y <= HEIGHT:
y = 0
https://docs.python.org/ 2/tutorial/classes.html – DJMcMayhem