首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >pygame.sprite.collision未在正确的位置执行

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

Stack Overflow用户
提问于 2017-03-15 05:04:11
回答 1查看 87关注 0票数 0

我目前正在学习一门A级计算机科学课程,在这门课程中,我必须为一个项目创建一个游戏。我知道这段代码可能效率不高,但我正试着将它保留在我自己的工作中,并希望能帮助我弄糊涂的部分。

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

冲突正在工作,但当前使用的rect必须放错位置或无法工作

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

代码语言:javascript
复制
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()    
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-15 07:40:35

试着画出机器人的矩形,你就会知道出了什么问题。

代码语言:javascript
复制
pg.draw.rect(gameDisplay, red, Robot.rect, 2)

你在错误的位置涂抹了图像。因为你做的是Robot.rect.midbottom = Robot.RobotPos,所以Robot.RobotPos是rect的中间底部位置。取而代之的是在矩形的topleft位置对图像进行Blit:gameDisplay.blit(Robot.image, Robot.rect)。如果矩形和图像具有不同的大小,则必须调整blit位置。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42796507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档