简体   繁体   English

如何修复图像冲突 - Pygame

[英]How can I fix my image collision - Pygame

I'm pretty new to Pygame and I don't really understand collision and images.我对 Pygame 很陌生,我不太了解碰撞和图像。 My game is a basic screen saver animation but my image bounces the wrong way when it touches the borders.我的游戏是一个基本的屏幕保护程序 animation,但我的图像在碰到边框时会以错误的方式反弹。

This is the image(Note: I resized the image in photoshop): https://lh3.googleusercontent.com/kN3Hrnzx2W1Fln0kkmYymfycyU3R4FCjt-3e9hGINPkblk00pEwJFbxOCJ8wW8rA6sg这是图像(注意:我在 Photoshop 中调整了图像的大小): https://lh3.googleusercontent.com/kN3Hrnzx2W1Fln0kkmYymfycyU3R4FCjt-3e9hGINPkblk00pEwJFbxOCJ8wW8rA6sg

import pygame
pygame.init()

width, height = (624, 392)
bg = (0,0,0)
img = pygame.image.load(r"C:\Users\victo\Downloads\sony_dvd.png")
win = pygame.display.set_mode((width, height))

pygame.display.set_caption("Screen Saver")
win.fill(bg)
clock = pygame.time.Clock()

x = 0
y = 0
w = 78
h = 49
vel = 2
direct = 'se'
running = True
while running:
    clock.tick(27)

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

    if direct == 'nw':
        x = x - vel
        y = y - vel
        win.blit(img, (x, y))
        if x <= 10:
            direct = 'sw'
        elif y <= 10:
            direct = 'ne'
    if direct == 'ne':
        x = x + vel
        y = y - vel
        win.blit(img, (x, y))
        if x >= 614 - w:
            direct = 'nw'
        elif y <= 10:
            direct = 'se'
    if direct == 'sw':
        x = x - vel
        y = y + vel
        win.blit(img, (x, y))
        if x <= 10:
            direct = 'se'
        elif y >= 392 - h:
            direct = 'nw'
    if direct == 'se':
        x = x + vel
        y = y + vel
        win.blit(img, (x, y))
        if x >= 614 - w:
            direct = 'sw'
        elif y >= 382 - h:
            direct = 'ne'

    pygame.display.update()

pygame.quit()

Nothing gets drawn on the screen that you do not draw, and conversely everything you draw on the screen stays there unless you draw over it.屏幕上没有你不画的东西,相反,你在屏幕上画的所有东西都留在那里,除非你在上面画。

You are getting after images of the things you draw because the old images drawn are still on the screen.您正在获取所绘制事物的图像,因为绘制的旧图像仍在屏幕上。

The easiest way to stop that is to fill(bg) the screen each frame (at the start of your loop) before you redraw the images.阻止这种情况的最简单方法是在重绘图像之前每帧(在循环开始时fill(bg)屏幕。

while running:
    clock.tick(27)

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

    win.fill(bg) # <--- add this here

There are other ways, but that is the simplest for someone starting out.还有其他方法,但这对于刚开始的人来说是最简单的。

A more complicated approach is just to draw over the area that your moved image used to cover with a section of the background, but for most simple situations just redrawing the entire thing is easier.更复杂的方法是在移动图像过去覆盖的区域上绘制背景的一部分,但对于大多数简单的情况,重绘整个区域会更容易。

Edit:编辑:

The OP edited and changed the question after getting an answer to the original question of why it was getting ghost trails behind the image. OP 在得到原始问题的答案后编辑并更改了问题,即为什么它在图像后面出现鬼迹。 Normally I believe this is not considered good form and so would not answer the revised question, however the OP is new and the issue is a simple typo so I will address it anyway.通常我认为这不是很好的形式,因此不会回答修改后的问题,但是 OP 是新的,问题是一个简单的错字,所以无论如何我都会解决它。

There is a small typo in the code which is why it is bouncing the wrong way of some of the edges.代码中有一个小错字,这就是为什么它以错误的方式弹跳某些边缘的原因。 You have the ne and sw flipped in the nw check.您在nw检查中翻转了nesw This:这个:

    if direct == 'nw':
        x = x - vel
        y = y - vel
        win.blit(img, (x, y))
        if x <= 10:
            direct = 'sw'
        elif y <= 10:
            direct = 'ne'

should be:应该:

        if direct == 'nw':
            x = x - vel
            y = y - vel
            win.blit(img, (x, y))
            if x <= 10:
                direct = 'ne'
            elif y <= 10:
                direct = 'sw'

win.fill(bg) needs to go inside while loop. win.fill(bg) 需要 go 在 while 循环内。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM