Trouble Shooting Projectiles Upward in Pygame - python

I'm in the process of writing a duel-player tank game, similar to one known as "Tank Trouble". Of course, I'm only in the baby steps of it right now and I'm a bit stuck on a particular little bug. At the moment, the code below enables me to display my sprite, able to move around in all the directions, and also able to shoot projectiles. The only issue, however, is that it can only shoot projectiles left or right. Does anyone have any suggestions as to how to change this so that the tank can shoot upward, when facing upward and downward, when facing downward? It can only shoot left and right, even when it is facing up or down, which is kind of odd. I am having trouble accessing the y-axis and having the projectile travel along it.
If anyone can find out how to do that and tell me what the new code is and where to place it, that would help a ton. Here is my code:
import pygame
pygame.init()
screen = pygame.display.set_mode((500,500))
pygame.display.set_caption("Game")
tankImg = pygame.image.load("tank1.png")
downTank = tankImg
leftTank = pygame.image.load("left1.png")
rightTank = pygame.image.load("right1.png")
upTank = pygame.image.load("up1.png")
bg = pygame.image.load("background.png")
screenWidth = 500
screenHeight = 500
clock = pygame.time.Clock()
class player(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.left = False
self.right = False
self.up = False
self.down = False
def draw(self,screen):
screen.blit(tankImg,(self.x,self.y))
class projectile(object):
def __init__(self,x, y, radius, color, facing):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.facing = facing
self.vel = 8 * facing
def draw(self,screen):
pygame.draw.circle(screen, self.color, (self.x,self.y), self.radius)
def redraw():
screen.blit(bg, (0,0))
tank.draw(screen)
for bullet in bullets:
bullet.draw(screen)
pygame.display.update()
run = True
tank = player(300, 410, 16, 16)
bullets = []
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
for bullet in bullets:
if bullet.x < screenWidth and bullet.x > 0:
bullet.x += bullet.vel
elif bullet.y > screenHeight and bullet.y > 0:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and tank.x > tank.vel:
tank.left = True
tankImg = leftTank
tank.x -= tank.vel
facing = -1
if keys[pygame.K_RIGHT] and tank.x < 500 - tank.width:
tank.right = True
tankImg = rightTank
tank.x += tank.vel
facing = 1
if keys[pygame.K_UP] and tank.y > tank.vel:
tank.up = True
tankImg = upTank
tank.y -= tank.vel
facing = 1
if keys[pygame.K_DOWN] and tank.y < 500 - tank.height:
down = True
tankImg = downTank
tank.y += tank.vel
facing = -1
if keys[pygame.K_SPACE]:
if len(bullets) < 1:
bullets.append(projectile(round(tank.x + tank.width // 2), round(tank.y + tank.height // 2), 4, (0,0,0),facing))
redraw()
pygame.quit()

The issue is caused in that part of the code:
if bullet.x < screenWidth and bullet.x > 0:
bullet.x += bullet.vel
elif bullet.y > screenHeight and bullet.y > 0:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))
As long the bullet is in bounds of the window, the 1st condition evaluates True. That causes that the bullet can move in x-direction only.
Instead of the facing attribute add a direction attribute to the class projectile. Furthermore add a method (move) which moves the projectile. This method ignores the bounds of the window:
class projectile(object):
def __init__(self,x, y, radius, color, direction):
self.x = x
self.y = y
self.radius = radius
self.color = color
self.direction = direction
self.vel = 8
def move(self):
self.x += self.direction[0] * self.vel
self.y += self.direction[1] * self.vel
def draw(self,screen):
pygame.draw.circle(screen, self.color, (self.x,self.y), self.radius)
Move the bullets in a loop end evaluate if a bullet is in bounds of the window. This can be done with ease, by using pygame.Rect and collidepoint():
while run:
# [...]
for bullet in bullets[:]:
bullet.move()
window_rect = pygame.Rect(0, 0, screenWidth, screenHeight)
if not window_rect.collidepoint((bullet.x, bullet.y)):
bullets.pop(bullets.index(bullet))
Set the direction of the bullet dependent on the moving direction of the tank:
direction = (-1, 0)
while run:
# [...]
if keys[pygame.K_LEFT] and tank.x > tank.vel:
tank.left = True
tankImg = leftTank
tank.x -= tank.vel
direction = (-1, 0)
if keys[pygame.K_RIGHT] and tank.x < 500 - tank.width:
tank.right = True
tankImg = rightTank
tank.x += tank.vel
direction = (1, 0)
if keys[pygame.K_UP] and tank.y > tank.vel:
tank.up = True
tankImg = upTank
tank.y -= tank.vel
direction = (0, -1)
if keys[pygame.K_DOWN] and tank.y < 500 - tank.height:
down = True
tankImg = downTank
tank.y += tank.vel
direction = (0, 1)
if keys[pygame.K_SPACE]:
if len(bullets) < 1:
px, py = round(tank.x + tank.width // 2), round(tank.y + tank.height // 2)
bullets.append(projectile(px, py, 4, (0,0,0), direction))
I recommend to spawn the bullets in the pygame.KEYDOWN event instead of the evaluating the pressed keys (keys[pygame.K_SPACE]). The event occurs once when a button is pressed. So every time when SPACE is pressed a single bullet is generated (Of course that is up to you):
direction = (-1, 0)
while run:
clock.tick(30)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
if event.type == pygame.KEYDOWN:
if event. key == pygame.K_SPACE:
px, py = round(tank.x + tank.width // 2), round(tank.y + tank.height // 2)
bullets.append(projectile(px, py, 4, (0,0,0), direction))
for bullet in bullets[:]:
bullet.move()
window_rect = pygame.Rect(0, 0, screenWidth, screenHeight)
if not window_rect.collidepoint((bullet.x, bullet.y)):
bullets.pop(bullets.index(bullet))
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT] and tank.x > tank.vel:
tank.left = True
tankImg = leftTank
tank.x -= tank.vel
direction = (-1, 0)
if keys[pygame.K_RIGHT] and tank.x < 500 - tank.width:
tank.right = True
tankImg = rightTank
tank.x += tank.vel
direction = (1, 0)
if keys[pygame.K_UP] and tank.y > tank.vel:
tank.up = True
tankImg = upTank
tank.y -= tank.vel
direction = (0, -1)
if keys[pygame.K_DOWN] and tank.y < 500 - tank.height:
down = True
tankImg = downTank
tank.y += tank.vel
direction = (0, 1)
redraw()

Currently, if bullet.x is between 0 and the screen width the first condition is met and the second is not checked
for bullet in bullets:
if bullet.x < screenWidth and bullet.x > 0:
# When this is True then the "elif" is not run
bullet.x += bullet.vel
elif bullet.y > screenHeight and bullet.y > 0:
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))
You should just have one condition that makes sure the bullet is in both the x and y bound so that you update both x and y
for bullet in bullets:
if 0 < bullet.x < screenWidth and 0 < bullet.y < screenHeight:
bullet.x += bullet.vel
bullet.y += bullet.vel
else:
bullets.pop(bullets.index(bullet))
You can combine conditions like this that check if a value is between 2 values like so min < value < max

Related

How Can I Shoot Projectiles with my mouse at any position? [duplicate]

This question already has answers here:
Shooting a bullet in pygame in the direction of mouse
(2 answers)
calculating direction of the player to shoot pygame
(1 answer)
How can you rotate the sprite and shoot the bullets towards the mouse position?
(1 answer)
Closed 2 years ago.
I made my projectile shoot left and right by buttons but that would be boring clicking alot of buttons
how do make it shoot with with my mouse? at any position x,y
# projectile class
if keys[pygame.K_f]:
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x += bullet.speed
else:
bullets.pop(bullets.index(bullet))
if len(bullets) < 2:
bullets.append(projectile(round(playerman.x+playerman.width//2),round(playerman.y + playerman.height-54),(0,0,0)))
if keys[pygame.K_g]:
for bullet in bullets:
if bullet.x < 500 and bullet.x > 0:
bullet.x -= bullet.speed
else:
bullets.pop(bullets.index(bullet))
if len(bullets) < 2:
bullets.append(projectile(round(playerman.x+playerman.width//2),round(playerman.y + playerman.height-54),(0,0,0)))
# Jump and Collisions
and this is my projectile class
class projectile(object):
def __init__(self, x, y,color):
self.x = x
self.y = y
self.slash = pygame.image.load("heart.png")
self.rect = self.slash.get_rect()
self.rect.topleft = ( self.x, self.y )
self.speed = 10
self.color = color
def draw(self, window):
self.rect.topleft = ( self.x,self.y )
window.blit(slash, self.rect)
You have to add the moving direction (dirx, diry) to the class projectile. Further add a method which moves the bullet:
class projectile(object):
def __init__(self, x, y, dirx, diry, color):
self.x = x
self.y = y
self.dirx = dirx
self.diry = diry
self.slash = pygame.image.load("heart.png")
self.rect = self.slash.get_rect()
self.rect.topleft = ( self.x, self.y )
self.speed = 10
self.color = color
def move(self):
self.x += self.dirx * self.speed
self.y += self.diry * self.speed
def draw(self, window):
self.rect.topleft = (round(self.x), round(self.y))
window.blit(slash, self.rect)
Compute the direction form the player to the mouse when the mouse button is pressed and spawn a new bullet.
The direction is given by the vector form the player to the muse position (mouse_x - start_x, mouse_y - start_y).
The vector has to be normalized (Unit vector) by dividing the vector components by the Euclidean distance:
for event in pygame.event.get():
# [...]
if event.type == pygame.MOUSEBUTTONDOWN:
if len(bullets) < 2:
start_x, start_y = playerman.x+playerman.width//2, playerman.y + playerman.height-54
mouse_x, mouse_y = event.pos
dir_x, dir_y = mouse_x - start_x, mouse_y - start_y
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
new_bullet = projectile(start_x, start_y, dir_x/distance, dir_y/distance, (0,0,0))
bullets.append(new_bullet)
Move the bullets in a loop in te main application loop and remove the bullet if it is out of the window
run = True
while run:
# [...]
for event in pygame.event.get():
# [...]
for bullet in bullets[:]:
bullet.move()
if bullet.x < 0 or bullet.x > 500 or bullet.y < 0 or bullet.y > 500:
bullets.pop(bullets.index(bullet))
# [...]
Example code
runninggame = True
while runninggame:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
runninggame = False
if event.type == pygame.MOUSEBUTTONDOWN:
if len(bullets) < 2:
start_x, start_y = playerman.x+playerman.width//2, playerman.y + playerman.height-54
mouse_x, mouse_y = event.pos
dir_x, dir_y = mouse_x - start_x, mouse_y - start_y
distance = math.sqrt(dir_x**2 + dir_y**2)
if distance > 0:
new_bullet = projectile(start_x, start_y, dir_x/distance, dir_y/distance, (0,0,0))
bullets.append(new_bullet)
for bullet in bullets[:]:
bullet.move()
if bullet.x < 0 or bullet.x > 800 or bullet.y < 0 or bullet.y > 800:
bullets.pop(bullets.index(bullet))
# [...]

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

Pygame - Getting enemy sprites to move towards player

As of currently the enemy sprites just spawn on the axis, and "move" with the player which is actually a scrolling background, and I'd like for the enemies to move only along the X axis so it doesn't destroy the immersion.
I'd also like for the sprites to spawn "Off the map" and whenever the map scrolls towards them they move towards the players set X axis? I think that would keep things simple, but isn't really the question at hand right now.
The current code I was trying to get working for the movement was :
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 * self.speed
self.rect.y += dy * self.speed
But it wouldn't work, this is with importing the math module.
(I know I don't need the y movements just wanted to get it working first)
Here is the rest of the code --
Zombie.py:
import pygame
from pygame.locals import *
import random
import math
class ZombieEnemy(pygame.sprite.Sprite):
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)
all_zombies = pygame.sprite.Group()
for i in range( 50 ):
new_x = random.randrange( 0, 10000) # random x-position
# new_y = random.randrange( 0, ) # random y-position
all_zombies.add(ZombieEnemy(new_x)) # create, and add to group
I wasn't sure if I should create a whole new class for enemy movement, or what my partner is currently working on the Player, and getting the sprite animations and movement working right.
But here is the main code I have as of now:
import pygame
from Zombie import *
import math
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.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
​
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()
​
#mainloop
man = Player(100, 340, 40, 60)
run = True
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(Player)
all_zombie.update()
win.blit(BackGround.image, BackGround.rect)
man.draw(win)
pygame.display.flip()
all_zombies.draw(screen)
​
pygame.quit()
I can't run code but I see two problems
1 - move_towards_player has to be inside class ZombieEnemy
# --- classes ---
class ZombieEnemy(pygame.sprite.Sprite):
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)
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 * self.speed
self.rect.y += dy * self.speed
# --- other ---
all_zombies = pygame.sprite.Group()
2 - you have to use it in main loop
for zombie in all_zombies:
zombie.move_towards_player(player)
in main loop
while running:
# --- events ---
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
# If keystroke is pressed check right, left.
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
#playerX_change = -2.0
BackGround.rect.left = BackGround.rect.left + 2.5
if event.key == pygame.K_RIGHT:
#playerX_change = 2.0
BackGround.rect.left = BackGround.rect.left - 2.5
# if event.type == pygame.KEYUP:
# if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
# BackGround.rect.left = 0
# --- updates/moves ---
playerX += playerX_change
for zombie in all_zombies:
zombie.move_towards_player(player)
all_zombies.update()
# --- draws ---
screen.blit(BackGround.image, BackGround.rect)
player(playerX,playerY)
all_zombies.draw(screen)
pygame.display.flip()
But here I see other problem - your function expects player as class instance with self.rect inside (similar to ZombieEnemy) but you keep player as separated variables playerImg, playerX, playerY, playerX_change
So you have to create class Player or you have to use playerx, playery in move_towards_player instead of player.rect.x, player.rect.y

How do I create a hitbox for all shapes in my program and a way to detect collision? [duplicate]

This question already has an answer here:
How to detect collisions between two rectangular objects or images in pygame
(1 answer)
Closed 2 years ago.
I am fairly new to python and the module pygame. I also recently started work on a small project where the purpose of the game is to if a circle touches the "player" (the square) then the game will end or present a message saying you loose (still trying to solve the hitbox issue so still thinking of what to do after). The problem I am having is I am having trouble creating a way to detect the collision of the hitboxes and every time I try to make a way to test if the hitboxes I have collided it just won't work, so if someone can tell me how to make the classes and hitbox, another method, or even how to fix my code so it won't crash with a player class. Thank you, for any help, I may receive.
Here is the code (apologies if it is horrible on the eyes I kept on deleting and adding stuff while trying to find a solution and also sorry if I did something improper this is one of my first questions).
import pygame
import random
pygame.init()
width, height = 800, 800
hbox, vbox = 15, 15
rect = pygame.Rect(500, 600, hbox, vbox)
velocity = (0, 0)
frames = 40
ball_size = 12
white = (255, 255, 255)
black = (0, 0, 0)
hp = 100
screen = pygame.display.set_mode((width, height))
pygame.display.set_caption("Space Invaders")
icon = pygame.image.load('ufo.png')
pygame.display.set_icon(icon)
is_blue = True
clock = pygame.time.Clock()
class Ball:
def __init__(self):
self.x = 0
self.y = 0
self.change_x = 0
self.change_y = 0
self.hitbox = (self.x + 1, self.y + 2, 31, 57)
def make_ball():
ball = Ball()
Ball.hitbox = ball.hitbox
ball.x = random.randrange(ball_size, width - ball_size)
ball.y = random.randrange(ball_size, height - ball_size)
ball.change_x = random.randrange(-2, 3)
ball.change_y = random.randrange(-2, 3)
return ball
ball_list = []
ball = make_ball()
ball_list.append(ball)
class player(object):
def __init__(self, ):
self.x = rect.x
self.y = rect.y
self.box_width = hbox
self.box_hieght = vbox
self.speed = move
player.hitbox = (self.x + 1, self.y + 11, 29, 52)
def draw(self, is_blue):
if is_blue:
color = (0, 128, 255)
else:
color = (255, 100, 0)
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
is_blue = not is_blue
Player = player
Player = pygame.draw.rect(screen, color, rect)
def move(self):
surface = pygame.Surface((100, 100))
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]:
move = 8
else:
move = 4
if keys[pygame.K_w]:
rect.y -= move
if rect.y < 0:
rect.y = 0
if keys[pygame.K_s]:
rect.y += move
if rect.y > height - hbox:
rect.y = height - vbox
if keys[pygame.K_a]:
rect.x -= move
if rect.x < 0:
rect.x = 0
if keys[pygame.K_d]:
rect.x += move
if rect.x > width - hbox:
rect.x = width - hbox
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
elif event.type == pygame.KEYDOWN:
# Space bar! Spawn a new ball.
if event.key == pygame.K_SPACE:
ball = make_ball()
ball_list.append(ball)
if rect.x == ball.x and rect.y == ball.y:
hp -100
if hp == 0:
pygame.quit()
if event.type == pygame.KEYDOWN and event.key == pygame.K_c:
is_blue = not is_blue
surface = pygame.Surface((100, 100))
keys = pygame.key.get_pressed()
if keys[pygame.K_LSHIFT] or keys[pygame.K_RSHIFT]:
move = 8
else:
move = 4
if keys[pygame.K_w]:
rect.y -= move
if rect.y < 0:
rect.y = 0
if keys[pygame.K_s]:
rect.y += move
if rect.y > height - hbox:
rect.y = height - vbox
if keys[pygame.K_a]:
rect.x -= move
if rect.x < 0:
rect.x = 0
if keys[pygame.K_d]:
rect.x += move
if rect.x > width - hbox:
rect.x = width - hbox
screen.fill((0, 0, 0))
if is_blue:
color = (0, 128, 255)
else:
color = (255, 100, 0)
color_2 = (255, 0, 0)
for ball in ball_list:
ball.x += ball.change_x
ball.y += ball.change_y
if ball.y > height - ball_size or ball.y < ball_size:
ball.change_y *= -1
if ball.x > width - ball_size or ball.x < ball_size:
ball.change_x *= -1
screen.fill(black)
for ball in ball_list:
pygame.draw.circle(screen, white, [ball.x, ball.y], ball_size)
Rectangle = pygame.draw.rect(screen, color, rect)
pygame.display.flip()
clock.tick(30)
`
In general I recommend to use pygame.sprite.Sprite and pygame.sprite.Group, but I'll show you a solution, which is closer to your current code.
Create a Ball class, which can move and draw the ball object. The class has a .rect attribute of type pygame.Rect instead of the attributes .x and .y and can be used for the "hitbox", too.
The rectangle is updated in the instance method move():
class Ball:
def __init__(self):
x = random.randrange(ball_size, width - ball_size)
y = random.randrange(ball_size, height - ball_size)
self.change_x, self.change_y = 0, 0
while self.change_x == 0 and self.change_y == 0:
self.change_x = random.randrange(-2, 3)
self.change_y = random.randrange(-2, 3)
self.rect = pygame.Rect(x-ball_size, y-ball_size, ball_size*2, ball_size*2)
def move(self):
self.rect = self.rect.move(self.change_x, self.change_y)
if self.rect.right >= height or self.rect.left < 0:
self.change_x *= -1
if self.rect.bottom >= width or self.rect.top <= 0:
self.change_y *= -1
def draw(self, surface):
pygame.draw.circle(surface, white, self.rect.center, ball_size)
def make_ball():
ball = Ball()
return ball
In the main application loop, the balls can be moved and drawn in a for-loop and the collision test can be done by .colliderect():
hits = 0
running = True
while running:
# [...]
for ball in ball_list:
if ball.rect.colliderect(rect):
hits += 1
print("hit " + str(hits))
# [...]
for ball in ball_list:
ball.move()
screen.fill(black)
for ball in ball_list:
ball.draw(screen)
Rectangle = pygame.draw.rect(screen, color, rect)
pygame.display.flip()
clock.tick(30)

Control shooting frequency when the button to shoot is pressed?

The bullets shoot all at once when the button is pressed but I only want them to shoot once every 0.5 seconds if the button to shoot is pressed.
I have tried to import time and delay the bullets but it just delayed pygame itself.
Is there any way that I can regulate how frequent the character can shoot?
import pygame
pygame.init()
win = pygame.display.set_mode((700, 480))
pygame.display.set_caption("First project")
run = True
red = (255, 0, 0)
green = (0, 255, 0)
def drawbg():
pygame.display.update()
win.fill((255, 255, 255))
for bullet in bullets:
bullet.draw(win)
class person(object):
def __init__(self, x, y, width, height):
self.x = x
self.y = y
self.width = width
self.height = height
self.vel = 5
self.IsJump = False
self.jumpCount = 10
self.left = False
self.right = False
class projectile(object):
def __init__(self, x, y, radius, colour, facing):
self.x = x
self.y = y
self.radius = radius
self.colour = colour
self.facing = facing
self.vel = 7 * facing
def draw(self, win):
pygame.draw.circle(win, self.colour, (self.x, self.y), self.radius)
man = person(100, 400, 50, 60)
man2 = person(500, 400, 50, 60)
bullets = []
while run:
pygame.time.delay(25)
for bullet in bullets:
if bullet.x < 700 and bullet.x > 0:
bullet.x += bullet.vel
else:
bullets.pop(bullets.index(bullet))
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
keys = pygame.key.get_pressed()
if keys[pygame.K_g]:
if man.left:
facing = -1
else:
facing = 1
if len(bullets) < 5:
bullets.append(projectile(man.x + man.width//2, round(man.y + man.height/2), 6, (100, 100, 100), facing))
if keys[pygame.K_p]:
if man2.left:
facing = -1
else:
facing = 1
if len(bullets) < 50:
bullets.append(projectile(man2.x + man2.width//2, round(man2.y + man2.height/2), 6, (0, 0, 0), facing))
if keys[pygame.K_LEFT] and man2.x > man2.vel:
man2.x -= man2.vel
man2.left = True
man2.right = False
if keys[pygame.K_RIGHT] and man2.x < 700 - man2.width - man2.vel:
man2.x += man2.vel
man2.right = True
man2.left = False
if keys[pygame.K_a] and man.x > man.vel:
man.x -= man.vel
man.left = True
man.right = False
if keys[pygame.K_d] and man.x < 700 - man.width - man.vel:
man.x += man.vel
man.right = True
man.left = False
if not man.IsJump and keys[pygame.K_w]:
man.IsJump = True
man.JumpCount = 10
if man.IsJump:
if man.JumpCount >= -10:
neg = 1
if man.JumpCount < 0:
neg = -1
man.y -= (man.JumpCount ** 2) / 2 * neg
man.JumpCount -= 1
else:
man.IsJump = False
man.JumpCount = 10
if not man2.IsJump and keys[pygame.K_UP]:
man2.IsJump = True
man2.JumpCount = 10
if man2.IsJump:
if man2.JumpCount >= -10:
neg = 1
if man2.JumpCount < 0:
neg = -1
man2.y -= (man2.JumpCount ** 2) / 2 * neg
man2.JumpCount -= 1
else:
man2.IsJump = False
man2.JumpCount = 10
pygame.draw.rect(win, red, (man.x, man.y, man.width, man.height))
pygame.draw.rect(win, green, (man2.x, man2.y, man2.width, man2.height))
drawbg()
pygame.quit()
You can check for the time the last bullet was fired.
initiate the variable by:
import time
last_bullet = time.time()
then change your code to:
if len(bullets) < 5:
if time.time() - last_bullet > 0.5:
last_bullet = time.time()
bullets.append(projectile(man.x + man.width//2, round(man.y + man.height/2), 6, (100, 100, 100), facing))

Categories

Resources