Pygame Sprite Collision - python-3.x

I've been working on creating a small, space invaders style game in pygame. I've almost reached the end however, I want to make it so if the enemy ships collide with my ship, a collision is detected and the game ends.
So far I have the code for detecting when a bullet and an enemy ship collides, however when I tried to rewrite this for a enemy/player collision it doesnt work as expected, so I think im doing something incorrect.
Code in question:
for block in block_list:
player_hit_list = pygame.sprite.spritecollide(block, player_list, True)
for player in player_hit_list:
explosion.play()
block_list.remove(block)
player_list.remove(player)
all_sprites_list.remove(block)
all_sprites_list.remove(player)
if block.rect.y < +10:
block_list.remove(block)
all_sprites_list.remove(block)
Full code: http://pastebin.com/FShPuR6A
Is anyone able to tell my why the code I have isnt functioning?
Thanks a lot

class Block(pygame.sprite.Sprite):
def __init__(self, color):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("spaceship.png")
self.rect = self.image.get_rect()
def update(self):
self.rect.y += 1
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.x=0
self.y=0
self.image = pygame.image.load("alien.png")
self.rect = self.image.get_rect()
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x = pos[0]
At first glance, you have a Block class, with a picture of a spaceship that moves like an Alien. The Player class, has an alien image, and moves with the mouse.
My advice would be to rename the classes so that you will know what is what. It will be easier to spot the error.
EDIT:
It also looks like your Player does not move at all:
def render(self):
if (self.currentImage==0):
screen.blit(self.image, (self.x, self.y))
You update the player via self.rect.x, but you draw using self.x and self.y.

Related

What causes a nested QRubberband to move unexpectedly?

I am just curious if I can make a nested QRubberband. (I or someone might find a use to it). I managed to edit the code from this answer to make a nested QRubberband. It is all fine and working until I move the QRubberband inside its parent QRubberband. I was very confused as it moves wildly when I'm dragging it.
This is the sample code:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class ResizableRubberBand(QRubberBand):
moving = False
def __init__(self, parent=None):
super(ResizableRubberBand, self).__init__(QRubberBand.Rectangle, parent)
self.setAttribute(Qt.WA_TransparentForMouseEvents, False)
self.draggable = True
self.dragging = False
self.is_dragging = False
self.dragging_threshold = 5
self.mousePressPos = None
self.borderRadius = 5
self.setWindowFlags(Qt.SubWindow)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(
QSizeGrip(self), 0,
Qt.AlignLeft | Qt.AlignTop)
layout.addWidget(
QSizeGrip(self), 0,
Qt.AlignRight | Qt.AlignBottom)
self.show()
def resizeEvent(self, event):
self.clearMask()
def paintEvent(self, event):
super().paintEvent(event)
qp = QPainter(self)
qp.setRenderHint(QPainter.Antialiasing)
qp.translate(.5, .5)
qp.drawRoundedRect(self.rect().adjusted(0, 0, -1, -1),
self.borderRadius, self.borderRadius)
def mousePressEvent(self, event):
if self.draggable and event.button() == Qt.RightButton:
self.mousePressPos = event.pos()
if event.button() == Qt.LeftButton:
self.first_mouse_location = (event.x(), event.y())
self.band = ResizableRubberBand(self)
self.band.setGeometry(event.x(), event.y(), 0, 0)
super(ResizableRubberBand, self).mousePressEvent(event)
def mouseMoveEvent(self, event):
if self.draggable and event.buttons() & Qt.RightButton:
diff = event.pos() - self.mousePressPos
if not self.dragging:
if diff.manhattanLength() > self.dragging_threshold:
self.dragging = True
if self.dragging:
geo = self.geometry()
parentRect = self.parent().rect()
geo.translate(diff)
if not parentRect.contains(geo):
if geo.right() > parentRect.right():
geo.moveRight(parentRect.right())
elif geo.x() < parentRect.x():
geo.moveLeft(parentRect.x())
if geo.bottom() > parentRect.bottom():
geo.moveBottom(parentRect.bottom())
elif geo.y() < parentRect.y():
geo.moveTop(parentRect.y())
self.move(geo.topLeft())
if event.buttons() & Qt.LeftButton:
first_mouse_location_x = self.first_mouse_location[0]
first_mouse_location_y = self.first_mouse_location[1]
new_x, new_y = event.x(), event.y()
difference_x = new_x - first_mouse_location_x
difference_y = new_y - first_mouse_location_y
self.band.resize(difference_x, difference_y)
super(ResizableRubberBand, self).mouseMoveEvent(event)
def mouseReleaseEvent(self, event):
if self.mousePressPos is not None:
if event.button() == Qt.RightButton and self.dragging:
event.ignore()
self.dragging = False
self.mousePressPos = None
super(ResizableRubberBand, self).mouseReleaseEvent(event)
class mQLabel(QLabel):
def __init__(self, parent=None):
QLabel.__init__(self, parent)
self.setContentsMargins(0,0,0,0)
self.setAlignment(Qt.AlignTop | Qt.AlignLeft)
self.setSizePolicy(QSizePolicy.MinimumExpanding, QSizePolicy.MinimumExpanding)
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self.first_mouse_location = (event.x(), event.y())
self.band = ResizableRubberBand(self)
self.band.setGeometry(event.x(), event.y(), 0, 0)
def mouseMoveEvent(self, event):
if event.buttons() & Qt.LeftButton:
first_mouse_location_x = self.first_mouse_location[0]
first_mouse_location_y = self.first_mouse_location[1]
new_x, new_y = event.x(), event.y()
difference_x = new_x - first_mouse_location_x
difference_y = new_y - first_mouse_location_y
self.band.resize(difference_x, difference_y)
class App(QWidget):
def __init__(self):
super().__init__()
## Set main window attributes
self.setFixedSize(1000,600)
# Add Label
self.label = mQLabel()
self.label.setStyleSheet("border: 1px solid black;")
self.label_layout = QHBoxLayout(self)
self.label_layout.addWidget(self.label)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())
I'm trying to figure it out for 2 hours but I can't really seem to figure out what causes the unnecessary movement. My best guess is it is coming from the mouseMoveEvent but I'm not quite sure if it is from the parent QRubberband or from the QRubberband inside. I hope someone can figure out what is happening here.
The problem is the call to the base implementation of mouse events, which by default gets propagated to the parent for widgets that do not directly implement them, including QRubberBand, which normally doesn't intercept mouse events at all (which we restored by disabling the relative window attribute).
Since the parent itself is a rubber band, it will be moved itself too, making the movement recursive for the child, since it receives a mouse move exactly due to the fact that its been moved: remember that if a widget is moved and the mouse doesn't directly follow the same movement, it will potentially receive a mouse move event relative to its new position.
You can either return before calling it when you handle it, or not call it at all, depending on your needs.
The important thing is that it's consistent (especially for press and move), otherwise a widget could receive a mouse move without receiving the mouse press, which will crash as the variables have not been set yet.
Be aware that if you're in the process of making a more advanced editor for clipping/selections, drawing, etc, you should really consider using the Graphics View Framework: while much more complex and with a more steep learning curve, you'll soon find out that continuing development on basic QWidgets becomes gradually much more convoluted and difficult, to a point where it is really hard to fix things, especially if you're going to deal with image scaling or even basic scroll and zoom.
QWidget and QLabel implementations are not intended for image management, not even simple editing, and custom placed/painted/nested widgets are often difficult to deal with. Consider that doing a similar selection tool would have been much more easy in a graphics scene: for instance, the moving implementation would be almost completely unnecessary, as it's enough to set a simple flag to make an item movable.

How does the ship object know to get displayed centred at the bottom of the main screen surface?

These are the relevant source files:
1. ship.py
#!/usr/bin/python3
import pygame
class Ship(object):
"""A class to manage the ship."""
def __init__(self, ai_game):
"""Initialize the ship and set its starting position."""
self.screen = ai_game.screen
self.screen_rect = ai_game.screen.get_rect()
# Load the ship image and get its rect.
self.image = pygame.image.load('images/ship.bmp')
self.rect = self.image.get_rect()
# Start each new ship at the bottom center of the screen.
self.rect.midbottom = self.screen_rect.midbottom
def blitme(self):
"""Draw the ship at its current location."""
self.screen.blit(self.image, self.rect)
2. alien_invasion.py
#!/usr/bin/python3
import pygame
from sys import exit
from settings import Settings
from ship import Ship
class AlienInvasion(object):
"""Overall class to manage game assets and behavior."""
def __init__(self):
"""Initialize the game, and create game resources."""
pygame.init()
self.settings = Settings()
self.screen = pygame.display.set_mode((self.settings.screen_width, self.settings.screen_height))
pygame.display.set_caption("Alien Invasion")
self.ship = Ship(self)
def run_game(self):
"""Start the main loop for the game"""
while True:
# Watch for keyboard and mouse events. ** The Event Loop **
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
# Redraw the screen during each pass through the loop
self.screen.fill(self.settings.bg_color)
self.ship.blitme()
# Make the most recently drawn screen visible.
pygame.display.flip()
if __name__ == '__main__':
# Make a game instance, and run the game.
ai = AlienInvasion()
ai.run_game()
Running alien_invasion.py produces the following output...
...which is the desired output. I just don't understand how the ship's rect is positioned (correctly) centered at the bottom when the Ship.blitme() method takes parameters image and rect (and not rect.midbottom). To my understanding, this doesn't use the self.rect.midbottom attribute anywhere to draw the image on screen after defining it.
TIA!
When you create an instance of the ship class, you move its rect to the bottom middle of the screen with self.rect.midbottom = self.screen_rect.midbottom. This moves rect.x and rect.y as well, so when you blit the ship with the rect, it is already positioned right

Positioning an Entity in a Class (Pygame)

I'm trying to blit an image of grass onto my game in a specific area, but I'm having trouble doing so because it's in a class. I want to do something like this..
grass_platform.draw(screen, (200, 200))
NOTE: I know this actually doesn't work
Here's my code...
import pygame
pygame.init()
#Screen Size
screen_size = [1024,576]
#Display Window
screen = pygame.display.set_mode(screen_size)
pygame.display.set_caption('The Adventures of Fresco the Explorer')
#Clock
clock = pygame.time.Clock()
#Colors
black = (0,0,0)
white = (255,255,255)
#Game Start
gameStart = False
#Backgrounds
forest = pygame.image.load('forest.png')
#Gravity
gravity =-10
fall = True
#Player
fresco = pygame.image.load('fresco v2.png').convert()
fresco = pygame.transform.scale(fresco,(32,136))
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, filename):
pygame.sprite.Sprite.__init__(self)
self.x = x
self.y = y
self.image = fresco
self.rect = self.image.get_rect()
def update (self):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.rect.x -= 6
self.image = pygame.transform.flip(fresco, True, False)
elif event.key == pygame.K_d:
self.rect.x += 6
self.image = pygame.transform.flip(fresco, False, False)
elif event.key == pygame.K_w:
self.rect.y -= 20
def draw(self, screen):
screen.blit(self.image, self.rect)
player = Player(0, 0, 'fresco v2.png')
#Grass Platform
grass = pygame.image.load('grass.png')
grass = pygame.transform.scale(grass, (90, 90))
class Grass (pygame.sprite.Sprite):
def __init__(self, filename):
self.image = grass
self.rect = self.image.get_rect()
def draw(self, screen):
screen.blit(self.image,self.rect)
grass_platform = Grass('grass.png')
#Game Loop
while not gameStart:
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameStart = True
#Background Blitted
screen.blit(forest,(0,0))
#Falling Event
if fall == True:
if gravity:
player.rect.y -= gravity
#Class Blitted
player.update()
player.draw(screen)
grass_platform.draw(screen)
#Updates Screen
pygame.display.update()
#FPS
clock.tick(30)
pygame.quit()
I dont know if i understood you correctly but if you mean just passing a coordinate parameter and blitting to that position that should be quite easy.
class Grass (pygame.sprite.Sprite):
def __init__(self, filename):
self.image = grass
self.rect = self.image.get_rect()
def draw(self, screen, pos):
screen.blit(self.image, pos)
and then when you draw() you just do this:
grass_platform.draw(screen, (300, 200))
or on the other hand if you want to keep the position you could add
this code to the init method:
def __init__(self, filename, x, y):
self.x = x
self.y = y
and then acces it later
def draw(self, screen):
window.blit(img, (self.x, self.y))
Some notes:
Player and Grass are subclasses from Sprite, so they have already a draw function which does exactly what you are doing. Just remove them.
the following part
#Falling Event
if fall == True:
if gravity:
player.rect.y -= gravity
is specific to the Player class and should be moved to the Player class' update function
When using the Sprite class, the rect attribute is used to store the position of the sprite, hence the x and y attribute in your Player class don't make much sense. Remove them, and use the x and y parameters to change the x and y attribute of the rect instead:
self.rect = self.image.get_rect(x=x, y=y)
Hence, if you want to draw grass_platform at a specific position, just alter it's rect, e.g.:
grass_platform.rect.topleft = (200, 200)
instead of calling draw and update on each of your sprites manually, just but them into a Group, and call draw and update on that Group.

Pygame Obstacles

I am working on a Pygame game. The premise of the game is you must dodge obstacles that start at the bottom of the screen and move upward to create the illusion the player is falling. I already have a player sprite and need help with the obstacle class. When I try to initialize my Obstacle class, I get an error.
class Obstable(pg.sprite.Sprite):
def __init__(self, color, width):
pg.sprite.Sprite.__init__(self)
self.image = pygame.surface([width, 50])
self.image.fill(black)
self.rect = self.image.get_rect()
BLACK = (0,0,0)
obst1 = Obstacle(BLACK, 100)
The class is named Obstable, you instantiate it as Obstacle. Simple Typo.
Would be even easier if you included the NameError Exception. Saying "I get an Error" is not helpful.
Firstly, your class is named Obstable. See the problem? You will need to change it to Obstacle. This should fix problem number one. Once you fix this, and run your code, you should run into another mistake.
Your second problem appears to be that you are referencing an undefined variable in the Obstacle class.
class Obstable(pg.sprite.Sprite):
def __init__(self, color, width):
pg.sprite.Sprite.__init__(self)
self.image = pygame.surface([width, 50])
self.image.fill(black)
self.rect = self.image.get_rect()
The error is here:
self.image.fill(black)
The variable black is undefined. Instead, you need to change black to self.color, but before you can do that, you must initialize self.color. I fixed your code for you:
class Obstacle(pg.sprite.Sprite):
def __init__(self, color, width):
self.color = color
pg.sprite.Sprite.__init__(self)
self.image = pygame.surface([width, 50])
self.image.fill(self.color)
self.rect = self.image.get_rect()
BLACK = (0,0,0)
obst1 = Obstacle(BLACK, 100)
Hopefully my answer was of assistance to you!

how do i determine a sprite's position in pygame?

I'm new to pygame and am making a zombie running game. I am controlling the human with the arrow keys and I would like to have a zombie run toward the player's position when the zombie is first created. I am unsure how to determine the players position to set the zombie in the correct direction. The zombie doesn't have to follow the human, just head in the direction of the players position when the zombie is created.
class Human(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("human.jpg")
self.image=self.image.convert()
self.rect=self.image.get_rect()
self.x=100
self.y=430
self.dx=50
self.dy=0
def update(self):
keys=pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
self.x-=self.dx
if keys[pygame.K_RIGHT]:
self.x+=self.dx
self.rect.center=(self.x,self.y)
class ZombieChase(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image=pygame.image.load("zombie.jpg")
self.image=self.image.convert()
self.rect=self.image.get_rect()
self.reset()
self.dx=15
self.dy=15
#self.xStart=random.randrange(0,screen.get_width())
def update(self):
self.rect.centery+=self.dy
self.rect.centerx+=self.dx
if self.rect.top>screen.get_height():
self.reset()
if self.rect.right>screen.get_width():
self.reset()
if self.rect.left<0:
self.reset()
def reset(self):
self.rect.top=0
speed=random.randrange(20,36)
xStart=random.randrange(0,screen.get_width())
yStart=0
#humanx, humany !!!!!!this is where i don't know what to put!!!!!
try:
self.dx=(humanx-xStart)/speed
except ZeroDivisionError:
self.dx=0
try:
self.dy=(humany-yStart)/speed
except ZeroDivisionError:
self.dy=1
self.rect.centerx=xStart
Sorry if the tabbing isn't right.
If you subclass pygame.sprite.Sprite the location is zombie.rect . For coords, zombie.rect.center or zombie.rect.topleft

Resources