I use pygame and python. I want to make a simple game. Two tanks in scene shoot each other. I have tank class:
class playerObject(pygame.sprite.Sprite):
def __init__(self, rect):
super().__init__()
self.__original_image = pygame.image.load(config.PATH_TO_IMAGES + '\\tank.png').convert_alpha()
self.__original_image = pygame.transform.scale(self.__original_image, (rect.width, rect.height))
self.image = self.__original_image
self.rect = self.image.get_rect()
self.rect.x = rect.x
self.rect.y = rect.y
self.lookAtVector = Vector2(-3, 0)
def addBall(self):
if len(self.balls) < 5:
self.balls.append(ballObject(pygame.Rect(self.rect.centerx, self.rect.top, 10, 10)))
self.balls[-1].lookAtVector.x = self.lookAtVector.x
self.balls[-1].lookAtVector.y = self.lookAtVector.y
angle = 0
__original_image = 0
image = 0
lookAtVector = 0
balls = []
And bullet class:
class ballObject(pygame.sprite.Sprite):
def __init__(self, rect):
super().__init__()
self.image = pygame.image.load(config.PATH_TO_IMAGES + '\\tank.png').convert_alpha()
self.image = pygame.transform.scale(self.image, (5, 5))
self.rect = self.image.get_rect()
self.rect.x = rect.x
self.rect.y = rect.y
self.lookAtVector = Vector2(-3, 0)
lookAtVector = 0
currentSpeed = 1.5
But ball spawns on center of tank. How to do that ball will spawn on tank gun?
tank.png
bullet.png
It needs basic trigonometry.
At start keep distance from center of tank to end of its barrel
self.distance = self.rect.height//2
and later get position of barrel base on angle
def get_barrel_end(self):
rad = math.radians(self.angle)
x = self.rect.centerx - math.sin(rad) * self.distance
y = self.rect.centery - math.cos(rad) * self.distance
return x, y
So you have position for bullet.
I use module math but maybe you could do it with pygame.math
EDIT: The same with pygame.math.Vector2()
At start keep distance from center of tank to end of its barrel
self.distance_vector = pygame.math.Vector2(0, -self.rect.height//2)
and later get position of barrel base on angle
def get_barrel_end(self):
return self.rect.center + self.distance_vector.rotate(-self.angle)
Working example - it draws tank in center and bullet on the end of tank's barrel. Using left/right arrow you can rotate tank and bullet keeps its position on end of tank's barrel.
import pygame
import math
# --- constants ---
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
FPS = 15
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
# --- classes ---
class Tank():
def __init__(self, x, y):
self.image_original = pygame.image.load('tank.png').convert_alpha()
self.angle = 0
self.dirty = False
self.image = self.image_original.copy()
self.rect = self.image.get_rect(center=(x, y))
self.turn_left = False
self.turn_right = False
#self.distance = self.rect.height//2
self.distance_vector = pygame.math.Vector2(0, -self.rect.height//2)
def draw(self, screen):
screen.blit(self.image, self.rect)
def update(self):
if self.turn_left:
self.angle = (self.angle + 2) % 360
self.dirty = True
if self.turn_right:
self.angle = (self.angle - 2) % 360
self.dirty = True
if self.dirty:
self.dirty = False
#print(self.angle)
self.image = pygame.transform.rotate(self.image_original, self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
def handle_event(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
self.turn_left = True
elif event.key == pygame.K_RIGHT:
self.turn_right = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_LEFT:
self.turn_left = False
elif event.key == pygame.K_RIGHT:
self.turn_right = False
#def get_barrel_end(self):
# rad = math.radians(self.angle)
# x = self.rect.centerx - math.sin(rad) * self.distance
# y = self.rect.centery - math.cos(rad) * self.distance
# return x, y
def get_barrel_end(self):
return self.rect.center + self.distance_vector.rotate(-self.angle)
class Bullet():
def __init__(self, x, y):
self.image = pygame.image.load('bullet.png').convert_alpha()
self.rect = self.image.get_rect(center=(x, y))
self.dirty = False
def draw(self, screen):
screen.blit(self.image, self.rect)
def update(self, x, y):
self.rect.center = (x, y)
def handle_event(self, event):
pass
# --- main ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))#, 32)
screen_rect = screen.get_rect()
tank = Tank(*screen_rect.center)
bullet = Bullet(*tank.get_barrel_end())
clock = pygame.time.Clock()
running = True
while running:
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
running = False
tank.handle_event(event)
# - updates -
tank.update()
bullet.update(*tank.get_barrel_end())
# - draws -
screen.fill(WHITE)
tank.draw(screen)
bullet.draw(screen)
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
Related
My game is a top down shooter. When the player is stationary and is shooting in any direction, the bullet goes in the same direction as the mouse. However, when I move diagonally whilst shooting the bullet is no longer in the direction of the mouse position. So basically it works when the player is stationary, but not when I'm moving.
Edit: I will keep trying to fix this but here is a video to better understand the issue https://imgur.com/a/8QRr1PO
Here is the code:
import pygame
from sys import exit
import math
pygame.init()
# window and text
WIDTH = 1280
HEIGHT = 720
FPS = 60
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption('Shooting problem demo')
game_font = pygame.font.Font('freesansbold.ttf', 50)
clock = pygame.time.Clock()
# loads imgs
background = pygame.image.load("background/gamemap.png").convert()
plain_bg = pygame.image.load("background/plain_bg.png").convert()
bullet_img = pygame.image.load("bullets/bluebullet.png").convert_alpha()
class Player(pygame.sprite.Sprite):
def __init__(self, pos):
super().__init__()
self.image = pygame.image.load("handgun/move/survivor-move_handgun_0.png").convert_alpha()
self.image = pygame.transform.rotozoom(self.image, 0, 0.35)
self.base_player_image = self.image
self.pos = pos
self.base_player_rect = self.base_player_image.get_rect(center = pos)
self.rect = self.base_player_rect.copy()
self.player_speed = 10 # was 4
self.shoot = False
self.shoot_cooldown = 0
def player_turning(self):
self.mouse_coords = pygame.mouse.get_pos()
self.x_change_mouse_player = (self.mouse_coords[0] - (WIDTH // 2))
self.y_change_mouse_player = (self.mouse_coords[1] - (HEIGHT // 2))
self.angle = int(math.degrees(math.atan2(self.y_change_mouse_player, self.x_change_mouse_player)))
self.angle = (self.angle) % 360
self.image = pygame.transform.rotate(self.base_player_image, -self.angle)
self.rect = self.image.get_rect(center=self.base_player_rect.center)
def player_input(self):
self.velocity_x = 0
self.velocity_y = 0
keys = pygame.key.get_pressed()
if keys[pygame.K_w]:
self.velocity_y = -self.player_speed
if keys[pygame.K_s]:
self.velocity_y = self.player_speed
if keys[pygame.K_d]:
self.velocity_x = self.player_speed
if keys[pygame.K_a]:
self.velocity_x = -self.player_speed
if self.velocity_x != 0 and self.velocity_y != 0: # moving diagonally
self.velocity_x /= math.sqrt(2)
self.velocity_y /= math.sqrt(2)
if keys[pygame.K_SPACE]:
self.shoot = True
self.is_shooting()
else:
self.shoot = False
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
self.shoot = False
def move(self):
self.base_player_rect.centerx += self.velocity_x
self.base_player_rect.centery += self.velocity_y
self.rect.center = self.base_player_rect.center
def is_shooting(self):
if self.shoot_cooldown == 0 and self.shoot:
self.bullet = Bullet(self.base_player_rect.centerx, self.base_player_rect.centery, self.angle)
self.shoot_cooldown = 20
bullet_group.add(self.bullet)
all_sprites_group.add(self.bullet)
def update(self):
self.player_turning()
self.player_input()
self.move()
if self.shoot_cooldown > 0:
self.shoot_cooldown -= 1
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, angle):
super().__init__()
self.image = bullet_img
self.image = pygame.transform.rotozoom(self.image, 0, 0.1)
self.image.set_colorkey((0,0,0))
self.rect = self.image.get_rect()
self.rect.center = (x, y)
self.x = x
self.y = y
self.speed = 10
self.angle = angle
self.x_vel = math.cos(self.angle * (2*math.pi/360)) * self.speed
self.y_vel = math.sin(self.angle * (2*math.pi/360)) * self.speed
self.bullet_lifetime = 750
self.spawn_time = pygame.time.get_ticks()
def bullet_movement(self):
self.x += self.x_vel
self.y += self.y_vel
self.rect.x = int(self.x)
self.rect.y = int(self.y)
if pygame.time.get_ticks() - self.spawn_time > self.bullet_lifetime:
self.kill()
def update(self):
self.bullet_movement()
class Camera(pygame.sprite.Group):
def __init__(self):
super().__init__()
self.offset = pygame.math.Vector2()
self.floor_rect = background.get_rect(topleft = (0,0))
def custom_draw(self):
self.offset.x = player.rect.centerx - (WIDTH // 2)
self.offset.y = player.rect.centery - (HEIGHT // 2)
#draw the floor
floor_offset_pos = self.floor_rect.topleft - self.offset
screen.blit(background, floor_offset_pos)
for sprite in all_sprites_group:
offset_pos = sprite.rect.topleft - self.offset
screen.blit(sprite.image, offset_pos)
# Groups
all_sprites_group = pygame.sprite.Group()
player = Player((900,900))
all_sprites_group.add(player)
bullet_group = pygame.sprite.Group()
camera = Camera()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
screen.blit(plain_bg, (0,0))
camera.custom_draw()
all_sprites_group.update()
pygame.display.update()
clock.tick(FPS)
Actually, there is no problem at all. It is just an optical illusion. The projectile does not move relative to the player, but relative to the camera. The player is always in the center of the screen, because the player doesn't move, but the camera does. When the camera moves, all objects move with the camera.
For example, if you shoot a bullet to the right and move the player up, it will look like the bullet is moving diagonally to the right and down. To the right because it changes position, to the right and down because the player moves upwards.
To illustrate this, I reduced the speed of the player (self.player_speed = 2) and the speed of the bullet (self.speed = 4) and drew the scene on a checkered background:
if event.type == pygame.KEYUP: only makes sens in the event loop, but not in player_input. Shooting only one bullet at once just needs another condition (and not self.shoot):
if keys[pygame.K_SPACE] and not self.shoot:
self.shoot = True
self.is_shooting()
else:
self.shoot = False
I am stuck on how to write my collision function in my player class. Also should I put my collision check function in my player class or should it be in my bullet class? I honestly don't know. I mean common sense says my bullet should have the collision check function in it, just because I want the top of my bullet checking if it hits a falling cement block sprite, but I don't know.... I would appreciate an answer on how to code my collision function. This would really help me out. My collision function is right below my shoot function in my player class.
My code:
import pygame
pygame.init()
#screen settings
WIDTH = 1000
HEIGHT = 400
screen = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("AutoPilot")
screen.fill((255, 255, 255))
#fps
FPS = 120
clock = pygame.time.Clock()
#load images
bg = pygame.image.load('background/street.png').convert_alpha() # background
bullets = pygame.image.load('car/bullet.png').convert_alpha()
debris_img = pygame.image.load('debris/cement.png')
#define game variables
shoot = False
#player class
class Player(pygame.sprite.Sprite):
def __init__(self, scale, speed):
pygame.sprite.Sprite.__init__(self)
self.bullet = pygame.image.load('car/bullet.png').convert_alpha()
self.bullet_list = []
self.speed = speed
#self.x = x
#self.y = y
self.moving = True
self.frame = 0
self.flip = False
self.direction = 0
#load car
self.images = []
img = pygame.image.load('car/car.png').convert_alpha()
img = pygame.transform.scale(img, (int(img.get_width()) * scale, (int(img.get_height()) * scale)))
self.images.append(img)
self.image = self.images[0]
self.rect = self.image.get_rect()
self.update_time = pygame.time.get_ticks()
self.movingLeft = False
self.movingRight = False
self.rect.x = 465
self.rect.y = 325
#draw car to screen
def draw(self):
screen.blit(self.image, (self.rect.centerx, self.rect.centery))
#move car
def move(self):
#reset the movement variables
dx = 0
dy = 0
#moving variables
if self.movingLeft and self.rect.x > 33:
dx -= self.speed
self.flip = True
self.direction = -1
if self.movingRight and self.rect.x < 900:
dx += self.speed
self.flip = False
self.direction = 1
#update rectangle position
self.rect.x += dx
self.rect.y += dy
#shoot
def shoot(self):
bullet = Bullet(self.rect.centerx + 18, self.rect.y + 30, self.direction)
bullet_group.add(bullet)
#def collision(self):
#write code here
#bullet class
class Bullet(pygame.sprite.Sprite):
def __init__(self, x, y, direction):
pygame.sprite.Sprite.__init__(self)
self.speed = 5
self.image = bullets
self.rect = self.image.get_rect()
self.rect.center = (x,y)
self.direction = direction
def update(self):
self.rect.centery -= self.speed
#check if bullet has gone off screen
if self.rect.top < 1:
self.kill()
#debris class
class Debris(pygame.sprite.Sprite):
def __init__(self, x, y, scale, speed):
pygame.sprite.Sprite.__init__(self)
self.scale = scale
self.x = x
self.y = y
self.speed = speed
self.vy = 0
self.on_ground = True
self.move = True
self.health = 4
self.max_health = self.health
self.alive = True
#load debris
self.image = debris_img
self.rect = self.image.get_rect()
self.rect.center = (x,y)
######################CAR/DEBRIS##########################
player = Player(1,5)
debris = Debris(300,15,1,5)
##########################################################
#groups
bullet_group = pygame.sprite.Group()
debris_group = pygame.sprite.Group()
debris_group.add(debris)
#game runs here
run = True
while run:
#draw street
screen.blit(bg, [0, 0])
#update groups
bullet_group.update()
bullet_group.draw(screen)
debris_group.update()
debris_group.draw(screen)
#draw car
player.draw()
player.move()
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#check if key is down
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
run = False
if event.key == pygame.K_a:
player.movingLeft = True
if event.key == pygame.K_d:
player.movingRight = True
if event.key == pygame.K_SPACE:
player.shoot()
shoot = True
#check if key is up
if event.type == pygame.KEYUP:
if event.key == pygame.K_a:
player.movingLeft = False
if event.key == pygame.K_d:
player.movingRight = False
#update the display
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
I suggest reading How do I detect collision in pygame?. Use pygame.sprite.spritecollide() for the collision detection. Set the dokill argument True. So the bullets gat automatically destroyed.
The following code is base on one of your previous questions: Check collision of bullet sprite hitting cement block sprite
class Car(pygame.sprite.Sprite):
# [...]
def collision(self, debris_group):
for debris in debris_group:
if pygame.sprite.spritecollide(debris, bullet_group, True):
debris.health -= 1
if debris.health <= 0:
debris.kill()
I have made a putt-putt game and now I want to add a slanted wall type. Because of this, I need to use masks for the collision (until now I have just used rects). I have spent hours learning about masks and trying to figure out why my code won't work. There are no errors, the collision just isn't detected.
I have simplified my code down to something much smaller just as a way for me to test it efficiently. From everything I've seen this seems like it should work, but it doesnt. Here it is:
import pygame
# Pygame init stuff
pygame.init()
wind_width = 1200
wind_height = 700
gameDisplay = pygame.display.set_mode((wind_width, wind_height))
pygame.display.set_caption("Mini Golf!")
pygame.display.update()
gameExit = False
clock = pygame.time.Clock()
# Class setups
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("sball.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
def render(self):
self.rect.topleft = (self.x, self.y)
gameDisplay.blit(self.image, self.rect)
class Slant:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("posslant.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image)
def render(self):
self.rect.topleft = (self.x, self.y)
gameDisplay.blit(self.image, self.rect)
# Creating objects
ball = Ball(250, 250)
slant = Slant(270, 250)
# Game loop
gameExit = False
while not(gameExit):
# Moves ball
for event in pygame.event.get():
if event.type == pygame.QUIT:
gameExit = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_UP:
ball.y -= 1
elif event.key == pygame.K_DOWN:
ball.y += 1
elif event.key == pygame.K_LEFT:
ball.x -= 1
elif event.key == pygame.K_RIGHT:
ball.x += 1
# Collision detection
offset_x, offset_y = (slant.rect.x - ball.rect.x), (slant.rect.y - ball.rect.y)
if slant.mask.overlap(ball.mask, (offset_x, offset_y)):
print("hit")
# Draws everything
gameDisplay.fill((0, 0, 0))
ball.render()
slant.render()
pygame.display.update()
clock.tick(100)
The offset parameter of the method overlap() is the relative position of the othermask in relation to the pygame.mask.Mask object.
So the offset is calculated by subtracting the coordinates of slant from the coordinates of ball:
offset_x, offset_y = (slant.rect.x - ball.rect.x), (slant.rect.y - ball.rect.y)
offset = (ball.rect.x - slant.rect.x), (ball.rect.y - slant.rect.y)
if slant.mask.overlap(ball.mask, offset):
print("hit")
When you create the mask images, then I recommend to ensure that the image has per pixel alpha format by calling .convert_alpha():
class Ball:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("sball.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image.convert_alpha()) # <---
class Slant:
def __init__(self, x, y):
self.x = x
self.y = y
self.image = pygame.image.load("posslant.png")
self.rect = self.image.get_rect()
self.mask = pygame.mask.from_surface(self.image.image.convert_alpha()) # <---
Minimal example: repl.it/#Rabbid76/PyGame-SurfaceMaskIntersect
See also: Mask
I'm having problems with making my car move. For an assignment, I have to make a simulator. I need to make cars spawn, let them check if the trafficlight's red, orange or green, and the move when the green light appears. How can i make it so that the cars I spawn move along waypoints i specify by using a waypoint-create class I made?
I have the following python file that makes a list of waypoints.
waypoints.py:
class WayPoint:
def __init__(self, x, y):
self.x = x
self.y = y
def getX(self):
return self.x
def getY(self):
return self.y
class WayPointsList:
def __init__(self):
self.wayPoints = []
def add_wayPoint(self, x, y):
self.wayPoints.append(WayPoint(x, y))
def __len__(self):
return len(self.wayPoints)
def get_wayPoint(self, i):
return [self.wayPoints[i].getX(), self.wayPoints[i].getY()]
When I call this class, the way I use it is like this:
l = WayPointsList
l.add_wayPoint(55,67)
Here's my main.py:
import math
import random, assets
from bus import Bus
from car import Car
from lights import Lights
from waypoints import WayPointsList
import pygame as pg
class Mainstuff(object):
def __init__(self):
pg.init()
self.clock = pg.time.Clock()
self.screen = pg.display.set_mode((assets.screen_width, assets.screen_height))
self.background = pg.image.load(assets.background)
self.running = True
self.car_group = pg.sprite.Group()
self.light_group = pg.sprite.Group()
def event_loop(self):
for event in pg.event.get():
if event.type == pg.QUIT:
running = False
elif event.type == pg.KEYDOWN:
if event.key == pg.K_1:
car110 = Car()
car110.set_image(assets.furore)
car110.rotate(180)
car110.set_position(0, 273)
self.car_group.add(car110)
elif event.key == pg.K_2:
car109 = Car()
car109.set_image(assets.hachura)
car109.rotate(180)
car109.set_position(0, 306)
self.car_group.add(car109)
elif event.key == pg.K_3:
car108 = Car()
car108.set_image(assets.jefferson)
car108.rotate(180)
car108.set_position(0, 343)
self.car_group.add(car108)
elif event.key == pg.K_4:
car107 = Car()
car107.set_image(assets.michelli)
car107.rotate(270)
car107.set_position(410, 550)
self.car_group.add(car107)
elif event.key == pg.K_5:
car106 = Car()
car106.set_image(assets.traceAM)
car106.rotate(270)
car106.set_position(460, 550)
self.car_group.add(car106)
elif event.key == pg.K_6:
car105 = Car()
car105.set_image(assets.traceAM)
car105.set_position(750, 300)
self.car_group.add(car105)
elif event.key == pg.K_7:
car104 = Car()
car104.set_image(assets.rumbler)
car104.set_position(750, 265)
self.car_group.add(car104)
elif event.key == pg.K_8:
car103 = Car()
car103.set_image(assets.rumbler)
car103.rotate(90)
car103.set_position(294, 0)
self.car_group.add(car103)
elif event.key == pg.K_9:
car102 = Car()
car102.set_image(assets.rumbler)
car102.rotate(90)
car102.set_position(337, 0)
self.car_group.add(car102)
elif event.key == pg.K_0:
car101 = Car()
car101.set_image(assets.rumbler)
car101.rotate(90)
car101.set_position(380, 0)
self.car_group.add(car101)
elif event.key == pg.K_b:
car201 = Bus()
car201.set_image(assets.bus)
car201.set_position(700, 229)
self.car_group.add(car201)
elif event.key == pg.K_x:
self.car_group.empty()
def update(self):
self.screen.fill(assets.white)
self.car_group.update()
#self.light_group.update()
def draw(self):
self.screen.blit(self.background,(0,0))
self.car_group.draw(self.screen)
self.light_group.draw(self.screen)
def run(self):
while self.running:
self.event_loop()
self.update()
self.draw()
pg.display.update()
self.clock.tick(assets.FPS)
if __name__ == '__main__':
test = Mainstuff()
test.run()
pg.quit()
And here's my car.py:
import pygame, assets, math
class Car(pygame.sprite.Sprite):
def __init__(self):
super(Car, self).__init__()
self.image = pygame.Surface((assets.car_width, assets.car_height))
self.image.fill(assets.red)
self.rect = self.image.get_rect()
self.speed = 1
self.angle = 0
def set_position(self, x, y):
self.rect.x = x
self.rect.y = y
def get_x_position(self):
return self.rect.x
def get_y_position(self):
return self.rect.y
def set_image(self, filename=None):
if filename != None:
self.image = pygame.image.load(filename).convert()
#self.image.fill(assets.green)
self.rect = self.image.get_rect()
def rotate(self, angle):
self.angle = angle
self.image = pygame.transform.rotate(self.image, angle)
self.rect = self.image.get_rect()
#not sure what to do here
def move_position(self, x, y):
# use angle to calculate direction
radius_angle = math.radians(self.angle)
self.rect.x -= self.speed * math.cos(radius_angle)
self.rect.y -= self.speed * math.sin(radius_angle)
print('move', self.angle, self.rect.x, self.rect.y)
def update(self):
self.move_position(0, 0)
I made example.
It takes first point from list as start position and second as target.
It uses pygame.math.Vector2() to calculate one step
current = current + (target - current).normalize() * speed
When player is in target then it get next point from the list as target.
#!/usr/bin/env python3
import pygame
# === CONSTANS === (UPPER_CASE names)
BLACK = ( 0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
GREEN = ( 0, 255, 0)
BLUE = ( 0, 0, 255)
SCREEN_WIDTH = 600
SCREEN_HEIGHT = 400
# === CLASSES === (CamelCase names)
class Player():
def __init__(self, waypoints, loop=False):
# create green circe
r = 10
self.image = pygame.Surface((2*r, 2*r)).convert_alpha()
self.rect = self.image.get_rect()
self.image.fill((0,0,0,0))
pygame.draw.circle(self.image, (0,255,0), (r, r), r)
# ---
self.loop = loop
self.speed = 5
self.waypoints = waypoints
self.next_point = 0
# set current position
# I use Vector2 because it keeps position as float numbers
# and it makes calcuations easier and more precise
self.current = pygame.math.Vector2(self.waypoints[0])
# set position in rect to draw it
self.rect.center = self.current
# set end point if exists on list
self.target_index = 1
if self.target_index < len(self.waypoints) - 1:
self.target = pygame.math.Vector2(self.waypoints[self.target_index])
self.moving = True
else:
self.target = self.current
self.moving = False
def move(self):
if self.moving:
# get distance to taget
distance = self.current.distance_to(self.target)
#
if distance > self.speed:
self.current = self.current + (self.target - self.current).normalize() * self.speed
self.rect.center = self.current
else:
# put player in tagert place,
# and find new target on list with waypoints
self.current = self.target
self.rect.center = self.current
# set next end point if exists on list
self.target_index += 1
if self.target_index < len(self.waypoints):
self.target = pygame.math.Vector2(self.waypoints[self.target_index])
else:
if self.loop:
self.target_index = 0
else:
self.moving = False
def draw(self, surface):
surface.blit(self.image, self.rect)
# === MAIN === (lower_case names)
# --- init ---
pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
screen_rect = screen.get_rect()
# --- objects ---
start = pygame.math.Vector2(screen_rect.centerx, screen_rect.bottom)
end = start
length = 150
waypoints = [(50, 50), (400, 150), (500, 50), (450, 350), (200, 200), (100, 350), (50, 50)]
player = Player(waypoints, True)
# --- mainloop ---
clock = pygame.time.Clock()
is_running = True
while is_running:
# --- events ---
for event in pygame.event.get():
# --- global events ---
if event.type == pygame.QUIT:
is_running = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
is_running = False
# --- objects events ---
# empty
# --- updates ---
# empty
player.move()
# --- draws ---
screen.fill(BLACK)
for start, end in zip(waypoints, waypoints[1:]):
pygame.draw.line(screen, RED, start, end)
player.draw(screen)
pygame.display.update()
# --- FPS ---
clock.tick(25)
# --- the end ---
pygame.quit()
BTW: because player doesn't change direction when it moves from one point to another (and target doesn't change position) so it could calculate this value only once
(target - current).normalize() * speed
and recalculate it when it get next point as targe.
BTW: example how to calculate with math module in answer to
finding change in x and y given two points and length of vector
In pygame I have a projectile being shot from one character sprite to another which I would like to determine whether there is a collision or not. That is a collision between a shot projectile and another character I will call TRUMP. I have found an equation in a tutorial that is the best example of an arching trajectory that I can accomplish. If that equation could be helped it would be awesome.
def fireshell(self, elapsedTime):
fire = True
FPS = 60 # frames per second setting
fpsClock = pg.time.Clock()
print("fire", self.pos.x, self.pos.y)
fireX = int(self.pos.x)
fireY = int(self.pos.y)
print("fireX", fireX, "fireY", fireY, "elapsed time", elapsedTime)
power = elapsedTime*.0005
x = int(self.pos.x)
y = int(self.pos.y) - 100
while fire:
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
pg.draw.circle(self.screen, RED, (x, y), 5)
x -= int(-(elapsedTime*6))
y += int((((x - fireX)*0.015)**2) - ((elapsedTime*2)/(12 - elapsedTime )))
print("X:", x,"Y:", y)
if y > HEIGHT or x > WIDTH:
fire = False
pg.display.update()
self.clock.tick(20)
My character sprite who I would like to check for collisions with the projectile is here:
class TRUMP(pg.sprite.Sprite):
def __init__(self, spritesheet, all_sprites, mudballGroup, jetsGroup):
pg.sprite.Sprite.__init__(self)
self.spritesheet = spritesheet
self.all_sprites = all_sprites
self.mudballGroup = mudballGroup
self.jetsGroup = jetsGroup
self.current_frame2 = 0
self.last_update2 = 0
self.load_images()
self.image = self.TRUMP_fingers_l
self.rect = self.image.get_rect()
self.rect.center = (WIDTH *3/4), (589)
self.rect.centerx = (WIDTH *3/4)
self.rect.centery = 589
self.rect.centerx = (WIDTH*5/6)
self.rect.centery = 589
self.pos = vec((WIDTH/2), (HEIGHT/2))
self.vel = vec(0, 0)
self.acc = vec(0, 0)
self.dir = 0
To get a ballistic trajectory, you can just add a GRAVITY constant to the y-value of the velocity vector each frame.
For the collision detection you can use pygame.sprite.spritecollide again (you already know how that works).
Here's a complete example:
import sys
import pygame as pg
GRAVITY = 3
class Player(pg.sprite.Sprite):
def __init__(self, pos, color):
super().__init__()
self.image = pg.Surface((50, 30))
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
self.pos = pg.math.Vector2(pos)
self.vel = pg.math.Vector2()
def update(self):
self.pos += self.vel
self.rect.center = self.pos
class Projectile(pg.sprite.Sprite):
def __init__(self, pos, color, target):
super().__init__()
self.image = pg.Surface((7, 5))
self.image.fill(color)
self.rect = self.image.get_rect(center=pos)
self.pos = pg.math.Vector2(pos)
direction = target - self.pos # Vector to the target.
# Normalize, then scale direction to adjust the speed.
self.vel = direction.normalize() * 33
def update(self):
self.pos += self.vel
self.vel.y += GRAVITY
self.rect.center = self.pos
if self.rect.y > 580:
self.kill()
class Game:
def __init__(self):
self.fps = 30
self.screen = pg.display.set_mode((800, 600))
pg.display.set_caption('Ballistic trajectory')
self.clock = pg.time.Clock()
self.bg_color = pg.Color(90, 120, 100)
self.green = pg.Color('aquamarine2')
self.blue = pg.Color(30, 90, 150)
self.font = pg.font.Font(None, 30)
self.player = Player((100, 300), self.green)
self.player2 = Player((400, 300), self.blue)
self.all_sprites = pg.sprite.Group(self.player, self.player2)
self.projectiles = pg.sprite.Group()
self.collisions = 0
self.done = False
def run(self):
while not self.done:
self.handle_events()
self.run_logic()
self.draw()
self.clock.tick(self.fps)
def handle_events(self):
for event in pg.event.get():
if event.type == pg.QUIT:
self.done = True
if event.type == pg.MOUSEBUTTONDOWN:
if event.button == 1:
proj = Projectile(
self.player.rect.center, pg.Color('sienna2'), event.pos)
self.projectiles.add(proj)
self.all_sprites.add(proj)
if event.type == pg.KEYDOWN:
if event.key == pg.K_a:
self.player.vel.x = -3
if event.key == pg.K_d:
self.player.vel.x = 3
if event.key == pg.K_w:
self.player.vel.y = -3
if event.key == pg.K_s:
self.player.vel.y = 3
if event.type == pg.KEYUP:
if event.key == pg.K_a and self.player.vel.x == -3:
self.player.vel.x = 0
if event.key == pg.K_d and self.player.vel.x == 3:
self.player.vel.x = 0
if event.key == pg.K_w and self.player.vel.y == -3:
self.player.vel.y = 0
if event.key == pg.K_s and self.player.vel.y == 3:
self.player.vel.y = 0
def run_logic(self):
self.all_sprites.update()
hits = pg.sprite.spritecollide(self.player2, self.projectiles, True)
for collided_sprite in hits:
self.collisions += 1
def draw(self):
self.screen.fill(self.bg_color)
self.all_sprites.draw(self.screen)
txt = self.font.render('Collisions {}'.format(self.collisions), True, self.green)
self.screen.blit(txt, (20, 20))
pg.display.flip()
if __name__ == '__main__':
pg.init()
game = Game()
game.run()
pg.quit()
sys.exit()