私はあなたたちが私を助けてくれることを願っているという問題を抱えています。このサッカーゲームでは、プレーヤーをフィールド内で動かすと、ゴールはそこの位置に固執せず、ボールと一緒に動きます。
例:
import pygame
import random
import sys, glob
from pygame.locals import *
class Ball(pygame.sprite.Sprite):
def __init__(self,x,y,image_x):
self.bottom = 20
self.image = pygame.image.load("res/images/ball1.png")
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
screen.blit(self.image,(self.rect.x,self.rect.y))
self.change_x=x
self.change_y=y
class Field(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
#self.field = pygame.image.load('res/images/field.png').convert()
#self.field_rect = self.field.get_rect()
#self.field_rect.x = 0
#screen.blit(self.field,(self.field_rect.x,self.field_rect.y))
def goal_top(self):
self.goal_top = pygame.image.load("res/images/goal-top.png")
screen.blit(self.goal_top,(425,0))
def goal_bottom(self):
self.goal_bottom = pygame.image.load("res/images/goal-btm.png")
self.goal_bottom_rect = self.goal_bottom.get_rect()
self.goal_bottom_rect.y = 720
screen.blit(self.goal_bottom,(425,self.goal_bottom_rect.y ))
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,image_x,image_y):
self.bottom = 20
self.image = pygame.image.load("res/images/player/boy.png")
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
screen.blit(self.image,(self.rect.x,self.rect.y))
self.change_x=x
self.change_y=y
def changespeed_x(self,x):
self.change_x = x
def changespeed_y(self,y):
self.change_y = y
class Camera(object):
def __init__(self, camera_func, width, height):
self.camera_func = camera_func
self.state = Rect(0, 0, width, height)
def apply(self, target):
try:
return target.rect.move(self.state.topleft)
except AttributeError:
return map(sum, zip(target, self.state.topleft))
def update(self, target):
self.state = self.camera_func(self.state, target.rect)
def complex_camera(camera, target_rect):
l, t, _, _ = target_rect
_, _, w, h = camera
l, t, _, _ = -l + HALF_WIDTH, -t+HALF_HEIGHT, w, h
l = min(0, l) # stop scrolling left
l = max(-(camera.width-WIN_WIDTH), l) # stop scrolling right
t = max(-(camera.height-WIN_HEIGHT), t) # stop scrolling bottom
t = min(0, t) # stop scrolling at the top
return Rect(l, t, w, h)
pygame.init()
# Set the height and width of the screen
WIN_WIDTH = 963
WIN_HEIGHT = 760
HALF_WIDTH = int(WIN_WIDTH / 2)
HALF_HEIGHT = int(WIN_HEIGHT / 2)
size=[WIN_WIDTH,WIN_HEIGHT]
screen=pygame.display.set_mode(size)
#screen=pygame.display.set_mode(size,FULLSCREEN)
pygame.display.set_caption("Soccer")
clock=pygame.time.Clock()
x=500
y=280
set_ball_x = 500
set_ball_y = 300
shot = False
type=''
dir_y=0
##INITIAL POSITION OF IMAGES
image_x=0
image_y=0
ball_image_x=0
dir_y=0
##CAMERA TRACK
total_level_width = 963 ##BACKGROUND (Field) width
total_level_height = 1200 ##BACKGROUND (Field) height
camera = Camera(complex_camera, total_level_width, total_level_height)
background = pygame.image.load('res/images/field.png').convert()
background_rect = background.get_rect()
while 1:
clock.tick(20)
screen.blit(background, camera.apply((0,0)))
ball = Ball(set_ball_x,set_ball_y,ball_image_x)
camera.update(ball)
player = Player(x,y,image_x,image_y)
field = Field()
field.goal_top()
field.goal_bottom()
if pygame.key.get_pressed()[pygame.K_RIGHT]:
x=x+6
pos =+1
image_x = 0
if image_y < 90:
image_y +=30
else:
image_y = 0
if pygame.key.get_pressed()[pygame.K_LEFT]:
x=x-6
image_x = 130
if image_y < 90:
image_y +=30
else:
image_y = 0
if pygame.key.get_pressed()[pygame.K_UP]:
y=y-6
image_x = 195
if image_y < 90:
image_y +=30
else:
image_y = 0
if pygame.key.get_pressed()[pygame.K_DOWN]:
y=y+6
image_x = 70
if image_y < 90:
image_y +=30
else:
image_y = 0
if pygame.sprite.collide_rect(player,ball):
set_ball_x = player.change_x
set_ball_y = player.change_y+20
if pygame.key.get_pressed()[pygame.QUIT]:
sys.exit()
if pygame.key.get_pressed()[pygame.K_q]: ## KEY "Q" to EXIT
sys.exit()
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
pygame.display.flip()
pygame.display.update()
なぜこれが起こっているのか考えはありますか?ありがとうございました