Pygame shooting in all directions - python

I am trying to make a space invaders/asteroids game where you can shoot in all directions using pygame. Currently, I can move my ship in all directions and shoot in all directions, but as soon as I change directions my bullets that were previously fired will stop moving/move in the direction that the ship is going. Thank you in advance.
import pygame
import random
# intialize the pygame
pygame.init()
# dimensions of the pygame
screen = pygame.display.set_mode((800,600))
# Caption and Icon
pygame.display.set_caption("Saving Earth")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
# images
earth_image = pygame.image.load('earth.png')
fly_up = pygame.image.load('spaceshipU.png')
fly_down = pygame.image.load('spaceshipD.png')
fly_left = pygame.image.load('spaceshipL.png')
fly_right = pygame.image.load('spaceshipR.png')
background = pygame.image.load('space.jpg')
clock = pygame.time.Clock()
#Player class
class player(object):
def __init__(self, player_x, player_y, width, height):
self.player_x = player_x
self.player_y = player_y
self.width = width
self.height = height
self.speed = 4
self.left = False
self.right = False
self.up = True
self.down = False
# draws the ship to screen
def draw(self, screen):
if self.left:
screen.blit(fly_left, (self.player_x,self.player_y))
elif self.right:
screen.blit(fly_right, (self.player_x,self.player_y))
elif self.up:
screen.blit(fly_up, (self.player_x,self.player_y))
elif self.down:
screen.blit(fly_down, (self.player_x,self.player_y))
else:
screen.blit(fly_up, (self.player_x,self.player_y))
class Bullet(object):
def __init__(self, bullet_x, bullet_y, radius, color):
self.bullet_x = bullet_x
self.bullet_y = bullet_y
self.radius = radius
self.color = color
self.speed = 6
self.vertfacing = 1
self.hortfacing = 1
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.bullet_x,self.bullet_y), self.radius)
# redraws game screen
def redraw_game_screen():
screen.blit(background, (0,0))
screen.blit(earth_image, (350, 230))
ship.draw(screen)
for bullet in bullets:
bullet.draw(screen)
pygame.display.update()
#Game Loop
ship = player(300, 300, 32, 32)
bullets = []
vertfacing = -1
hortfacing = 1
running = True
while running:
clock.tick(60)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
for bullet in bullets:
if bullet.bullet_x < 800 and bullet.bullet_x > 0 and bullet.bullet_y < 600 and bullet.bullet_y > 0 :
if hortfacing == -1 and ship.left:
bullet.bullet_x -= bullet.speed
elif hortfacing == 1 and ship.right:
bullet.bullet_x += bullet.speed
elif vertfacing == 1 and ship.down:
bullet.bullet_y += bullet.speed
elif vertfacing == -1 and ship.up:
bullet.bullet_y -= bullet.speed
else:
bullets.remove(bullet)
#key commands
keys = pygame.key.get_pressed()
if keys[pygame.K_SPACE]:
if ship.left:
hortfacing = -1
elif ship.right:
hortfacing = 1
elif ship.up:
vertfacing = -1
elif ship.down:
vertfacing = 1
if len(bullets) < 100:
bullets.append(Bullet(round(ship.player_x + ship.width //2), round(ship.player_y + ship.height//2), 6, (255,165,0)))
if keys[pygame.K_LEFT] and ship.player_x > ship.speed:
ship.player_x -= ship.speed
ship.left = True
ship.right = False
ship.up = False
ship.down = False
elif keys[pygame.K_RIGHT] and ship.player_x < 800 - ship.speed - ship.width:
ship.player_x += ship.speed
ship.right = True
ship.left = False
ship.up = False
ship.down = False
elif keys[pygame.K_UP] and ship.player_y > ship.speed:
ship.player_y -= ship.speed
ship.up = True
ship.down = False
ship.left = False
ship.right = False
elif keys[pygame.K_DOWN] and ship.player_y < 600 - ship.height - ship.speed:
ship.player_y += ship.speed
ship.down = True
ship.up = False
ship.left = False
ship.right = False
redraw_game_screen()
pygame.quit()

You have to se the attributes vertfacing and hortfacing in the class Bullet:
class Bullet(object):
def __init__(self, bullet_x, bullet_y, radius, color, vertfacing, hortfacing):
self.bullet_x = bullet_x
self.bullet_y = bullet_y
self.radius = radius
self.color = color
self.speed = 6
self.vertfacing = vertfacing
self.hortfacing = hortfacing
def draw(self, screen):
pygame.draw.circle(screen, self.color, (self.bullet_x,self.bullet_y), self.radius)
Pass the attributes to the constructor when a Bullet spawns:
if len(bullets) < 100:
bullet = Bullet(round(ship.player_x + ship.width //2),
round(ship.player_y + ship.height//2), 6, (255,165,0),
vertfacing, hortfacing)
bullets.append(bullet)
Use the attributes of bullet (bullet.hortfacing, bullet.vertfacing) when you calculate the new position.
Add a method to the class Bullet that moves it:
class Bullet(object):
# [...]
def move(self):
if self.hortfacing == -1:
self.bullet_x -= self.speed
elif self.hortfacing == 1:
self.bullet_x += self.speed
elif self.vertfacing == 1:
self.bullet_y += self.speed
elif self.vertfacing == -1:
self.bullet_y -= self.speed
Call the method for each bullet:
for bullet in bullets:
if bullet.bullet_x < 800 and bullet.bullet_x > 0 and bullet.bullet_y < 600 and bullet.bullet_y > 0 :
bullet.move()
else:
bullets.remove(bullet)

Related

how would i get the enemy to avoid the characters line at all costs while still trying to eliminate him

How would I get the enemy to avoid the characters line at all costs while still trying to eliminate him? The code I have as far makes the enemy run straight to the player and die pretty much straight away.
Does anyone have any idea how to keep the enemy from touching the players line?
Here is the code:
import random
import pygame, sys
from pygame.locals import *
import time
GREEN=(0,125,0)
BLUE=(0,0,125)
Yplayer = 225/2
Xplayer = 350
playerlinesavedata = []
playercurrent = []
Xplayerchange=0
Yplayerchange=0
Yenemy = 450
Xenemy = 300
enemylinesavedata = []
enemycurrent = []
Xenemychange=0
Yenemychange=0
windowX = 900
windowY = 600
pygame.init()
DISPLAY=pygame.display.set_mode((windowX,windowY),0,32)
def enemymovement():
global Xplayer,Yplayer,Xenemy,Yenemy
enemy1 = True
enemydirection = random.randint(1,4)
# enemy moves toward player
if Xenemy > Xplayer: enemydirection = 1
elif Xenemy < Xplayer: enemydirection = 2
elif Yenemy > Yplayer: enemydirection = 3
elif Yenemy < Yplayer: enemydirection = 4
while enemy1 == True:
if enemydirection == 1:
Xenemychange= -random.randint(1,4)
Xenemy+=Xenemychange
if enemydirection == 2:
Xenemychange= random.randint(1,4)
Xenemy+=Xenemychange
if enemydirection == 3:
Yenemychange= -random.randint(1,4)
Yenemy+=Yenemychange
if enemydirection == 4:
Yenemychange= random.randint(1,4)
Yenemy+=Yenemychange
enemylinesavedata.append([Xenemy,Yenemy])
enemy()
break
def enemy():
for XnY in enemylinesavedata:
pygame.draw.rect(DISPLAY,BLUE, [XnY[0],XnY[1],5,5])
def player():
for XnY in playerlinesavedata:
pygame.draw.rect(DISPLAY,GREEN,[XnY[0],XnY[1],5,5])
def main():
global playerlinesavedata, enemylinesavedata
global Xplayer, Yplayer, Xenemy, Yenemy
WHITE = (255,255,255)
size = 5
clock = pygame.time.Clock()
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type==QUIT:
pygame.quit()
sys.exit()
keys = pygame.key.get_pressed()
Xplayer += (keys[pygame.K_d] - keys[pygame.K_a]) * size
Yplayer += (keys[pygame.K_s] - keys[pygame.K_w]) * size
playerlinesavedata.append((Xplayer, Yplayer))
enemymovement()
player_rectlist = [pygame.Rect(x, y, size, size) for x, y in playerlinesavedata]
enemy_rectlist = [pygame.Rect(x, y, size, size) for x, y in enemylinesavedata]
if player_rectlist[-1].collidelist(enemy_rectlist) >= 0:
pygame.quit()
sys.exit()
if enemy_rectlist[-1].collidelist(player_rectlist) >= 0:
playerlinesavedata = []
Yplayer = 225//2
Xplayer = 350
enemylinesavedata = []
Yenemy = 450
Xenemy = 300
DISPLAY.fill(WHITE)
player()
enemy()
pygame.display.update()
main()
It is a bad practice to use globalize variables. It is much better to use python classes as the pygame objects.
Here is a cleaned up version of your code so that you or other users on Stack Overflow can help you with your problem more easily:
import random
import pygame, sys
from pygame.locals import *
import time
pygame.init()
GREEN = (0, 125, 0)
BLUE = (0, 0, 125)
WHITE = (255,255,255)
windowX = 900
windowY = 600
wn = pygame.display.set_mode((windowX, windowY), 0, 32)
class Object:
def __init__(self, X, Y, enemy=False, color=GREEN):
self.enemy = enemy
self.X = X
self.Y = Y
self.current = []
self.Xchange = 0
self.Ychange = 0
self.direction = 0
self.rectlist = []
self.color = color
self.size = 5
def movement(self):
for rect in self.rectlist:
pygame.draw.rect(wn, self.color, rect)
if self.enemy:
if self.X > player.X:
self.direction = 1
elif self.X < player.X:
self.direction = 2
elif self.Y > player.Y:
self.direction = 3
elif self.Y < player.Y:
self.direction = 4
if self.direction == 1:
self.Xchange = -random.randint(1,4)
self.X += self.Xchange
if self.direction == 2:
self.Xchange = random.randint(1,4)
self.X += self.Xchange
if self.direction == 3:
self.Ychange = -random.randint(1,4)
self.Y += self.Ychange
if self.direction == 4:
self.Ychange = random.randint(1,4)
self.Y += self.Ychange
else:
keys = pygame.key.get_pressed()
player.X += (keys[pygame.K_d] - keys[pygame.K_a]) * player.size
player.Y += (keys[pygame.K_s] - keys[pygame.K_w]) * player.size
self.rectlist.append(pygame.Rect(self.X, self.Y, self.size, self.size))
player = Object(350, 225 / 2)
enemy = Object(450, 300, enemy=True, color=BLUE)
def main():
clock = pygame.time.Clock()
size = 5
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
player.movement()
enemy.movement()
if player.rectlist[-1].collidelist(enemy.rectlist) >= 0:
pygame.quit()
sys.exit()
if enemy.rectlist[-1].collidelist(player.rectlist) >= 0:
player.rectlist = []
player.X = 350
player.Y = 225 // 2
enemy.rectlist = []
enemy.X = 450
enemy.Y = 300
pygame.display.update()
wn.fill(WHITE)
main()
Here is my implementation of how to let the enemy avoid the player, though it is very buggy, as in, it will go through the player's line
most of the time:
import random
import pygame, sys
from pygame.locals import *
import time
pygame.init()
GREEN = (0, 125, 0)
BLUE = (0, 0, 125)
WHITE = (255,255,255)
windowX = 900
windowY = 600
wn = pygame.display.set_mode((windowX, windowY), 0, 32)
class Object:
def __init__(self, X, Y, enemy=False, color=GREEN):
self.enemy = enemy
self.X = X
self.Y = Y
self.current = []
self.Xchange = 0
self.Ychange = 0
self.direction = 0
self.rectlist = []
self.color = color
self.size = 5
self.bounce = 0
def movement(self):
for rect in self.rectlist:
pygame.draw.rect(wn, self.color, rect)
if self.enemy:
enemydirection = random.randint(1,4)
if not self.bounce:
if self.X > player.X:
self.direction = 1
elif self.X < player.X:
self.direction = 2
elif self.Y > player.Y:
self.direction = 3
elif self.Y < player.Y:
self.direction = 4
else:
self.bounce -= 1
if self.direction == 1:
self.Xchange = -random.randint(1,4)
self.X += self.Xchange
if self.direction == 2:
self.Xchange = random.randint(1,4)
self.X += self.Xchange
if self.direction == 3:
self.Ychange = -random.randint(1,4)
self.Y += self.Ychange
if self.direction == 4:
self.Ychange = random.randint(1,4)
self.Y += self.Ychange
else:
keys = pygame.key.get_pressed()
player.X += (keys[pygame.K_d] - keys[pygame.K_a]) * player.size
player.Y += (keys[pygame.K_s] - keys[pygame.K_w]) * player.size
self.rectlist.append(pygame.Rect(self.X, self.Y, self.size, self.size))
player = Object(350, 225 / 2)
enemy = Object(450, 300, enemy=True, color=BLUE)
def main():
clock = pygame.time.Clock()
size = 5
while True:
clock.tick(60)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
player.movement()
enemy.movement()
if player.rectlist[-1].collidelist(enemy.rectlist) >= 0:
pygame.quit()
sys.exit()
if enemy.rectlist[-1].collidelist(player.rectlist) >= 0:
if not enemy.bounce:
while enemy.rectlist[-1].collidelist(player.rectlist) >= 0:
enemy.rectlist.pop()
enemy.rectlist.pop()
enemy.rectlist.pop()
enemy.X, enemy.Y = enemy.rectlist[-1].x, enemy.rectlist[-1].y
enemy.direction = enemy.direction - 2 if enemy.direction - 2 else enemy.direction + 2
enemy.bounce = 40
pygame.display.update()
wn.fill(WHITE)
main()
Output:

Pygame - getting a sprite to cycle through images in a class, and assign a hitbox

TLDR; Code is below
I'm so done with pygame I'm just so confused,
like I guess I am learning more about classes, and loops and everything but, I think we bit off more than we can chew in this school project for just a few days of working on together (in-person). Going to school for Full Stacks Dev, and this is the first project they have us doing and I learn a lot from you on stackoverflow, but you give complex answers and we get them working but I guess i'm learning a lot about debugging.
Anyways, reason for so many pygame related questions for sure don't wanna be a pygame expert...
Zombie Code:
import pygame
import random
import math
# import fighting_game
# Player = player()
class ZombieEnemy(pygame.sprite.Sprite):
# zwalkRight = [pygame.image.load('images/zombiestandright1.png'), pygame.image.load('images/ztep.png'), pygame.image.load('imageszombiestandright1.png'), pygame.image.load('images/zombiestandright1.png')]
# zwalkLeft = [(pygame.image.load('images/zombiestandleft.png'), pygame.image.load('images/ztepleft.png'), pygame.image.load('images/zombiestandleft.png'), pygame.image.load('images/ztep2.png')]
def __init__(self, x=300, y=360):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('images/zombie.png')
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.speedy = random.randrange(1, 8)
def move_towards_player(self, player):
# Find direction vector (dx, dy) between enemy and player.
dx, dy = player.rect.x - self.rect.x, player.rect.y - self.rect.y
dist = math.hypot (dx, dy)
dx, dy = dx / dist, dy / dist # Normalize
# Move along this normalized vector towards the player
self.rect.x += dx * 10
self.rect.y += dy * 0
Main Code:
import pygame
import math
import random
from Zombie import *
# from pygame.locals import *
pygame.init()
win = pygame.display.set_mode((900,567))
pygame.display.set_caption("Power Rangers ZOMBIES")
walkRight = [pygame.image.load('images/walk1.png'), pygame.image.load('images/walk2.png'), pygame.image.load('images/walk3.png'), pygame.image.load('images/walk4.png'), pygame.image.load('images/walk5.png'), pygame.image.load('images/walk6.png')]
walkLeft = [pygame.image.load('images/leftwalk2.png'), pygame.image.load('images/leftwalk3.png'), pygame.image.load('images/leftwalk4.png'), pygame.image.load('images/leftwalk5.png'), pygame.image.load('images/leftwalk6.png'), pygame.image.load('images/leftwalk7.png')]
bg = pygame.image.load('images/background.png')
char = pygame.image.load('images/standingstill.png')
clock = pygame.time.Clock()
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
self.image = pygame.Surface((144,200))
self.rect = self.image.get_rect()
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 0
self.isJump = False
self.left = False
self.right = False
self.walkCount = 0
self.jumpCount = 10
self.rect.x = x
self.rect.y = y
def draw(self, win):
if self.walkCount + 1 >= 18:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.x,self.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.x,self.y))
self.walkCount +=1
else:
win.blit(char, (self.x,self.y))
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self) #call Sprite initializer
self.image = pygame.image.load('images/background.png')
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
BackGround = Background('images/background.png', [0,0])
# def redrawGameWindow():
# win.blit(bg, (0,0))
# man.draw(win)
# pygame.display.update()
all_zombies = pygame.sprite.Group()
#mainloop
man = Player(100, 340, 40, 60)
run = True
for i in range( 50 ):
new_x = random.randrange( 0, 10000) # random x-position
# new_y = random.randrange( 0, ) # random y-position
z = ZombieEnemy(new_x)
all_zombies.add(z) # create, and add to group
z.move_towards_player(man)
#####
while run:
clock.tick(27)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and man.x > man.vel:
BackGround.rect.left = BackGround.rect.left + int(10)
man.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT]: #and man.x < 500 - man.width - man.vel:
BackGround.rect.left = BackGround.rect.left - int(10)
man.x += man.vel
man.right = True
man.left = False
else:
man.right = False
man.left = False
man.walkCount = 0
if not(man.isJump):
if keys[pygame.K_SPACE]:
man.isJump = True
man.right = False
man.left = False
man.walkCount = 0
else:
if man.jumpCount >= -10:
neg = 1
if man.jumpCount < 0:
neg = -1
man.y -= (man.jumpCount ** 2) * 0.5 * neg
man.jumpCount -= 1
else:
man.isJump = False
man.jumpCount = 10
# redrawGameWindow()
for zombie in all_zombies:
zombie.move_towards_player(man)
win.blit(BackGround.image, BackGround.rect)
all_zombies.update()
man.draw(win)
all_zombies.draw(win)
pygame.display.flip()
pygame.quit()
The player (man) is a Sprite and the all the zombies are in the Group all_zombies.
Use pygame.sprite.spritecollide() to finde collisions between a zombie and the player. e.g.:
if pygame.sprite.spritecollide(man, all_zombies, False):
print("hit")
To make that work you have to track the position oft the player in the .rect attribute., because that is used by pygame.sprite.spritecollide to finde the collisions.
You don't need the attributes .x and .y at all. e.g.:
class Player(pygame.sprite.Sprite):
def __init__(self,x,y,width,height):
# [...]
self.rect.x = x
self.rect.y = y
def draw(self, win):
if self.walkCount + 1 >= 18:
self.walkCount = 0
if self.left:
win.blit(walkLeft[self.walkCount//3], (self.rect.x,self.rect.y))
self.walkCount += 1
elif self.right:
win.blit(walkRight[self.walkCount//3], (self.rect.x,self.rect.y))
self.walkCount +=1
else:
win.blit(char, (self.rect.x,self.rect.y))
while run:
# [...]
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and man.rect.x > man.vel:
BackGround.rect.left = BackGround.rect.left + int(10)
man.rect.x -= man.vel
man.left = True
man.right = False
elif keys[pygame.K_RIGHT]: #and man.x < 500 - man.width - man.vel:
BackGround.rect.left = BackGround.rect.left - int(10)
man.rect.x += man.vel
man.right = True
man.left = False
else:
man.right = False
man.left = False
man.walkCount = 0

Player jumps only once using player.jump in pygame

I am trying to make my player jump (from this tutorial https://opensource.com/article/19/12/jumping-python-platformer-game) but it only jumps once when you press key_up or key "w". And when I look at the output produced in the terminal while I am running the game.py file I see that it was printed jump several times in the terminal.
game.py:
import pygame
import sys
import os
'''
Objects
'''
class Platform(pygame.sprite.Sprite):
# x location, y location, img width, img height, img file
def __init__(self,xloc,yloc,imgw,imgh,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img)).convert()
self.image.convert_alpha()
self.rect = self.image.get_rect()
self.rect.y = yloc
self.rect.x = xloc
class Player(pygame.sprite.Sprite):
'''
Spawn a player
'''
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.movex = 0
self.movey = 0
self.frame = 0
self.health = 10
# gravity variables here
self.collide_delta = 0
self.jump_delta = 6
self.score = 1
self.images = []
for i in range(1,9):
img = pygame.image.load(os.path.join('images','spr' + str(i) + '.png')).convert()
img.convert_alpha()
img.set_colorkey(ALPHA)
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
def jump(self,platform_list):
self.jump_delta = 0
def gravity(self):
self.movey += 3.2 # how fast player falls
if self.movey >= 15:
self.movey = 6
if self.rect.bottom > worldy and self.movey >= 0: # <-- uses bottom
self.movey = 0
self.rect.bottom = worldy # <-- uses bottom
def control(self,x,y):
'''
control player movement
'''
self.movex += x
self.movey += y
def update(self):
'''
Update sprite position
'''
plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
for p in plat_hit_list:
self.collide_delta = 0 # stop jumping
self.movey = 0
if self.rect.y > p.rect.y:
self.rect.y = p.rect.y+ty
else:
self.rect.y = p.rect.y-ty
if self.collide_delta < 6 and self.jump_delta < 6:
self.jump_delta = 6*2
self.movey -= 33 # how high to jump
self.collide_delta += 6
self.jump_delta += 6
self.rect.x = self.rect.x + self.movex
self.rect.y = self.rect.y + self.movey
# moving left
if self.movex < 0:
self.frame += 1
if self.frame > ani*3:
self.frame = 0
self.image = self.images[self.frame//ani]
# moving right
if self.movex > 0:
self.frame += 1
if self.frame > ani*3:
self.frame = 0
self.image = self.images[(self.frame//ani)+4]
# collisions
enemy_hit_list = pygame.sprite.spritecollide(self, enemy_list, False)
for enemy in enemy_hit_list:
self.health -= 1
print(self.health)
plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
for p in plat_hit_list:
self.rect.bottom = p.rect.top
ground_hit_list = pygame.sprite.spritecollide(self, ground_list, False)
for g in ground_hit_list:
self.movey = 0
self.rect.y = worldy-ty-ty
self.collide_delta = 0 # stop jumping
if self.rect.y > g.rect.y:
self.health -= 1
print(self.health)
class Enemy(pygame.sprite.Sprite):
'''
Spawn an enemy
'''
def __init__(self,x,y,img):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(os.path.join('images',img))
#self.image.convert_alpha()
#self.image.set_colorkey(ALPHA)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.counter = 0
def move(self):
'''
enemy movement
'''
distance = 80
speed = 8
if self.counter >= 0 and self.counter <= distance:
self.rect.x += speed
elif self.counter >= distance and self.counter <= distance*2:
self.rect.x -= speed
else:
self.counter = 0
self.counter += 1
class Level():
def bad(lvl,eloc):
if lvl == 1:
enemy = Enemy(eloc[0],eloc[1],'yeti.png') # spawn enemy
enemy_list = pygame.sprite.Group() # create enemy group
enemy_list.add(enemy) # add enemy to group
if lvl == 2:
print("Level " + str(lvl) )
return enemy_list
def loot(lvl,lloc):
print(lvl)
def ground(lvl,gloc,tx,ty):
ground_list = pygame.sprite.Group()
i=0
if lvl == 1:
while i < len(gloc):
ground = Platform(gloc[i],worldy-ty,tx,ty,'ground.png')
ground_list.add(ground)
i=i+1
if lvl == 2:
print("Level " + str(lvl) )
return ground_list
def platform(lvl,tx,ty):
plat_list = pygame.sprite.Group()
ploc = []
i=0
if lvl == 1:
ploc.append((0,worldy-ty-128,3))
ploc.append((300,worldy-ty-256,3))
ploc.append((500,worldy-ty-128,4))
while i < len(ploc):
j=0
while j <= ploc[i][2]:
plat = Platform((ploc[i][0]+(j*tx)),ploc[i][1],tx,ty,'ground.png')
plat_list.add(plat)
j=j+1
print('run' + str(i) + str(ploc[i]))
i=i+1
if lvl == 2:
print("Level " + str(lvl) )
return plat_list
'''
Setup
'''
worldx = 960
worldy = 720
fps = 40 # frame rate
ani = 4 # animation cycles
clock = pygame.time.Clock()
pygame.init()
main = True
BLUE = (25,25,200)
BLACK = (23,23,23 )
WHITE = (254,254,254)
ALPHA = (0,255,0)
world = pygame.display.set_mode([worldx,worldy])
backdrop = pygame.image.load(os.path.join('images','stage.png')).convert()
backdropbox = world.get_rect()
player = Player() # spawn player
player.rect.x = 0
player.rect.y = 0
player_list = pygame.sprite.Group()
player_list.add(player)
steps = 10 # how fast to move
eloc = []
eloc = [200,20]
gloc = []
#gloc = [0,630,64,630,128,630,192,630,256,630,320,630,384,630]
tx = 64 #tile size
ty = 64 #tile size
i=0
while i <= (worldx/tx)+tx:
gloc.append(i*tx)
i=i+1
enemy_list = Level.bad( 1, eloc )
ground_list = Level.ground( 1,gloc,tx,ty )
plat_list = Level.platform( 1,tx,ty )
'''
Main loop
'''
while main == True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit(); sys.exit()
main = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT or event.key == ord('a'):
print("LEFT")
player.control(-steps,0)
if event.key == pygame.K_RIGHT or event.key == ord('d'):
print("RIGHT")
player.control(steps,0)
if event.key == pygame.K_UP or event.key == ord('w'):
print('jump')
if event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT or event.key == ord('a'):
player.control(steps,0)
if event.key == pygame.K_RIGHT or event.key == ord('d'):
player.control(-steps,0)
if event.key == pygame.K_UP or event.key == ord('w'):
player.jump(plat_list)
if event.key == ord('q'):
pygame.quit()
sys.exit()
main = False
world.blit(backdrop, backdropbox)
player.gravity() # check gravity
player.update()
player_list.draw(world)
enemy_list.draw(world)
ground_list.draw(world)
plat_list.draw(world)
for e in enemy_list:
e.move()
pygame.display.flip()
clock.tick(fps)
The original images are from here https://opengameart.org/sites/default/files/opp2_sprites.zip but I have separated in imgur for easy explanation:
content folder images:
For enemy (sprit) is yeti.png:
For background is stage.png: https://imgur.com/YyiEJ0q
and the image of the player: spr.png:
To do a jump, self.collide_delta and self.jump_delta have to be less than 6. See your code:
if self.collide_delta < 6 and self.jump_delta < 6:
self.jump_delta = 6*2
The issue is that self.collide_delta is not set 0, when the player hits a platform:
(in the 2nd case)
plat_hit_list = pygame.sprite.spritecollide(self, plat_list, False)
for p in plat_hit_list:
self.rect.bottom = p.rect.top
self.collide_delta = 0 # <----- this is missing

player's Rect is not aligned with the player/character

I got the bullets to shoot but the player rect is not aligned with the player itself, so the bullets doesn't come from the player but rather from the rect that is offset.
The 3 main Classes:
(bullet, camera and player)
def RelRect(char, camera):
return Rect(char.rect.x - camera.rect.x, char.rect.y - camera.rect.y, char.rect.w, char.rect.h)
class Camera(object):
'''Class for center screen on the player'''
def __init__(self, screen, player, levelWidth, levelHeight):
self.player = player
self.rect = screen.get_rect()
self.rect.center = self.player.center
self.worldRect = Rect(0, 0, levelWidth, levelHeight)
def update(self):
if self.player.centerx > self.rect.centerx:
self.rect.centerx = self.player.centerx
if self.player.centerx < self.rect.centerx:
self.rect.centerx = self.player.centerx
if self.player.centery > self.rect.centery:
self.rect.centery = self.player.centery
if self.player.centery < self.rect.centery:
self.rect.centery = self.player.centery
def draw_sprites(self, surface, sprites):
for sprite in sprites:
if sprite.rect.colliderect(self.rect):
surface.blit(sprite.image, RelRect(sprite, self))
class Bullet():
def __init__(self, x, y, targetX, targetY):
self.image = ''
self.origX = x
self.origY = y
self.x = x
self.y = y
self.targetX = targetX
self.targetY = targetY
self.image = image.load('res/attack/attack.png')
self.vel = 20
# rnge is the range of the bullet, in frames
self.rnge = 50
# prog is the progress of the bullet, in frames
self.prog = 0
# dmg is the damage that the bullet will do upon impact
self.dmg = 1
self.dmg_mult = 1
# deathtick is the timer for enemy death
self.deathTick = 0
# rect is the hitbox of the bullet
self.w, self.h = self.image.get_width(), self.image.get_height()
self.rect = Rect(self.x, self.y, self.w, self.h)
def update(self):
# Increases Progress of the bullet
if not (sqrt((self.targetX - self.origX) ** 2 + (self.targetY - self.origY) ** 2)) == 0:
self.x += int((self.vel) * (self.targetX - self.origX) /
(sqrt((self.targetX - self.origX) ** 2 +
(self.targetY - self.origY) ** 2)))
self.y += int((self.vel) * (self.targetY - self.origY) /
(sqrt((self.targetX - self.origX) ** 2 +
(self.targetY - self.origY) ** 2)))
self.rect.center = [self.x, self.y]
def check(self, enemies):
# Checks if the bullet is out of range, then deletes it, if it is
if self.prog >= self.rnge:
bullets.remove(self)
#checks if bullets are out of bounds
elif not 0 < self.x < WIDTH - self.w or not 0 < self.y < HEIGHT - self.h:
bullets.remove(self)
else:
#checks if bullet hits target hitbox, if so, starts a timer that kills the bullet after 1 frame
for e in enemies:
if self.rect.colliderect(e.hitbox):
self.deathTick += 1
if self.deathTick > 1:
bullets.remove(self)
#draws each bullet
def draw(self):
screen.blit(self.image, self.rect)
#draws bullet hitboxes
def debug(self):
draw.rect(screen, (0,0,0), self.rect, 2)
draw.line(screen, (255,255,255), (self.x, self.y), (self.targetX, self.targetY), 4)
class Player(sprite.Sprite):
'''class for player and collision'''
def __init__(self, x, y):
sprite.Sprite.__init__(self)
self.moveUnitsY = 0
self.moveUnitsX = 0
self.x = x
self.y = y
self.ground = False
self.jump = False
self.image = image.load("res/move/Ridle.png").convert()
self.rect = self.image.get_rect()
self.Lrun = ["res/move/L1.png",
"res/move/L2.png",
"res/move/L3.png",
"res/move/L4.png",
"res/move/L5.png",
"res/move/L6.png"]
self.Rrun = ["res/move/R1.png",
"res/move/R2.png",
"res/move/R3.png",
"res/move/R4.png",
"res/move/R5.png",
"res/move/R6.png"]
self.direction = "right"
self.rect.topleft = [x, y]
self.frame = 0
def update(self, up, down, left, right):
if up:
if self.ground:
if self.direction == "right":
self.image = image.load("res/move/Ridle.png")
self.jump = True
self.moveUnitsY -= 20
if down:
if self.ground and self.direction == "right":
self.image = image.load("res/move/Ridle.png").convert_alpha()
if self.ground and self.direction == "left":
self.image = image.load("res/move/Lidle.png").convert_alpha()
if not down and self.direction == "right":
self.image = image.load("res/move/Ridle.png").convert_alpha()
if not down and self.direction == "left":
self.image = image.load("res/move/Lidle.png").convert_alpha()
if left:
self.direction = "left"
self.moveUnitsX = -vel
if self.ground:
self.frame += 1
self.image = image.load(self.Lrun[self.frame]).convert_alpha()
if self.frame == 4: self.frame = 0
else:
self.image = self.image = image.load("res/move/Lidle.png").convert_alpha()
if right:
self.direction = "right"
self.moveUnitsX = +vel
if self.ground:
self.frame += 1
self.image = image.load(self.Rrun[self.frame]).convert_alpha()
if self.frame == 4: self.frame = 0
else:
self.image = self.image = image.load("res/move/Ridle.png").convert_alpha()
if not (left or right):
self.moveUnitsX = 0
self.rect.right += self.moveUnitsX
self.collide(self.moveUnitsX, 0, world)
if not self.ground:
self.moveUnitsY += 0.3
if self.moveUnitsY > 10:
self.moveUnitsY = 10
self.rect.top += self.moveUnitsY
if self.jump:
self.moveUnitsY += 2
self.rect.top += self.moveUnitsY
if self.ground == True:
self.jump = False
self.ground = False
self.collide(0, self.moveUnitsY, world)
def collide(self, moveUnitsX, moveUnitsY, world):
self.ground = False
for pos in world:
if self.rect.colliderect(pos):
if moveUnitsX > 0:
self.rect.right = pos.rect.left
if moveUnitsX < 0:
self.rect.left = pos.rect.right
if moveUnitsY > 0:
self.rect.bottom = pos.rect.top
self.moveUnitsY = 0
self.ground = True
if moveUnitsY < 0:
self.rect.top = pos.rect.bottom
self.moveUnitsY = 0
and then the running loop:
while running:
for evnt in event.get():
if evnt.type == QUIT or evnt.type == KEYDOWN and evnt.key == K_ESCAPE:
running = False
if evnt.type == KEYDOWN and evnt.key == K_UP:
up = True
if evnt.type == KEYDOWN and evnt.key == K_DOWN:
down = True
if evnt.type == KEYDOWN and evnt.key == K_LEFT:
left = True
if evnt.type == KEYDOWN and evnt.key == K_RIGHT:
right = True
if evnt .type == MOUSEBUTTONDOWN:
# checks if any mouse button is down, if so sets clicking to true
button = evnt.button
#startTicks = time.get_ticks()
if evnt.type == MOUSEBUTTONUP:
# checks if any mouse button is down, if so sets clicking to true
button = 0
if evnt.type == MOUSEMOTION:
# sets mx and my to mouse x backgand y if mouse is moving
mx, my = evnt.pos
if evnt.type == KEYUP and evnt.key == K_UP:
up = False
if evnt.type == KEYUP and evnt.key == K_DOWN:
down = False
if evnt.type == KEYUP and evnt.key == K_LEFT:
left = False
if evnt.type == KEYUP and evnt.key == K_RIGHT:
right = False
if button == 1:
bullets.append(Bullet(player.rect[0]+ player.rect[2]//2, player.rect[1] + player.rect[3]//2, mx, my))
asize = ((screen_rect.w // background_rect.w + 1) * background_rect.w, (screen_rect.h // background_rect.h + 1) * background_rect.h)
bg = Surface(asize)
for x in range(0, asize[0], background_rect.w):
for y in range(0, asize[1], background_rect.h):
screen.blit(background, (x, y))
for b in bullets:
b.update()
b.draw()
b.check(enemies)
time_spent = sec(clock, FPS)
camera.draw_sprites(screen, all_sprite)
draw.rect(screen, (255,0,0), player.rect, 4)
player.update(up, down, left, right)
camera.update()
display.flip()
if you run the program itself, you can see that the red rectangle (4th last line) that represents the player rect is not to where the character is suppose to appear....
How can I make it so that the player rect will be at the position of the character? So that the bullets come from the player.
Thanks :)
Full Code Here:
https://pastebin.com/z1LwxYYt
The problem with your code is that neither your red rectangle nor the bullets are drawn to the screen in relation to the camera.
The Bullet class should subclass Sprite, too, so you can add them to the all_sprite-group, like you do with the obstacles and the player.
Then let the Camera-class handle the drawing of the bullets.
As for the red rectangle, I suggest removing the RelRect function and move it into the Camera class itself, like this:
class Camera(object):
...
def translate(self, rect):
return Rect(rect.x - self.rect.x, rect.y - self.rect.y, rect.w, rect.h)
def draw_sprites(self, surface, sprites):
for sprite in sprites:
if sprite.rect.colliderect(self.rect):
surface.blit(sprite.image, self.translate(sprite.rect, self))
which would allow you to draw the rect like this:
draw.rect(screen, (255,0,0), camera.translate(player.rect), 4)

how to target a sprite randomly

I am trying to create a retro style 2d shooter game and am currently making a homing missile. I need to randomly select a sprite from a group (pygame.sprite.Group) and find out its x and y coords. I have already made it home in on these coords so I do not need help with that. My code is:
an Enemy class (they are all the same just with different sizes and pictures):
class EnemyShipLevel1(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.image.load("C:/Users /Me/PycharmProjects/Game folder/SpaceShip game/Enemy1.png")
self.rect = self.image.get_rect()
self.rect.y = -50
self.rect.centerx = random.randint(15, SCREEN_WIDTH-15)
self.change_y = 0
self.cooldown = 0
self.targetx = 500
self.health = 1
if self.targetx >= self.rect.centerx:
self.change_x = 2
else:
self.change_x = -2
def update(self):
self.rect.x += self.change_x
self.rect.y += self.change_y
if self.rect.centery < 200:
self.rect.centery += 2
self.get_target()
if self.targetx > self.rect.centerx+10:
self.change_x = 3
elif self.targetx < self.rect.centerx-10:
self.change_x = -3
else:
self.change_x = 0
self.cooldown -= 1
if self.rect.left < screen_rect.left:
self.change_x = -self.change_x
if self.rect.right > screen_rect.right:
self.change_x = -self.change_x
if self.cooldown < 0 and (self.rect.centerx-50 < self.targetx < self.rect.centerx+50) and player.trans is False:
self.cooldown = random.randint(15, 25)
laser = NormalLaser(self.rect.centery, self.rect.centerx, False, 0, 8)
all_sprite_list.add(laser)
enemy_laser_list.add(laser)
player_laser_hit_list = pygame.sprite.spritecollide(self, player_laser_list, True)
for laser in player_laser_hit_list:
if self.health < 1:
all_sprite_list.remove(self)
enemy_list.remove(self)
else:
self.health -= 1
def get_target(self):
self.targetx = player.rect.centerx
Targeting Laser class (I know you can't bend lasers):
class TargetingLaser(pygame.sprite.Sprite):
def __init__(self, start_x, start_y, dest_x, dest_y):
super().__init__()
self.floating_point_x = start_x
self.floating_point_y = start_y
self.x_diff = dest_x - start_x
self.y_diff = dest_y - start_y
self.angle = math.atan2(self.y_diff, self.x_diff)
self.velocity = 10
self.change_x = math.cos(self.angle) * self.velocity
self.change_y = math.sin(self.angle) * self.velocity
self.image = pygame.image.load("C:/Users/Minecraft/PycharmProjects/Game folder/SpaceShip game/Laser.png")
self.image = pygame.transform.rotate(self.image, -(math.degrees(self.angle)+90))
self.rect = self.image.get_rect()
self.rect.centerx = start_x
self.rect.centery = start_y
def update(self):
self.floating_point_y += self.change_y
self.floating_point_x += self.change_x
self.rect.y = int(self.floating_point_y)
self.rect.x = int(self.floating_point_x)
if self.rect.top < screen_rect.top:
player_laser_list.remove(self)
enemy_laser_list.remove(self)
all_sprite_list.remove(self)
if self.rect.bottom > screen_rect.bottom:
player_laser_list.remove(self)
all_sprite_list.remove(self)
enemy_laser_list.remove(self)
if self.rect.left < screen_rect.left:
player_laser_list.remove(self)
all_sprite_list.remove(self)
enemy_laser_list.remove(self)
if self.rect.right > screen_rect.right:
player_laser_list.remove(self)
all_sprite_list.remove(self)
enemy_laser_list.remove(self)
Game Loop:
all_sprite_list = pygame.sprite.Group()
enemy_list = pygame.sprite.Group()
player_list = pygame.sprite.GroupSingle()
player_laser_list = pygame.sprite.Group()
enemy_laser_list = pygame.sprite.Group()
live_list = pygame.sprite.Group()
health_bar_list = pygame.sprite.GroupSingle()
screen = pygame.display.set_mode([SCREEN_WIDTH, SCREEN_HEIGHT])
screen_rect = screen.get_rect()
pygame.display.set_caption('Game')
clock = pygame.time.Clock()
Play = True
player = Player(SCREEN_WIDTH/2, SCREEN_HEIGHT-30)
all_sprite_list.add(player)
player_list.add(player)
for counter in range(2):
enemy = EnemyShipLevel1()
all_sprite_list.add(enemy)
enemy_list.add(enemy)
live1 = LivesIcon(5, 0, 1)
live_list.add(live1)
live1 = LivesIcon(27, 30, 2)
live_list.add(live1)
live1 = LivesIcon(50, 0, 3)
live_list.add(live1)
live1 = LivesIcon(72, 30, 4)
live_list.add(live1)
live1 = LivesIcon(95, 0, 5)
live_list.add(live1)
all_sprite_list.add(live_list)
health_bar = PlayerHealthBar(0, 690)
all_sprite_list.add(health_bar)
health_bar_list.add(health_bar)
ship = EnemyShipLevel3()
all_sprite_list.add(ship)
enemy_list.add(ship)
while Play is True:
# Shooting
if player.shooting is True:
if player.laser_cooldown < 0:
player.laser_cooldown = 5
if player.last_shot == "left":
player.last_shot = "right"
laser = NormalLaser(player.rect.centery-2, player.rect.centerx + 11, True, 0, 8)
all_sprite_list.add(laser)
player_laser_list.add(laser)
else:
player.last_shot = "left"
laser = NormalLaser(player.rect.centery-2, player.rect.centerx - 11, True, 0, 8)
all_sprite_list.add(laser)
player_laser_list.add(laser)
if player.rocket_cooldown < 0:
player.rocket_cooldown = 15
for counter in range(10):
laser = TargetingLaser(player.rect.centerx,player.rect.centery, player.rect.centerx, 0)
all_sprite_list.add(laser)
player_laser_list.add(laser)
# Shooting
# Levels
if len(enemy_list) == 0:
all_sprite_list.remove(enemy_laser_list)
all_sprite_list.remove(player_laser_list)
enemy_laser_list.empty()
player_laser_list.empty()
player.level += 1
for counter in range(player.spawn_enemies1):
enemy = EnemyShipLevel1()
all_sprite_list.add(enemy)
enemy_list.add(enemy)
for counter in range(player.spawn_enemies2):
enemy2 = EnemyShipLevel2()
all_sprite_list.add(enemy2)
enemy_list.add(enemy2)
for counter in range(player.spawn_enemies3):
ship = EnemyShipLevel3()
all_sprite_list.add(ship)
enemy_list.add(ship)
if player.level == 2:
player.spawn_enemies1 = 3
player.spawn_enemies2 = 1
if player.level == 3:
player.spawn_enemies1 = 5
player.spawn_enemies2 = 2
if player.level == 4:
player.spawn_enemies1 = 10
player.spawn_enemies2 = 5
if player.level == 5:
player.spawn_enemies1 = 12
player.spawn_enemies2 = 8
player.spawn_enemies3 = 1
# Levels
# End Game
if player.lives == 0:
Play = False
# End Game
# Keys
for event in pygame.event.get():
if event.type == pygame.QUIT:
Play = False
elif event.type == pygame.KEYDOWN:
if (event.key == pygame.K_a) or (event.key == pygame.K_LEFT):
player.change_x = -5
elif (event.key == pygame.K_d) or (event.key == pygame.K_RIGHT):
player.change_x = 5
if event.key == pygame.K_SPACE:
player.shooting = True
elif event.type == pygame.KEYUP:
player.rotation = 0
player.change_x = 0
player.change_y = 0
if event.key == pygame.K_SPACE:
player.shooting = False
# Keys
all_sprite_list.update()
screen.fill(BLACK)
all_sprite_list.draw(screen)
enemy_laser_list.draw(screen)
player_laser_list.draw(screen)
pygame.display.update()
clock.tick(60)
quit()
Each time you kill all of the spawned enemies then the game spawns in more and adds them to enemy_list().
If you are asking how to randomly choose a sprite then use a random.randint(0, 30) (or the number of sprites) then do a long series of if statements. Example Code.
import random
import pygame
class Enemy:
def __init__()
random_sprite = random.randint(0, 1)
# Then put in two variables which equal to the png (I will use sprite1 and sprite2)
def sprite_render
if random_sprite == 0:
# Just blit sprite1
elif random_sprite == 1:
# Just blit sprite2
(The reason why I just commented the actions for the image stuff is because almost everyone does it in a different way and that is not the main point of this answer.)
You could assign a number attribute to each enemy.
For example, you could start off your enemy function like this:
class EnemyShipLevel1(pygame.sprite.Sprite):
number = None
def __init__(self,number):
super().__init__()
self.number = number
self.image = pygame.image.load("C:/Users /Me/PycharmProjects/Game folder/SpaceShip game/Enemy1.png")
Now when you spawn in the enemies, you would add the number attribute:
for counter in range(2): # change this number to use more than 2 enemies (I use 30 later)
enemy = EnemyShipLevel1(counter)
all_sprite_list.add(enemy)
enemy_list.add(enemy)
All you have to do is randomly choose the sprite:
choice = random.randrange(0,30) # between 0 and the number of enemies spawned
for i in enemy_list:
if i.number == choice:
# make i the target of the laser
target_x = i.rect.x
target_y = i.rect.y

Categories

Resources