PyGame-Character 离开屏幕 [英] PyGame-Character Goes Off Screen

查看:67
本文介绍了PyGame-Character 离开屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 pygame 制作游戏,但我不知道如何防止我的角色离开屏幕(设置限制).我有一个由用户输入控制的 .png 图像,但角色可能会正常离开可见屏幕区域.我无法弄清楚如何做到这一点.我在窗口周围制作了一个矩形 (pygame.draw.rect),但我无法将矩形分配给变量,因此我可以创建碰撞.我也试过这个:

I am trying to make a game with pygame but I can't figure out how to keep my character from going off screen(set a limit). I have a .png image controlled by user input, but it's possible for the character to go off the visible screen area normally. I can't figure out how to do this. I made a rectangle around the window, (pygame.draw.rect) but I can't assign the rect to a variable so I can create a collision. I also tried this:

if not character.get_rect() in screen.get_rect():
    print("error")

但它没有用,只是用错误"消息向 python 控制台发送垃圾邮件.

But it didn't work, just spammed the python console with "error" messages.

(我用这个问题检查了另一个帖子,但没有任何效果/没有得到它)​​

(i checked the other post with this question but nothing worked/didn't get it)

所以我的问题是,我怎样才能让我的角色不离开屏幕,最好的方法是什么?

So my question is, how can I keep my character from going offscreen, and which is the best way to do that?

~谢谢

我的游戏没有滚动游戏场/相机.(只是整个窗口的固定视图)

My game doesn't have a scrolling playfield/camera. (just a fixed view on the whole window)

推荐答案

if not character.get_rect() in screen.get_rect():
    print("error")

我明白你在尝试什么.如果你想检查一个 Rect 是否在另一个里面,使用 contains():

I see what you are trying here. If you want to check if a Rect is inside another one, use contains():

包含()
测试一个矩形是否在另一个矩形内
contains(Rect) ->布尔
当参数完全在 Rect 内时返回 true.

contains()
test if one rectangle is inside another
contains(Rect) -> bool
Returns true when the argument is completely inside the Rect.

如果您只想停止屏幕边缘的移动,一个简单的解决方案是使用 clamp_ip():

If you simply want to stop the movement on the edges on the screen, an easy solution is to use clamp_ip():

clamp_ip()
将矩形内部移动到另一个位置
clamp_ip(Rect) ->无
与 Rect.clamp() 相同 [返回一个新矩形,该矩形被移动到完全位于参数 Rect 内.如果矩形太大而无法放入其中,则它在参数 Rect 内居中,但其大小不会改变.] 方法,而是就地操作.

clamp_ip()
moves the rectangle inside another, in place
clamp_ip(Rect) -> None
Same as the Rect.clamp() [Returns a new rectangle that is moved to be completely inside the argument Rect. If the rectangle is too large to fit inside, it is centered inside the argument Rect, but its size is not changed.] method, but operates in place.

这是一个简单的示例,您无法将黑色矩形移出屏幕:

Here's a simple example where you can't move the black rect outside the screen:

import pygame
pygame.init()
screen=pygame.display.set_mode((400, 400))
screen_rect=screen.get_rect()
player=pygame.Rect(180, 180, 20, 20)
run=True
while run:
    for e in pygame.event.get():
        if e.type == pygame.QUIT: run = False
    keys = pygame.key.get_pressed()
    if keys[pygame.K_w]: player.move_ip(0, -1)
    if keys[pygame.K_a]: player.move_ip(-1, 0)
    if keys[pygame.K_s]: player.move_ip(0, 1)
    if keys[pygame.K_d]: player.move_ip(1, 0)
    player.clamp_ip(screen_rect) # ensure player is inside screen
    screen.fill((255,255,255))
    pygame.draw.rect(screen, (0,0,0), player)
    pygame.display.flip()

这篇关于PyGame-Character 离开屏幕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆