gpt4 book ai didi

python - 防止角色离开屏幕

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:23 28 4
gpt4 key购买 nike

所以我有一个非常简单的问题,但我找不到解决方案。

我已经尝试使用“clamp_ip”来尝试阻止我的角色离开屏幕,但我不确定为什么这不起作用,因为我已经按照我看到的所有示例进行操作,但仍然遇到这个问题

这是我的代码:


import math
from random import randint
import pygame
from pygame.math import Vector2

pygame.init()

screen = pygame.display.set_mode((600, 600))
screen_width = 600
screen_height = 600
black = (0, 0, 0)
pygame.display.set_caption("gang")
clock = pygame.time.Clock()


class Character:
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height


class Player(pygame.sprite.Sprite):

def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((50, 30), pygame.SRCALPHA)
pygame.draw.polygon(self.image, pygame.Color('steelblue2'), [(0, 0), (50, 15), (0, 30)])
self.original_image = self.image
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.velocityx = Vector2(6, 0)
self.velocityy = Vector2(0, 6)

def update(self):
self.rect.center = (int(self.pos.x), int(self.pos.y))
self.rotate()

def rotate(self):
direction = pygame.mouse.get_pos() - self.pos
radius, angle = direction.as_polar()
self.image = pygame.transform.rotate(self.original_image, -angle)
self.rect = self.image.get_rect(center=self.rect.center)


class Enemy(Character):
def __init__(self, x, y, width, height):
Character.__init__(self, x, y, width, height)

def draw(self, win):
pygame.draw.rect(win, (0, 255, 0), (enemy1.x, enemy1.y, enemy1.width, enemy1.height))


class Projectile(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.Surface((9, 15), pygame.SRCALPHA)
pygame.draw.circle(self.image, (0, 0, 0), (30, 50), 14)
self.rect = self.image.get_rect(center=pos)
self.pos = Vector2(pos)
self.velocityx = Vector2(9, 0)
self.velocityy = Vector2(0, 9)

def update(self):
self.pos += self.velocityx
self.rect.center = self.pos


def draw_window():
screen.fill((30, 30, 30))
enemy1.draw(screen)
all_sprites.update()
all_sprites.draw(screen)
bullets.update()
bullets.draw(screen)
pygame.display.update()


def xor(a, b):
if bool(a) != bool(b):
return True
else:
return False


def game_end():
pygame.font.init()
text = pygame.font.Font('freesansbold.ttf', 32)
text_surface = text.render('Game Over', False, (0, 0, 0))
text_rect = text_surface.get_rect()
text_rect.center = (86, 86)
screen.blit(text_surface, (172, 172))
pygame.display.update()



player = Player((150, 150))
enemy1 = Enemy(randint(300, 500), randint(300, 500), 60, 60)
projectile = Projectile((150, 150))
player_sprite = pygame.sprite.RenderPlain(player)
bullet_sprites = pygame.sprite.RenderPlain(projectile)
bullets = pygame.sprite.Group(projectile)
all_sprites = pygame.sprite.Group(player, projectile)


def main():
screen_rect = screen.get_rect()

run = True
while run:
clock.tick(60)

keys = pygame.key.get_pressed()

for e in pygame.event.get():
if e.type == pygame.QUIT:
run = False


if keys[pygame.K_SPACE]:
if len(bullets) < 5:
projectile.pos += projectile.velocityx
if xor(keys[pygame.K_a], keys[pygame.K_LEFT]):
player.pos -= player.velocityx
elif xor(keys[pygame.K_d], keys[pygame.K_RIGHT]):
player.pos += player.velocityx
elif xor(keys[pygame.K_w], keys[pygame.K_UP]):
player.pos -= player.velocityy
elif xor(keys[pygame.K_s], keys[pygame.K_DOWN]):
player.pos += player.velocityy

player.rect.clamp_ip(screen_rect)

draw_window()


main()

程序运行没有问题,但只是没有按照我的意愿执行,我可能不了解 clamp 的工作原理。

我希望我的角色不会离开屏幕。我需要做出哪些改变才能实现这一点?

最佳答案

player.posplayer.rect 必须先同步。钳制 player.rect 是不够的,因为 player.rectplayer.pos 不断更新。
player.rect 夹在 Player.update 中。如果 player.rect 被夹住,则必须将更改后的位置写回 player.pos

class Player(pygame.sprite.Sprite):

# [...]

def update(self):
self.rect.center = (int(self.pos.x), int(self.pos.y))
self.rotate()
clamp_rect = self.rect.clamp(screen.get_rect())
if clamp_rect != self.rect:
self.rect = clamp_rect
self.pos.x, self.pos.y = self.rect.center

关于python - 防止角色离开屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55610725/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com