so I can't change the Shape of the players character from a rectangle to a circle. Beforehand it said that there was one argument missing so we added it and then it said there is one too much. Now it says something different which I don't remember anymore could any body please looked over the code provided.
Thanks for your help!
import pygame
import turtle
import time
import math
import random
import sys
import os
pygame.init()
WHITE = (255,255,255)
GREEN = (0,255,0)
BGColor = (117,168,55)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
MOVE = 2.5
size = (1200, 620)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Zombie Game")
class Char(pygame.sprite.Sprite):
def __init__(self, color, pos, radius, width):
super().__init__()
self.image = pygame.Surface([radius, width])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.circle(self.image, color, [0, 0], radius, width)
self.circle = self.image.get_circle()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, pixels):
self.rect.y -= pixels
def moveDown(self, pixels):
self.rect.y += pixels
all_sprites_list = pygame.sprite.Group()
playerChar = Char(BLUE, [0, 0], 30, 0)
playerChar.rect.x = 0
playerChar.rect.y = 0
all_sprites_list.add(playerChar)
carryOn = True
clock = pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x:
carryOn=False
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
playerChar.moveLeft(MOVE)
if keys[pygame.K_d]:
playerChar.moveRight(MOVE)
if keys[pygame.K_w]:
playerChar.moveUp(MOVE)
if keys[pygame.K_s]:
playerChar.moveDown(MOVE)
screen.fill(BGColor)
pygame.display.flip()
clock.tick(60)
pygame.quit()
If you want to draw a circle with a radius to surface, then you've t o create a surface with the doubled width and height of the radius:
self.image = pygame.Surface([radius*2, radius*2])
To keep the class working, you still have to set the mebmer self.rect:
self.rect = self.image.get_rect()
Finally the surface tha to be blit to the screen Surface:
screen.blit(playerChar.image,playerChar.rect)
See the example, where I appied the suggestions to your original code:
import pygame
import turtle
import time
import math
import random
import sys
import os
pygame.init()
WHITE = (255,255,255)
GREEN = (0,255,0)
BGColor = (117,168,55)
RED = (255,0,0)
BLUE = (0,0,255)
BLACK = (0,0,0)
MOVE = 2.5
size = (1200, 620)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Zombie Game")
class Char(pygame.sprite.Sprite):
def __init__(self, color, pos, radius, width):
super().__init__()
self.image = pygame.Surface([radius*2, radius*2])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
pygame.draw.circle(self.image, color, [radius, radius], radius, width)
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
pass
def moveLeft(self, pixels):
self.rect.x -= pixels
pass
def moveUp(self, pixels):
self.rect.y -= pixels
pass
def moveDown(self, pixels):
self.rect.y += pixels
pass
all_sprites_list = pygame.sprite.Group()
playerChar = Char(BLUE, [0, 0], 30, 0)
playerChar.rect.x = 0
playerChar.rect.y = 0
all_sprites_list.add(playerChar)
carryOn = True
clock = pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x:
carryOn=False
keys = pygame.key.get_pressed()
if keys[pygame.K_a]:
playerChar.moveLeft(MOVE)
if keys[pygame.K_d]:
playerChar.moveRight(MOVE)
if keys[pygame.K_w]:
playerChar.moveUp(MOVE)
if keys[pygame.K_s]:
playerChar.moveDown(MOVE)
screen.fill(BGColor)
screen.blit(playerChar.image,playerChar.rect)
pygame.display.flip()
clock.tick(60)
pygame.quit()
Related
I am writing a simple invaders game. To add damage to the bases I figured I could blit a small, black surface on the base at bullet impact, and use a mask to check if the bullet was on the damage or the base, but it isn't working and I feel I am misunderstanding the mask. The first collision is detected but after that it also detects a collision but doesn't put any more damage on the base. I thought because the surface was black the base mask wouldn't include it, but it isn't working. Here is a short test to demo this. Press space (or any key) to fire a bullet at the base. I thought maybe I should generate a new mask for the base but that doesn't work. The mask collide is from the pygame sprite code on github.
import sys, pygame, random
from pygame.locals import *
screenwidth = 600
screenheight = 400
pygame.init()
screen = pygame.display.set_mode((screenwidth, screenheight))
pygame.display.set_caption("shoot 'em up")
screenrect = screen.get_rect()
black = (0, 0, 0)
blue = (10, 10, 255)
yellow = (238, 238, 0)
base_width = 80
base_height = 40
bullet_width = 3
bullet_height = 10
class Bullet(pygame.Surface):
def __init__(self, point):
super().__init__((bullet_width, bullet_height), pygame.SRCALPHA)
self.rect = self.get_rect()
self.rect.midbottom = point
self.fill(yellow)
self.velocity = -5
self.alive = True
self.mask = pygame.mask.from_surface(self)
def update(self):
self.rect.top += self.velocity
def draw(self, surf):
surf.blit(self, self.rect)
class Base(pygame.Surface):
def __init__(self, x, y, colour):
super().__init__((base_width, base_height), pygame.SRCALPHA)
self.rect = self.get_rect()
self.rect.x = x
self.rect.y = y
self.fill(colour)
self.alive = True
def add_damage(self, bullet):
width = random.randint(3, 6)
height = random.randint(8, 12)
damage = pygame.Surface((width, height), pygame.SRCALPHA)
damage.fill(black)
rect = damage.get_rect()
rect.x = bullet.rect.x - self.rect.x
rect.y = bullet.rect.top - self.rect.top
self.blit(damage, rect)
#self.mask = pygame.mask.from_surface(self)
def draw(self, surf):
surf.blit(self, self.rect)
class Test(pygame.Surface):
def __init__(self):
super().__init__((600, 400))
self. base = Base(50, 300, blue)
self.bullets = []
def run(self):
while 1:
self.get_events()
self.update()
self.draw()
def get_events(self):
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
if event.type == pygame.KEYDOWN:
bullet = Bullet((60, 380))
self.bullets.append(bullet)
def update(self):
if self.bullets:
for bullet in self.bullets:
bullet.update()
self.collision_check(bullet)
for bullet in self.bullets:
if not bullet.alive:
self.bullets.remove(bullet)
def collision_check(self, bullet):
if bullet.rect.colliderect(self.base):
if self.collide_mask(bullet, self.base):
print("collide")
self.base.add_damage(bullet)
bullet.alive = False
def collide_mask(self, left, right):
xoffset = right.rect[0] - left.rect[0]
yoffset = right.rect[1] - left.rect[1]
try:
leftmask = left.mask
except AttributeError:
leftmask = pygame.mask.from_surface(left)
try:
rightmask = right.mask
except AttributeError:
rightmask = pygame.mask.from_surface(right)
return leftmask.overlap(rightmask, (xoffset, yoffset))
def draw(self):
self.fill(black)
self.base.draw(self)
for bullet in self.bullets:
bullet.draw(self)
screen.blit(self, (0,0))
pygame.display.flip()
if __name__=="__main__":
t = Test()
t.run()
As you can see this is not using pygame sprites.
if the pygame.Surface object is changed you need to recreate the mask with pygame.mask.from_surface. However, the mask is generated form the Surface's alpha channel. Therefore, you need to make the damaged area transparent. Create a completely transparent rectangle (RGBA = 0, 0, 0, 0) and blit the rectangle using the special flag BLEND_RGBA_MULT (or BLEND_RGBA_MIN). Finally recreate the mask:
damage = pygame.Surface((width, height), pygame.SRCALPHA)
self.blit(damage, rect, special_flags=pygame.BLEND_RGBA_MULT)
self.mask = pygame.mask.from_surface(self)
add_damage Mehtod:
class Base(pygame.Surface):
# [...]
def add_damage(self, bullet):
width = random.randint(3, 6)
height = random.randint(8, 12)
damage = pygame.Surface((width, height), pygame.SRCALPHA)
rect = damage.get_rect()
rect.x = bullet.rect.x - self.rect.x
rect.y = bullet.rect.top - self.rect.top
self.blit(damage, rect, special_flags=pygame.BLEND_RGBA_MULT)
self.mask = pygame.mask.from_surface(self)
I've completed a small game project of the breakout game, but my main problem is that the ball seems to get stuck going side to side much more often than i would be okay with it happening. It's definitely a problem with the ball velocity and I've tried messing around with the numbers but I can't seem to make it stop getting stuck. Any ideas on how to fix it ,or even improve any of the code, would be greatly appreciated.
import sys
import pygame as pg
from random import randint
black = (0,0,0)
white = (255,255,255)
red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)
score = 0
lives = 3
def run_game():
pg.init()
screen = pg.display.set_mode((800,600))
pg.display.set_caption("Brick Breaker")
bg_color = (0, 0, 0)
screen.fill(bg_color)
class Paddle(pg.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pg.Surface([width, height])
self.image.fill(black)
self.image.set_colorkey(black)
pg.draw.rect(self.image, color, [0, 0, width, height])
self.rect = self.image.get_rect()
def moveLeft(self,pixels):
self.rect.x -= pixels
if self.rect.x < 0:
self.rect.x = 0
def moveRight(self,pixels):
self.rect.x += pixels
if self.rect.x > 700:
self.rect.x = 700
class Ball(pg.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pg.Surface([width, height])
self.image.fill(black)
self.image.set_colorkey(black)
pg.draw.rect(self.image, color, [0, 0, width, height])
self.velocity = [randint(4,8),randint(-8,8)]
self.rect = self.image.get_rect()
def update(self):
self.rect.x += self.velocity[0]
self.rect.y += self.velocity[1]
def bounce(self):
self.velocity[0] = -self.velocity[0]
self.velocity[1] = randint(-8,10)
class Brick(pg.sprite.Sprite):
def __init__(self, color, width, height):
super().__init__()
self.image = pg.Surface([width, height])
self.image.fill(black)
self.image.set_colorkey(black)
pg.draw.rect(self.image, color, [0, 0, width, height])
self.rect = self.image.get_rect()
clock = pg.time.Clock()
all_sprites_list = pg.sprite.Group()
paddle = Paddle(white,100,10)
paddle.rect.x = 350
paddle.rect.y = 560
ball = Ball(white,10,10)
ball.rect.x = 345
ball.rect.y = 195
all_bricks = pg.sprite.Group()
for i in range(7):
brick = Brick(red,80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 60
all_sprites_list.add(brick)
all_bricks.add(brick)
for i in range(7):
brick = Brick(blue,80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 100
all_sprites_list.add(brick)
all_bricks.add(brick)
for i in range(7):
brick = Brick(green,80,30)
brick.rect.x = 60 + i* 100
brick.rect.y = 140
all_sprites_list.add(brick)
all_bricks.add(brick)
all_sprites_list.add(paddle)
all_sprites_list.add(ball)
while True:
for event in pg.event.get():
if event.type == pg.QUIT:
sys.exit()
keys = pg.key.get_pressed()
if keys[pg.K_LEFT]:
paddle.moveLeft(10)
if keys[pg.K_RIGHT]:
paddle.moveRight(10)
all_sprites_list.update()
if ball.rect.x>=790:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.x<=0:
ball.velocity[0] = -ball.velocity[0]
if ball.rect.y>590:
ball.velocity[1] = -ball.velocity[1]
global lives
lives-=1
if lives == 0:
font = pg.font.Font(None, 74)
text = font.render("YOU LOSE", 1, red)
screen.blit(text,(280,250))
pg.display.flip()
sys.exit()
if ball.rect.y<40:
ball.velocity[1] = -ball.velocity[1]
if pg.sprite.collide_mask(ball, paddle):
ball.rect.x -= ball.velocity[0]
ball.rect.y -= ball.velocity[1]
ball.bounce()
brick_collision_list = pg.sprite.spritecollide(ball,all_bricks,False)
for brick in brick_collision_list:
ball.bounce()
brick.kill()
if len(all_bricks)==0:
font = pg.font.Font(None, 74)
text = font.render("YOU WIN", 1, green)
screen.blit(text,(280,250))
pg.display.flip()
sys.exit()
screen.fill(black)
font = pg.font.Font(None, 40)
text = font.render("lives: " + str(lives), 1, white)
screen.blit(text, (10,10))
all_sprites_list.draw(screen)
pg.display.flip()
clock.tick(50)
run_game()
When the ball hits the paddle, then set the bottom of the ball to the top of the paddle. It is not necessary to change the direction of the ball, because it's direction is changed in ball.bounce()
if pg.sprite.collide_mask(ball, paddle):
ball.rect.bottom = paddle.rect.top
#ball.rect.x -= ball.velocity[0]
#ball.rect.y -= ball.velocity[1]
ball.bounce()
In Ball.bounce invert the y direction of the ball and compute a new random x direction:
class Ball(pg.sprite.Sprite):
def __init__(self, color, width, height):
# [...]
self.velocity = [randint(-8,8), randint(4,8)]
def bounce(self):
# invert y
self.velocity[1] = -self.velocity[1]
# either random x
# self.velocity[0] = randint(-8, 8)
# or slightly changed x
self.velocity[0] = max(-8, min(8, self.velocity[0] + randint(-3, 3)))
I am using pygame and following a tutorial HERE. I have the following code:
import pygame
import sys
#Let's import the Car Class
from player import Car
pygame.init()
GREEN = (20, 255, 140)
GREY = (210, 210 ,210)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
PURPLE = (255, 0, 255)
SCREENWIDTH=400
SCREENHEIGHT=500
size = (SCREENWIDTH, SCREENHEIGHT)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Car Racing")
#This will be a list that will contain all the sprites we intend to use in our game.
all_sprites_list = pygame.sprite.Group()
playerCar = Car(RED, 20, 30)
playerCar.rect.x = 200
playerCar.rect.y = 300
# Add the car to the list of objects
all_sprites_list.add(playerCar)
#Allowing the user to close the window...
carryOn = True
clock=pygame.time.Clock()
while carryOn:
for event in pygame.event.get():
if event.type==pygame.QUIT:
carryOn=False
elif event.type==pygame.KEYDOWN:
if event.key==pygame.K_x: #Pressing the x Key will quit the game
carryOn=False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
playerCar.moveLeft(5)
if keys[pygame.K_RIGHT]:
playerCar.moveRight(5)
if keys[pygame.K_UP]:
playerCar.moveUp(5)
#Game Logic
all_sprites_list.update()
#Drawing on Screen
screen.fill(GREEN)
#Draw The Road
pygame.draw.rect(screen, GREY, [40,0, 200,300])
#Draw Line painting on the road
pygame.draw.line(screen, WHITE, [140,0],[140,300],5)
#Now let's draw all the sprites in one go. (For now we only have 1 sprite!)
all_sprites_list.draw(screen)
#Refresh Screen
pygame.display.flip()
#Number of frames per secong e.g. 60
clock.tick(60)
pygame.quit()
The
if keys[pygame.K_UP]:
playerCar.moveUp(5)
is my own contribution. I also have my my sprite class:
import pygame
WHITE = (255, 255, 255)
class Car(pygame.sprite.Sprite):
#This class represents a car. It derives from the "Sprite" class in Pygame.
def __init__(self, color, width, height):
# Call the parent class (Sprite) constructor
super().__init__()
# Pass in the color of the car, and its x and y position, width and height.
# Set the background color and set it to be transparent
self.image = pygame.Surface([width, height])
self.image.fill(WHITE)
self.image.set_colorkey(WHITE)
# Draw the car (a rectangle!)
pygame.draw.rect(self.image, color, [0, 0, width, height])
# Instead we could load a proper pciture of a car...
# self.image = pygame.image.load("car.png").convert_alpha()
# Fetch the rectangle object that has the dimensions of the image.
self.rect = self.image.get_rect()
def moveRight(self, pixels):
self.rect.x += pixels
def moveLeft(self, pixels):
self.rect.x -= pixels
def moveUp(self, pixels, y):
self.rect.x += pixels
The
def moveUp(self, pixels, y):
self.rect.x += pixels
is my own contribution. Before when my contribution was:
def moveUp(self, pixels):
self.rect.x += pixels
my sprite moved right when I pressed the up arrow. With the code as it is now I get the error:
TypeError: moveUp() missing 1 required positional argument: 'y'
How do I fix this and get my sprite to move up when I press the up arrow?
You want to change self.rect.x to self.rect.y:
Try changing the line
def moveUp(self, pixels, y):
self.rect.x += pixels
to this:
def moveUp(self, pixels):
self.rect.y += pixels
I am currently trying to develop a small, basic game using Pygame. However, I am not able to draw my sprite object onto the screen. My code is below:
import pygame
# window
WIDTH = 400
HEIGHT = 400
FPS = 60
# colors
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
PINK = (255, 0, 191)
BROWN = (102, 51, 0)
GRAY = (102, 102, 51)
# player
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50,50))
self.image.fill(GREEN)
# variables needed for movement
self.rect = self.image.get_rect()
self.rect.centerx = 200
self.rect.bottom = 200
self.speedx = 0
self.speedy = 0
def update(self):
# move sprite
self.speedx = 0
keystate = pygame.key.get_pressed()
if keystate[pygame.K_w]:
self.speedx = -5
if keystate[pygame.K_d]:
self.speedx = 5
self.rect.x += self.speedx
# boundary collision
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x < 400:
self.rect.x = 400
# initialisation
pygame.init()
# sound
pygame.mixer.init()
# draw window
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("Shoot")
clock = pygame.time.Clock()
# sprite group
all_sprites = pygame.sprite.Group()
# create player
player = Player()
# add sprites to sprite group
all_sprites.add(player)
# game loop
running = True
while running:
# keeps speed constant
clock.tick(FPS)
# allows for exit
for event in pygame.event.get():
# check before closing window
if event.type == pygame.QUIT:
running = False
# update sprites
all_sprites.update()
# draw screen
#screen.fill(BLACK)
all_sprites.draw(screen)
#player.draw(screen)
# used to draw
pygame.display.flip()
pygame.quit()
As you can see in the code, I have a player object which I create from the Player class. I then add this to an all_sprites group. This should then be drawn on the screen. However, nothing is drawn
The issue is that the rectangle of the Player object is out of the window, because of if self.rect.x < 400: self.rect.x = 400 in:
class Player(pygame.sprite.Sprite):
# [...]
def update(self):
# [...]
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x < 400:
self.rect.x = 400
It has to be self.rect.x > 400:
class Player(pygame.sprite.Sprite):
# [...]
def update(self):
# [...]
if self.rect.x < 0:
self.rect.x = 0
elif self.rect.x > 400: # <----
self.rect.x = 400
Hello I am new to pygame and I am trying to write a shmup game.
However I am always having this error:
TypeError: add() argument after * must be an iterable, not int
self.add(*group)
This is the traceback of the error:
File "C:/Users/Pygame/game.py", line 195, in
player.shoot()
File "C:/Users/Pygame/game.py", line 78, in shoot
bullet = Bullets(self.rect.center,self.angle)
File "C:/Users/Pygame/game.py", line 124, in init
super(Bullets,self).init(pos,angle)
This is the code I have written so far, it works well however when the user wants to shoot the error is being raised.
import os
import pygame
import random
import math
WIDTH = 480
HEIGHT = 600
FPS = 60
#colors:
WHITE = (255,255,255)
BLACK = (0,0,0)
GREEN = (0,250,0)
RED = (255,0,0)
BLUE = (0,0,255)
YELLOW = (255,255,0)
#setup assets
game_folder = os.path.dirname("C:/Users/PygameP/")
img_folder = os.path.join(game_folder,"img")
#intialise pygame
pygame.init()
pygame.mixer.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface((50,40))
self.image.fill(GREEN)
self.rect = self.image.get_rect()
self.rect.centerx = WIDTH/2
self.rect.bottom = HEIGHT-10
#controls the speed
self.angle = 0
self.orig_image = self.image
#self.rect = self.image.get_rect(center=pos)
def update(self):
keystate = pygame.key.get_pressed()
if keystate[pygame.K_LEFT]:
self.angle -= 5
self.rotate()
if keystate[pygame.K_RIGHT]:
self.angle += 5
self.rotate()
def rotate(self):
self.image = pygame.transform.rotozoom(self.orig_image, self.angle, 1)
self.rect = self.image.get_rect(center=self.rect.center)
def shoot(self):
bullet = Bullets(self.rect.center,self.angle)
all_sprites.add(bullet)
bullets.add(bullet)
class Mob(pygame.sprite.Sprite):
def __init__(self):
super(Mob,self).__init__()
self.image = pygame.Surface((30,40))
self.image = meteor_img
self.image = pygame.transform.scale(meteor_img,(50,38))
self.image.set_colorkey(BLACK)
self.rect = self.image.get_rect()
self.radius = int(self.rect.width/2)
self.rect.x = random.randrange(0,WIDTH - self.rect.width)
self.rect.y = random.randrange(-100,-40)
self.speedy = random.randrange(1,8)
#updating the position of the sprite
def update(self):
self.rect.y += self.speedy
if self.rect.top > HEIGHT + 10:
self.rect.x = random.randrange(0,WIDTH - self.rect.width)
self.rect.y = random.randrange(-100,-40)
self.speedy = random.randrange(1,8)
class Bullets(pygame.sprite.Sprite):
def __init__(self,pos,angle):
super(Bullets,self).__init__(pos,angle)
# Rotate the image.
self.image = pygame.Surface((10,20))
self.image = bullet_img
self.image = pygame.transform.scale(bullet_img,(50,38))
self.image = pygame.transform.rotate(bullet_img, angle)
self.rect = self.image.get_rect()
speed = 5
self.velocity_x = math.cos(math.radians(-angle))*speed
self.velocity_y = math.sin(math.radians(-angle))*speed
#store the actual position
self.pos = list(pos)
def update(self):
self.pos[0] += self.velocity_x
self.pos[1] += self.velocity_y
self.rect.center = self.pos
if self.rect.bottom <0:
self.kill()
#load all game graphics
background = pygame.image.load(os.path.join(img_folder,"background.png")).convert()
background_rect = background.get_rect()
player_img = pygame.image.load(os.path.join(img_folder,"arrow.png")).convert()
bullet_img = pygame.image.load(os.path.join(img_folder,"bullet.png")).convert()
meteor_img = pygame.image.load(os.path.join(img_folder,"m.png")).convert()
#creating a group to store sprites to make it easier to deal with them
#every sprite we make goes to this group
all_sprites = pygame.sprite.Group()
mobs = pygame.sprite.Group()
bullets = pygame.sprite.Group()
player = Player()
all_sprites.add(player)
for i in range(8):
m = Mob()
all_sprites.add(m)
mobs.add(m)
running = True
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
player.shoot()
#Update
all_sprites.update()
#checking if a bullet hits a mob
hits = pygame.sprite.groupcollide(mobs,bullets,True,True)
for hit in hits:
m = Mob()
all_sprites.add(m)
mobs.add(m)
hits = pygame.sprite.spritecollide(player,mobs, False,pygame.sprite.collide_circle)
#drawing the new sprites here
screen.fill(BLACK)
#show the background image
screen.blit(background,background_rect)
all_sprites.draw(screen)
pygame.display.flip()
pygame.quit()
Any comments?
You're passing the pos and the angle to the __init__ method of pygame.sprite.Sprite here,
super(Bullets,self).__init__(pos,angle)
but you can only pass sprite groups to which this sprite instance will be added. So just remove those arguments:
super(Bullets,self).__init__()