如何在 Pygame 中将精灵保持在边界内?

发布于 2025-01-17 04:08:40 字数 808 浏览 0 评论 0原文

我正在研究如何将我的精灵保持在 Pygame 窗口的设定边界内。这里有人可以帮我让汽车精灵始终保持在线路内吗?谢谢! (请不要来编辑我的问题,实际上是帮助我!)

import pygame

pygame.init()
screen = pygame.display.set_mode((300,208))
pygame.display.set_caption("TinyRacer")
car = pygame.image.load("car.png")
bg = pygame.image.load("bg.png")
run = True
y = 84

while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    key = pygame.key.get_pressed()
    if key[pygame.K_UP]:
      y -= 16
    if key[pygame.K_DOWN]:
      y += 16
    screen.fill((255,255,255))
    screen.blit(car, (0,y))
    screen.blit(bg,(0,0))   
    pygame.display.update() 
    
pygame.quit()

我已经尝试按照 Techwithtim 的教程进行操作,但没有成功。

my_game

I am looking on how to keep my sprite within the set boundaries of a window in Pygame. Could anyone here please help me keep the car sprite within the lines at all times? Thanks! (please don't come to edit my question, actually help me!)

import pygame

pygame.init()
screen = pygame.display.set_mode((300,208))
pygame.display.set_caption("TinyRacer")
car = pygame.image.load("car.png")
bg = pygame.image.load("bg.png")
run = True
y = 84

while run:
    pygame.time.delay(100)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
    key = pygame.key.get_pressed()
    if key[pygame.K_UP]:
      y -= 16
    if key[pygame.K_DOWN]:
      y += 16
    screen.fill((255,255,255))
    screen.blit(car, (0,y))
    screen.blit(bg,(0,0))   
    pygame.display.update() 
    
pygame.quit()

I have tried following Techwithtim's tutorial on this, but to no avail.

my_game

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怀里藏娇 2025-01-24 04:08:41

如果您将位置和大小保留在 pygame.Rect() 中,那么您可以使用特殊函数来检查碰撞。

或者简单地检查

car_rect.top < screen_rect.top and screen_rect.bottom < car_rect.bottom

或者您可以使用 contains 来检查如果一个矩形完全位于另一个矩形内。


我创建了比窗口小的绿色背景,并使用 Rect() 来检查汽车是否位于该绿色区域内。

        car_rect.y -= 16

        if car_rect.top < bg_rect.top:
            car_rect.top = bg_rect.top

我还使用 Rect() 将元素放置在屏幕中央。

car_rect.centerx = screen_rect.centerx
car_rect.centery = screen_rect.centery

您可以以同样的方式放置其他元素(即按钮中心的文本)

为了使其更简单,我使用表面而不是图像,以便每个人都可以简单地复制并运行它。

输入图片此处描述

import pygame

pygame.init()

screen = pygame.display.set_mode((300,208))
screen_rect = screen.get_rect()
pygame.display.set_caption("TinyRacer")

#car = pygame.image.load("car.png")
car = pygame.Surface((20,10))
car.fill((255,0,0))
car_rect = car.get_rect()
car_rect.centerx = screen_rect.centerx
car_rect.centery = screen_rect.centery
         
#bg = pygame.image.load("bg.png")
bg = pygame.Surface((300,100))
bg.fill((0,255,0))
bg_rect = bg.get_rect()
bg_rect.centery = screen_rect.centery

clock = pygame.time.Clock()         
run = True

while run:
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
         
    key = pygame.key.get_pressed()
    
    if key[pygame.K_UP]:
        car_rect.y -= 16
        if car_rect.top < bg_rect.top:
            car_rect.top = bg_rect.top
    if key[pygame.K_DOWN]:
        car_rect.y += 16
        if car_rect.bottom > bg_rect.bottom:
            car_rect.bottom = bg_rect.bottom
         
    screen.fill((255,255,255))
    screen.blit(bg, bg_rect)   
    screen.blit(car, car_rect)
    pygame.display.update() 
    
    clock.tick(25)  # 25FPS (it gives 40ms delay)
         
pygame.quit()

If you would keep position and size in pygame.Rect() then you could use special functions to check collisions.

Or simply check

car_rect.top < screen_rect.top and screen_rect.bottom < car_rect.bottom

Or you could use contains to check if one rect is fully inside another.


I create green background which is smaller than window, and I use Rect() to check if car in inside this green region.

        car_rect.y -= 16

        if car_rect.top < bg_rect.top:
            car_rect.top = bg_rect.top

I also use Rect() to put elements in center of screen.

car_rect.centerx = screen_rect.centerx
car_rect.centery = screen_rect.centery

The same way you can put other elements (i.e. text in center of button)

To make it simpler I use surfaces instead of images so everyone can simply copy and run it.

enter image description here

import pygame

pygame.init()

screen = pygame.display.set_mode((300,208))
screen_rect = screen.get_rect()
pygame.display.set_caption("TinyRacer")

#car = pygame.image.load("car.png")
car = pygame.Surface((20,10))
car.fill((255,0,0))
car_rect = car.get_rect()
car_rect.centerx = screen_rect.centerx
car_rect.centery = screen_rect.centery
         
#bg = pygame.image.load("bg.png")
bg = pygame.Surface((300,100))
bg.fill((0,255,0))
bg_rect = bg.get_rect()
bg_rect.centery = screen_rect.centery

clock = pygame.time.Clock()         
run = True

while run:
        
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            run = False
         
    key = pygame.key.get_pressed()
    
    if key[pygame.K_UP]:
        car_rect.y -= 16
        if car_rect.top < bg_rect.top:
            car_rect.top = bg_rect.top
    if key[pygame.K_DOWN]:
        car_rect.y += 16
        if car_rect.bottom > bg_rect.bottom:
            car_rect.bottom = bg_rect.bottom
         
    screen.fill((255,255,255))
    screen.blit(bg, bg_rect)   
    screen.blit(car, car_rect)
    pygame.display.update() 
    
    clock.tick(25)  # 25FPS (it gives 40ms delay)
         
pygame.quit()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文