嗨,我一直在创造一个游戏,玩家必须躲避油桶,并一直在努力获得直接,所以我可以在2个精灵碰撞时创造一些东西。然而,似乎碰撞盒搞砸了,因为每当我启动游戏时它会自动检测到玩家和枪管之间的碰撞。
这是我的代码:
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)
非常感谢任何帮助。
答案 0 :(得分:0)
由于您的oil
和sprite
类不是来自pygame.Rect
类,我相信您需要测试它们之间的冲突,如下所示:
def checkCollision(self, sprite):
if self.rect.colliderect(sprite.rect):
print ("GameOver")
应该像这样调用:
oil.checkCollision(sprite)
而不是你拥有的:
oil.checkCollision(oil.rect, sprite.rect)
在面向对象的编程中,这被称为类之间的“has_a”关系,与通过子类化或从另一个类派生的“is_a”关系相关。
顺便说一句,根据我强烈建议您遵循的PEP 8 - Style Guide for Python Code,这两个类名应该已大写,并命名为Oil
和Sprite
分别
答案 1 :(得分:0)
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