简体   繁体   中英

Rect Errors on Pygame

Hi I have been creating a game where a player has to dodge oil barrels and have been working on getting rect so I can create something when the 2 sprites collide. However it seems that the collision boxes are messed up because whenever I start up the game it automatically detect a collision between the player and the barrel.

This is my code:

import pygame
import os
import time
import sys
import pyganim
import random
global rand

from threading import Thread 

class UpdateThread(Thread):
    def __init__(self):
        self.stopped = False
        Thread.__init__(self) 
    def run(self):
        while not self.stopped:
            self.downloadValue()
            time.sleep(2)
    def downloadValue(self):
        rand=random.randrange(1, 10)


class oil(object): 
    def __init__(self):
        self.image = pygame.image.load('oil.png')
        self.image = pygame.transform.scale(self.image,(300,300)) 
        self.rect = self.image.get_rect()
        self.x = 1600
        self.y = 400
    def handle_keys(self):
        key = pygame.key.get_pressed()
        distance = 10
        self.x -= distance

    def draw(self, surface):
        surface.blit(self.image, (self.x, self.y))

    def checkCollision(self, oil, sprite):
        col = sprite.colliderect(oil)
        if col == True:
            print ("GameOver")


def blit_alpha(target, source, location, opacity):
        x = location[0]
        y = location[1]
        temp = pygame.Surface((source.get_width(), source.get_height())).convert()
        temp.blit(target, (-x, -y))
        temp.blit(source, (0, 0))
        temp.set_alpha()        
        target.blit(temp, location)

class sprite(object): 
    def __init__(self):

        self.image = pygame.image.load('image1.png')
        self.rect = self.image.get_rect()
        self.x = 0
        self.y = 0
    def handle_keys(self):

        key = pygame.key.get_pressed()
        distance = 10
        if key[pygame.K_DOWN]: 
            self.y += distance
        if key[pygame.K_UP]:
            self.y -= distance
        if key[pygame.K_RIGHT]: 
            self.x += distance
        if key[pygame.K_LEFT]: 
            self.x -= distance


    def draw(self, surface):

        #surface.blit(self.image, (self.x, self.y))
        anim.blit(surface, (self.x, self.y))




anim = pyganim.PygAnimation([("image1.png", 0.5), ("image2.png", 0.5)])

pygame.init()
screen = pygame.display.set_mode((1600, 800))
oil=oil()
sprite=sprite() 
clock = pygame.time.Clock()
backgroundo= pygame.image.load('background.png')
background = pygame.transform.scale(backgroundo, (1600, 800))
anim.play()

myThread = UpdateThread()
myThread.start()
oil.checkCollision(oil.rect,sprite.rect)


while True:

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

    sprite.handle_keys()
    oil.handle_keys()

    screen.fill((255,255,255))
    blit_alpha(screen, background, (0, 0), 128)
    sprite.draw(screen)
    oil.draw(screen)

    pygame.display.update()



    clock.tick(30)

Any help would be greatly appreciated.

Since your oil and sprite classes aren't derived from a pygame.Rect class, I believe you need to be testing for collisions between them like this:

    def checkCollision(self, sprite):
        if self.rect.colliderect(sprite.rect):
            print ("GameOver")

And should be called like this:

oil.checkCollision(sprite)

instead of what you have:

oil.checkCollision(oil.rect, sprite.rect)

In object-oriented programming this is called a "has_a" relationship between to classes verses the "is_a" relationship that one gets through subclassing or deriving from another class.

BTW, according to the PEP 8 - Style Guide for Python Code , which I strongly recommend that you follow, those two classnames should have been capitalized, and named Oil and Sprite respectively.

The problem is it:

    > class sprite(object): 
    > > >def __init__(self):
    > > > >self.image = pygame.image.load('image1.png')
    > > > >self.rect = self.image.get_rect()
    > > > >self.x = 0
    > > > >self.y = 0 

when you use "getRect()" you can't use x or y for this image, you must to use othres attrib, for example:

   > class sprite(object): 
    > > >def __init__(self):
    > > > >self.image = pygame.image.load('image1.png')
    > > > >self.rect = self.image.get_rect()
    > > > >self.rect.centery = 325
    > > > >self.rect.centerx = 300

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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