Python 如何让我的精灵在pygame中跳出边界

Python 如何让我的精灵在pygame中跳出边界,python,vector,pygame,Python,Vector,Pygame,因此,基本上我想尝试让精灵从我的pygame屏幕(19201020)的边界反弹出来,但它不能与玩家的移动一起工作。对我来说,要么有球员的动作,要么有反弹。下面的代码包括玩家的移动,但不包括弹跳。但是我想要两个。。。有什么想法吗?谢谢归功于拉比和安·陈 代码: 导入pygame 导入操作系统 随机输入 输入数学 导入winsound #播放声音(“explosion.wav”,winsound.SND_别名) 操作系统环境['SDL\u视频窗口位置]=%d,%d%(0,30) wn=pygame.

因此,基本上我想尝试让精灵从我的pygame屏幕(19201020)的边界反弹出来,但它不能与玩家的移动一起工作。对我来说,要么有球员的动作,要么有反弹。下面的代码包括玩家的移动,但不包括弹跳。但是我想要两个。。。有什么想法吗?谢谢归功于拉比和安·陈

代码:

导入pygame
导入操作系统
随机输入
输入数学
导入winsound
#播放声音(“explosion.wav”,winsound.SND_别名)
操作系统环境['SDL\u视频窗口位置]=%d,%d%(0,30)
wn=pygame.display.set_模式((19201020))
clock=pygame.time.clock()
icon=pygame.image.load('icon.png'))
pygame.image.load('Sprite0.png')
pygame.image.load('Sprite0.png')
pygame.display.set_图标(图标)
pygame.display.set_标题('DeMass.io')
职业玩家(pygame.sprite.sprite):
定义初始化(self,x,y):
pygame.sprite.sprite.\uuuuu init\uuuuuuu(自我)
z=random.randint(1,2)
如果z==2:
self.original_image=pygame.image.load('Sprite0.png')
其他:
self.original_image=pygame.image.load('Sprite3.png')
self.image=self.original\u图像
self.rect=self.image.get_rect(中心=(x,y))
self.direction=pygame.math.Vector2((0,-1))
自速度=5
self.position=pygame.math.Vector2(x,y)
def点_位于(自身、x、y):
self.direction=pygame.math.Vector2(x,y)-self.rect.center
如果self.direction.length()大于0:
self.direction=self.direction.normalize()
角度=自身方向角度到((0,-1))
self.image=pygame.transform.rotate(self.original\u图像,角度)
self.rect=self.image.get_rect(中心=self.rect.center)
def移动(自身、x、y):
自位置-=自方向*y*自速度
self.position+=pygame.math.Vector2(-self.direction.y,self.direction.x)*x*self.velocity
self.rect.center=圆形(self.position.x),圆形(self.position.y)
def反射(自身,NV):
self.direction=self.direction.reflect(pygame.math.Vector2(NV))
def更新(自我):
自位置+=自方向*自速度
self.rect.center=圆形(self.position.x),圆形(self.position.y)
def命中(自我,玩家):
distance=math.sqrt(math.pow(self.xcor()-player.xcor(),2)+math.pow(self.ycor()-player.ycor(),2))
如果距离小于20:
返回真值
其他:
返回错误
玩家=玩家(200200)
尽管如此:
时钟滴答(60)
对于pygame.event.get()中的事件:
如果event.type==pygame.QUIT:
pygame.quit()
elif event.type==pygame.MOUSEMOTION:
玩家。指向(*事件位置)
keys=pygame.key.get_pressed()
如果键[pygame.K_w]或键[pygame.K_UP]:
玩家移动(0,-1)
wn.fill((255,255,255))
所有精灵。绘制(wn)
pygame.display.update()

制作反弹动画。定义边界,并在碰到墙时运行动画。(在本例中,您必须预定义hit_wall并使用if语句检查它是否在您的边界上)
例如:


如果要将玩家限制为矩形区域,则必须限制玩家的
位置
rect
属性。在方法
move
中添加一个附加参数
clamp\u rect
。如果玩家的位置超出矩形的边界,请将玩家移动到矩形内:

职业玩家(pygame.sprite.sprite):
# [...]
def移动(自、x、y、夹紧):
自位置-=自方向*y*自速度
self.position+=pygame.math.Vector2(-self.direction.y,self.direction.x)*x*self.velocity
self.rect.center=圆形(self.position.x),圆形(self.position.y)
如果self.rect.leftclamp_rect.right:
self.rect.right=右夹钳
self.position.x=self.rect.centerx
如果self.rect.top钳制\u rect.bottom:
self.rect.bottom=夹具\u rect.bottom
self.position.y=self.rect.centery
将窗口矩形(
wn.get_rect()
)传递到
move

为True时:
# [...]
keys=pygame.key.get_pressed()
如果键[pygame.K_w]或键[pygame.K_UP]:
player.move(0,-1,wn.get_rect())
# [...]

Lol这部分我已经讲过了。只是想知道如何将它与我的主代码合并而不出现任何问题。。。无论如何谢谢你!你说的“反弹”是什么意思?玩家不会自动移动。你想把播放器限制在窗口吗?好的。我希望玩家,是的,将被限制在屏幕的边界内,但不是不能离开屏幕,而是从屏幕上反弹。玩家1击中屏幕,哦,他弹了下来。谢谢我不明白。球员应该如何弹跳?反弹的标准是什么?他应该反弹多远?我希望他能反弹大约10-20像素。反弹意味着在墙上反射。谢谢这不是一项容易的任务,因为它不应该立即反弹。它应该是几帧的动画,这取决于播放器的速度。K,反弹效果。但是这段代码确实有效,所以玩家不能离开画面!谢谢
import pygame
import os
import random
import math
import winsound
# winsound.PlaySound("explosion.wav", winsound.SND_ALIAS)

os.environ['SDL_VIDEO_WINDOW_POS'] = "%d,%d" % (0, 30)
wn = pygame.display.set_mode((1920, 1020))
clock = pygame.time.Clock()
icon = pygame.image.load('Icon.png')
pygame.image.load('Sprite0.png')
pygame.image.load('Sprite0.png')
pygame.display.set_icon(icon)
pygame.display.set_caption('DeMass.io')




class Player(pygame.sprite.Sprite):
    def __init__(self, x, y):
        pygame.sprite.Sprite.__init__(self)
        z = random.randint(1, 2)
        if z == 2:
            self.original_image = pygame.image.load('Sprite0.png')
        else:
            self.original_image = pygame.image.load('Sprite3.png')
        self.image = self.original_image
        self.rect = self.image.get_rect(center=(x, y))
        self.direction = pygame.math.Vector2((0, -1))
        self.velocity = 5
        self.position = pygame.math.Vector2(x, y)


    def point_at(self, x, y):
        self.direction = pygame.math.Vector2(x, y) - self.rect.center
        if self.direction.length() > 0:
            self.direction = self.direction.normalize()
        angle = self.direction.angle_to((0, -1))
        self.image = pygame.transform.rotate(self.original_image, angle)
        self.rect = self.image.get_rect(center=self.rect.center)

    def move(self, x, y):
        self.position -= self.direction * y * self.velocity
        self.position += pygame.math.Vector2(-self.direction.y, self.direction.x) * x * self.velocity
        self.rect.center = round(self.position.x), round(self.position.y)


    def reflect(self, NV):
        self.direction = self.direction.reflect(pygame.math.Vector2(NV))

    def update(self):
        self.position += self.direction * self.velocity
        self.rect.center = round(self.position.x), round(self.position.y)

    def hit(self, player):
        distance = math.sqrt(math.pow(self.xcor() - player.xcor(), 2) + math.pow(self.ycor() - player.ycor(), 2))
        if distance < 20:
            return True
        else:
            return False



player = Player(200, 200)



while True:
    clock.tick(60)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            pygame.quit()
        elif event.type == pygame.MOUSEMOTION:
            player.point_at(*event.pos)




    keys = pygame.key.get_pressed()
    if keys[pygame.K_w] or keys[pygame.K_UP]:
        player.move(0, -1)






    wn.fill((255, 255, 255))
    all_sprites.draw(wn)
    pygame.display.update()
if hit_wall:
    animate(direction)
def animate(direction):
    if direction == (0, 1):
        /code to make it bounce the opposite direction
    elif direction == (0, -1):
        /code to make it bounce the opposite direction
    elif direction == (1, 0):
        /code to make it bounce the opposite direction
    elif direction == (-1, 0):
        /code to make it bounce the opposite direction