pygame.sprite.collision未在正确的位置执行

时间:2017-03-14 21:04:11

标签: python pygame

我目前正在攻读A级计算机科学课程,我必须为项目创建一个游戏。我知道这段代码可能效率不高但是我想把它保留在我自己的工作中,并且想要一只手让我感到困惑。

此代码当前为机器人和平台创建了一个精灵,我正在尝试解决两者之间的冲突。此刻,他们确实发生了碰撞,但只有当机器人的顶部接触到平台的底部时,它才不再出现在屏幕上。

碰撞正在发挥作用,但目前使用的矩形必须放错位置或不起作用

任何想法或帮助都将不胜感激。

import pygame as pg
import time
import random

pg.init()#initiates pygame

display_height = 690#Creates width and height of screen
display_width = 1024
#Colours
white = (255,255,255) 
black = (0,0,0)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
sky = (73,71,65) # grey

Robot_height = 100#Height of robot
Robot_width = 112 #Width of robot
lives = 3 #Robot Lives
Bullet_Fired = False
PowerUp_Active = False
Robot_acc = 0.3 #Robot Acceleration
vec = pg.math.Vector2

gameDisplay = pg.display.set_mode((display_width,display_height)) #Sets display properties of window
pg.display.set_caption ("Game") #Title on window
clock = pg.time.Clock()
robotImg = pg.image.load("robot1.png") #Loads robots image

#Class for platforms
class Platform(pg.sprite.Sprite):
    def __init__(self, x,y,w,h):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((w,h))
        self.image.fill(blue)
        self.rect = self.image.get_rect()
        self.rect.x = x
        self.rect.y = y

#class for robot
class RobotClass(pg.sprite.Sprite):
    def __init__(self):
        pg.sprite.Sprite.__init__(self)
        self.image = pg.Surface((Robot_width,Robot_height))
        self.rect = self.image.get_rect()
        self.rect.center = (display_width / 2, display_height / 2)
        self.RobotPos = vec(display_width / 2, display_height / 2)
        self.bottom = (0,0)
        self.vel = vec(0, 0)
        self.acc = vec(0, 0.005)

#creates platform 1
p1 = Platform(0,display_height - 40,display_width,40)

#creates a sprite group for platforms
platforms = pg.sprite.Group()
#Adds platform 1
platforms.add(p1)

#game loop
def game_loop():
    Robot = RobotClass()
    Robot_friction = -0.3 #Friction value
    vec = pg.math.Vector2 #Setting vec as vector quantity
    while True:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                pg.quit
                quit()
            #Starts acceleration when key is pressed
            if event.type == pg.KEYDOWN:
                if event.key == pg.K_LEFT:
                    Robot.acc.x = -Robot_acc
                elif event.key == pg.K_RIGHT:
                    Robot.acc.x = Robot_acc
            #Adds friction to accleration to slow robot down when key is not being pressed
            if event.type == pg.KEYUP:
                if event.key == pg.K_LEFT or event.key == pg.K_RIGHT:
                    Robot.acc.x = Robot.acc.x * Robot_friction

        #Adjusts velocity of robot by adding the acceleration on each cycle
        Robot.vel = Robot.vel+ Robot.acc
        #Fills background
        gameDisplay.fill(sky)
        #Draws the platform p1 to the screen
        pg.draw.rect(gameDisplay, blue, (p1))
        #Changes Robot position according to its velocity,acceleration and the friction
        Robot.RobotPos = Robot.RobotPos + Robot.vel + 0.5 * Robot.acc
        #Loads robot onto screen
        gameDisplay.blit(robotImg,(Robot.RobotPos))
        #Updates display
        pg.display.update()
        clock.tick(60)

        #Sets bottom of robot to its position
        Robot.rect.midbottom =  Robot.RobotPos

        #Collision detection
        hits = pg.sprite.spritecollide(Robot , platforms, False)
        if hits:
            print ("Collision")
            #Puts Robot on top of platform
            Robot.RobotPos.y = hits[0].rect.top + 1
            Robot.vel.y = 0

        #Sets top velocity of robot    
        if Robot.vel.x > 6:
            Robot.vel.x = 6
        if Robot.vel.x < -6:
            Robot.vel.x = -6
        #Makes robot velocity = 0 when it is close to 0
        if Robot.vel.x < 0.05 and Robot.vel.x > -0.05:
            Robot.acc.x = 0
            Robot.vel.x = 0


game_loop()
pg.quit()
quit()    

1 个答案:

答案 0 :(得分:0)

尝试绘制机器人的矩形,你会发现错误。

pg.draw.rect(gameDisplay, red, Robot.rect, 2)

您将图像置于错误的位置。 Robot.RobotPos是rect的midbottom pos,因为你做Robot.rect.midbottom = Robot.RobotPos。将图像转换为矩形的位置,而不是gameDisplay.blit(Robot.image, Robot.rect)。如果矩形和图像的大小不同,则必须调整blit位置。