如何对冲突进行编程

我想编写冲突代码。我有2个类,如果它们碰撞其中一个应该取消绘制1秒


#Laden der Pygame Bibliothek

import pygame

import time

import random

#Initialisierung der Pygame Bibliothek

pygame.init()


# Spiel-Fenster erstellen

size = [700, 500]

screen = pygame.display.set_mode(size)

screen.fill((255,255,255))

# Noetig um die fps zu begrenzen

clock = pygame.time.Clock()


# Speichert ob das Spiel-Fenster geschlossen wurde

done = False

生成只能向左和向右移动的对象的第一个类


class Schlitten():

    def __init__(self, px, py, pscreen):

        self.FARBE1 = (139,87,66)

        self.FARBE2 = (139,90,43)

        self.braun = (104,73,71)

        self.x = px

        self.grau = (118,122,121)

        self.y = py

        self.red = (255,0,0)

        self.screen = pscreen

        self.hit = False    



    def draw(self):

        if self.hit == False:

            pygame.draw.rect(self.screen, self.FARBE2, [self.x,self.y,5,75])

            pygame.draw.rect(self.screen, self.FARBE2, [self.x+29,self.y,5,75])

            pygame.draw.rect(self.screen, self.braun, [self.x+5,self.y+20,24,3])

            pygame.draw.rect(self.screen, self.braun, [self.x+5,self.y+55,24,3])

            pygame.draw.rect(self.screen, self.FARBE1, [self.x+6,self.y+15,3,50])

            pygame.draw.rect(self.screen, self.FARBE1, [self.x+12,self.y+15,3,50])

            pygame.draw.rect(self.screen, self.FARBE1, [self.x+18,self.y+15,3,50])

            pygame.draw.rect(self.screen, self.FARBE1, [self.x+24,self.y+15,3,50])

            pygame.draw.rect(self.screen, self.grau, [self.x+5,self.y+10,24,2])


    def kollision(self):

        self.hit = True



    def movemint(self):

        keys = pygame.key.get_pressed()

        if keys [pygame.K_LEFT] :

            self.x -= 4


        if keys [pygame.K_RIGHT] :

            self.x += 4


        if self.x < 0:

            self.x += 4


        if self.x > 665:

            self.x -= 4



    def left(self):

        return self.x


    def right(self):

        return self.x+34


    def up(self):

        return self.y


    def down(self):

        return self.y+75

郎朗坤
浏览 96回答 1
1回答

HUH函数

好的,首先是对这些问题的标准回答:如果你使用PyGame Sprite函数,在最初做一些额外的工作之后,你的程序将更容易编写和维护。要在任意对象上进行良好的碰撞,首先需要一个“边界框”。这是一个围绕您的对象的矩形。查看施利滕/雪橇的代码,我必须从各种绘图坐标中计算出这一点(但我只是在做一个快速/粗略的工作)。它看起来像从 和 渲染扩展了另外 31 像素和 75 像素在 .您可能希望临时添加一些代码来绘制边界框以进行检查。Schlitten.xSchlitten.yxy因此,要定义碰撞函数,我们需要一个 PyGame 矩形。class Schlitten:&nbsp; &nbsp; def __init__( self ):&nbsp; &nbsp; &nbsp; &nbsp; self.x&nbsp; &nbsp; &nbsp; = px&nbsp; &nbsp; &nbsp; &nbsp; self.y&nbsp; &nbsp; &nbsp; = py&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.width&nbsp; = 31&nbsp; &nbsp; &nbsp; &nbsp; self.height = 75&nbsp; &nbsp; &nbsp; &nbsp; self.rect&nbsp; &nbsp;= pygame.Rect( px, py, self.width, self.height )从代码中可以看出,PyGame 矩形只需要坐标。需要随着对象的移动而更新,但我们可以在碰撞测试之前执行此操作。我们添加了,因此我们的代码不会到处都是无意义的数字。此外,如果雪橇的绘图发生变化,则只需在一个地方调整这些数字。.rectself.widthself.height无论如何,现在对于碰撞函数:class Schlitten:&nbsp; &nbsp; ...&nbsp; &nbsp; def collideWith( self, other_rect ):&nbsp; &nbsp; &nbsp; &nbsp; """ Has the sleigh collided with the other object """&nbsp; &nbsp; &nbsp; &nbsp; self.rect.x = self.x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # update the rect position&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y = self.y&nbsp; &nbsp; &nbsp; &nbsp; collision&nbsp; &nbsp;= self.rect.colliderect( other_rect )&nbsp; &nbsp; &nbsp; &nbsp; return collision对类进行类似的更改 - 至少到代码具有 .Baumbaum.rectclass Baum():&nbsp; &nbsp; def __init__( self, pos_x, pos_y, pscreen, pschlitten ):&nbsp; &nbsp; &nbsp; &nbsp; self.x&nbsp; &nbsp; &nbsp; = pos_x&nbsp; &nbsp; &nbsp; &nbsp; self.y&nbsp; &nbsp; &nbsp; = pos_y&nbsp; &nbsp; &nbsp; &nbsp; self.width&nbsp; = 50&nbsp; &nbsp; &nbsp; &nbsp; self.height = 95&nbsp; &nbsp; &nbsp; &nbsp; self.rect&nbsp; &nbsp;= pygame.Rect( pos_x, pos_y, self.width, self.height )&nbsp; &nbsp; def bewegung( self ):&nbsp; &nbsp; &nbsp; &nbsp; self.y&nbsp; &nbsp; &nbsp; += 5&nbsp; &nbsp; &nbsp; &nbsp; self.rect.y += 5&nbsp; &nbsp; def spawn( self ):&nbsp; &nbsp; &nbsp; &nbsp; if self.y > 600:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.y&nbsp; &nbsp; &nbsp; = -50&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.x&nbsp; &nbsp; &nbsp; = random.randrange(0,700)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect.x = x&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; self.rect.y = y然后,这允许代码快速轻松地检查冲突:alles_baumen = [ Baum1, Baum2, Baum3 ]&nbsp; &nbsp; # all tree objects# main loopwhile not exiting:&nbsp; &nbsp; ...&nbsp; &nbsp; for baum in alles_baumen:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# for each tree&nbsp; &nbsp; &nbsp; &nbsp; baum.draw()&nbsp; &nbsp; &nbsp; &nbsp; baum.bewegung()&nbsp; &nbsp; &nbsp; &nbsp; baum.spawn()&nbsp; &nbsp; &nbsp; &nbsp; if ( speiler1.collideWith( baum.rect ) ):&nbsp; &nbsp;# player hits tree?&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; speiler1.kollision()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; # Oh-noes!注: 树绘图功能将绘制树,就好像坐标是左下角一样。我所做的更改没有考虑到这一点,因此要么更改绘图代码,要么更改 的位置以适应此负 y 布局。yBaum.rect
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python