Shoot problem: How to show a bullet before shoot? - python

I would like the 'banana' to be shown when the space bar is pressed, while it follows the character movement, and then to be fired upon the release of the same.
import pygame
from pygame.locals import *
#inizializzazione
pygame.init()
screen=pygame.display.set_mode((400,750))
pygame.display.set_caption('MonkeyBanana')
FPS=60
#load image
img_ground = pygame.image.load('Surf/sfondo.jpg').convert_alpha()
surf_ground=pygame.transform.scale(img_ground,(img_ground.get_width(),screen.get_height()))
class Monkey (pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.sprites = [pygame.transform.scale((pygame.image.load('Surf/Animazioni/monkey_idle_00.png').convert_alpha()),
(screen.get_width() / 5, screen.get_height() / 7)),
pygame.transform.scale((pygame.image.load('Surf/Animazioni/monkey_idle_01.png').convert_alpha()),
(screen.get_width() / 5, screen.get_height() / 7)),
pygame.transform.scale((pygame.image.load('Surf/Animazioni/monkey_idle_02.png').convert_alpha()),
(screen.get_width() / 5, screen.get_height() / 7)),
pygame.transform.scale((pygame.image.load('Surf/Animazioni/monkey_idle_03.png').convert_alpha()),
(screen.get_width() / 5, screen.get_height() / 7))
]
self.launch_monkey=[]
for i in range(0, 19):
filename = "Surf/Animazioni/monkey_{:0>3}.png".format(i)
img = pygame.image.load(filename).convert_alpha()
img_reduced = pygame.transform.scale(img, (screen.get_width() / 5, screen.get_height() / 7))
self.launch_monkey.append(img_reduced)
self.count=0
self.count1 = 0
self.image = self.sprites[self.count]
self.rect = self.image.get_rect()
self.rect.midbottom = (screen.get_width() / 2, screen.get_height())
all_sprites.add(self)
def update(self):
self.count += 0.08
if self.count >= 3:
self.count = 0
self.image = self.sprites[int(self.count)]
if move_right and monkey.rect.right < screen.get_width():
monkey.rect.x += 5
elif move_left and monkey.rect.left > 0:
monkey.rect.x -= 5
def launch(self):
global launch
if launch:
self.count1 += 1
if self.count1 >= 19:
self.count1 = 0
launch = False
self.image = self.launch_monkey[int(self.count1)]
class Banana (pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image= pygame.transform.scale((pygame.image.load('Surf/banana.png').convert_alpha()),
(screen.get_width() / 10, screen.get_height() / 15))
self.rect=self.image.get_rect()
self.rect.center = monkey.rect.center
all_banana.add(self)
here is the problem. Everything works fine on the first launch. The second time I press the space bar, the banana returns to the character's hands instead of continuing the run.
def update(self):
global new_banana
if keep_banana:
self.rect.center = monkey.rect.center
new_banana=False
if launch_banana:
self.rect.y -= 5
if self.rect.top<50:
self.kill()
#create sprite and Group
all_sprites = pygame.sprite.Group()
all_banana= pygame.sprite.Group()
monkey=Monkey()
#other variables
move_right=False
move_left=False
launch=False
launch_banana=False
keep_banana=False
new_banana=False
#principal cicle
done=False
while not done:
for ev in pygame.event.get():
if ev.type == QUIT:
done = True
if ev.type ==KEYDOWN:
if ev.key== K_RIGHT:
move_right=True
idle = False
if ev.key== K_LEFT:
move_left = True
idle = False
if ev.key==K_SPACE:
keep_banana = True
new_banana=True
elif ev.type ==KEYUP:
if ev.key== K_RIGHT:
move_right=False
idle = True
if ev.key== K_LEFT:
move_left=False
idle = True
if ev.key==K_SPACE:
launch_banana=True
keep_banana = False
launch = True
#Game logic
if new_banana:
banana=Banana()
#screen update
screen.blit(surf_ground, (0, 0))
all_sprites.draw(screen)
all_banana.draw(screen)
all_banana.update()
all_sprites.update()
monkey.launch()
pygame.display.flip()
pygame.time.Clock().tick(FPS)
#end principal cicle
pygame.quit()
It works to me in another version without classes,by adding the banana to a new group every time it is fired, but I can't get it to work on the classes. I have tried so many times, but nothing seems to work

launch_banana makes the banana fly:
if launch_banana:
self.rect.y -= 5
launch_banana is False at the beginning, but it is never set False again once it has become True. You have to set launch_banana to False when keep_banana is set True:
launch_banana=False
keep_banana=False
new_banana=False
# [...]
while not done:
for ev in pygame.event.get():
# [...]
if ev.type == KEYDOWN:
# [...]
if ev.key == K_SPACE:
launch_banana = False # <---
keep_banana = True
new_banana = True
elif ev.type == KEYUP:
# [...]
if ev.key == K_SPACE:
launch_banana = True
keep_banana = False
launch = True
Anyway, instead of all these Boolean variables, I propose to add a state to the Banana class. The state can be "keep" at the beginning and can be changed to "launched" after the banana is started.
class Banana (pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image= pygame.transform.scale((pygame.image.load('Surf/banana.png').convert_alpha()),
(screen.get_width() / 10, screen.get_height() / 15))
self.rect=self.image.get_rect()
self.rect.center = monkey.rect.center
all_banana.add(self)
self.state = "keep"
def start(self):
self.state = "launched"
def update(self):
if self.state == "keep":
self.rect.center = monkey.rect.center
elif self.state == "launched":
self.rect.y -= 5
if self.rect.top<50:
self.kill()

Thanks for the reply
So
implement the launch_banana= False in Space Keydown don't work:
if ev.key == K_SPACE:
launch_banana = False # <---
keep_banana = True
new_banana = True
I implemented the banana states in this way in the game logic and work fine, by creating 2 sprite group:
import pygame
from pygame.locals import *
#inizializzazione
pygame.init()
screen=pygame.display.set_mode((400,750))
pygame.display.set_caption('MonkeyBanana')
FPS=60
#load image
img_ground = pygame.image.load('Surf/sfondo.jpg').convert_alpha()
surf_ground=pygame.transform.scale(img_ground,(img_ground.get_width(),screen.get_height()))
class Monkey (pygame.sprite.Sprite):
[...]
class Banana(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.transform.scale((pygame.image.load('Surf/banana.png').convert_alpha()),
(screen.get_width() / 10, screen.get_height() / 15))
self.rect = self.image.get_rect()
self.rect.center = monkey.rect.center
one_banana.add(self) <-------------GroupSingle
self.state = "keep"
def start(self):
self.state = "launched"
def update(self):
if self.state == "keep":
self.rect.center = monkey.rect.center
elif self.state == "launched":
self.rect.y -= 5
if self.rect.top < 50:
self.kill()
#create sprite and Group
all_sprites = pygame.sprite.Group()
all_banana= pygame.sprite.Group()
one_banana= pygame.sprite.GroupSingle() <------add this group
monkey=Monkey()
#other variables
move_right=False
move_left=False
launch=False
launch_banana=False
keep_banana=False
new_banana=False
#principal cicle
done=False
while not done:
for ev in pygame.event.get():
if ev.type == QUIT:
done = True
if ev.type ==KEYDOWN:
if ev.key== K_RIGHT:
move_right=True
idle = False
if ev.key== K_LEFT:
move_left = True
idle = False
if ev.key==K_SPACE:
keep_banana = True
new_banana=True
elif ev.type ==KEYUP:
if ev.key== K_RIGHT:
move_right=False
idle = True
if ev.key== K_LEFT:
move_left=False
idle = True
if ev.key==K_SPACE:
launch_banana=True
keep_banana = False
launch = True
#Game logic
**if new_banana:
banana=Banana()
if launch_banana:
one_banana.empty()
all_banana.add(banana) <------The new logic works
banana.start()
launch_banana=False
new_banana=False**
#screen update
screen.blit(surf_ground, (0, 0))
all_sprites.draw(screen)
all_banana.draw(screen)
one_banana.draw(screen)
one_banana.update()
all_banana.update()
all_sprites.update()
monkey.launch()
pygame.display.flip()
pygame.time.Clock().tick(FPS)
#end principal cicle
pygame.quit()
There is a way much simple to do this?
Thank you in any case

Related

I need to be able to pause the whole game, but only my car sprite pauses when I press the p key on my keyboard

I implemented a pause feature in my game in the while loop at the end of my code, but the problem is that only my car sprite pauses. I have tried a bunch of different ways setting up my pause code but none of the ways I arrange it seems to pause the whole game.
I want to be able to pause my whole game when I press the p key on my keyboard, not just my car sprite. Can someone please tell me how I can fix this code? The code that will pause the whole game will be nice. Please, the rearranged code that will pause the whole game will help. Also I don't just want the answer, I want a quick explanation of why only my car sprite would pause instead of the whole game.
My code:
import random
import pygame
import pygame.freetype
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
self.score = 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)
#check collision
def collision(self, debris_group):
for debris in debris_group:
if debris.health > 0 and pygame.sprite.spritecollide(debris, bullet_group, True):
debris.health -= 1
if debris.health <= 0:
self.score += 1
#player stats/score
def stats(self):
myfont = pygame.font.SysFont('comicsans', 30)
scoretext = myfont.render("Score: " + str(self.score), 1, (0,0,0))
screen.blit(scoretext, (100,10))
#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.centery < 1:
self.kill()
#debris class
class Debris(pygame.sprite.Sprite):
def __init__(self,scale,speed):
pygame.sprite.Sprite.__init__(self)
self.scale = scale
self.x = random.randrange(100,800)
self.speed_y = 10
self.y = 15
self.speed = speed
self.vy = 0
self.on_ground = True
self.move = True
self.health = 4
self.max_health = self.health
self.alive = True
self.velocity = random.randrange(1,2)
self.speed_x = random.randrange(-3,3)
self.moving_down = True
self.is_destroyed = False
#load debris
self.image = debris_img
self.rect = self.image.get_rect()
self.rect.x = random.randrange(100, 800)
self.rect.y = random.randrange(-150, -100)
self.rect.center = (self.x,self.y)
#load explosion
self.img_explosion_00 = pygame.image.load('explosion/0.png').convert_alpha()
self.img_explosion_00 = pygame.transform.scale(self.img_explosion_00, (self.img_explosion_00.get_width() * 2,
self.img_explosion_00.get_height() * 2))
self.img_explosion_01 = pygame.image.load('explosion/1.png').convert_alpha()
self.img_explosion_01 = pygame.transform.scale(self.img_explosion_01, (self.img_explosion_01.get_width() * 2,
self.img_explosion_01.get_height() * 2))
self.img_explosion_02 = pygame.image.load('explosion/2.png').convert_alpha()
self.img_explosion_02 = pygame.transform.scale(self.img_explosion_02, (self.img_explosion_02.get_width() * 2,
self.img_explosion_02.get_height() * 2))
self.img_explosion_03 = pygame.image.load('explosion/3.png').convert_alpha()
self.img_explosion_03 = pygame.transform.scale(self.img_explosion_03, (self.img_explosion_03.get_width() * 2,
self.img_explosion_03.get_height() * 2))
#explosion list
self.anim_explosion = [self.img_explosion_00,
self.img_explosion_01,
self.img_explosion_02,
self.img_explosion_03]
self.anim_index = 0
self.frame_len = 20 #frames before explosion animation disappears
#spawn new debris
def spawn_new_debris(self):
self.rect.x = random.randrange(100, 800)
self.rect.y = random.randrange(-150, -100)
self.velocity = random.randrange(1, 2)
self.speed_x = random.randrange(-3, 3)
#respawn debris when they go of the screen
def boundaries(self):
if self.rect.left > WIDTH + 10 or self.rect.right < -10 or self.rect.top > HEIGHT + 10:
self.spawn_new_debris()
#update image
def update(self):
self.rect.y += self.velocity
self.rect.x += self.speed_x
self.boundaries()
if self.health <= 0:
max_index = len(self.anim_explosion) - 1
if self.anim_index > max_index:
self.kill()
else:
if self.frame_len == 0:
self.image = self.anim_explosion[self.anim_index]
self.anim_index += 1
self.frame_len = 20
else:
self.frame_len -= 1
#make debris fall down
def falldown(self):
self.rect.centery += self.velocity
if self.moving_down and self.rect.y > 350:
self.kill()
######################CAR/DEBRIS##########################
player = Player(1,5)
##########################################################
#groups
bullet_group = pygame.sprite.Group()
debris_group = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
for x in range(100):
d = Debris(1, 5)
debris_group.add(d)
all_sprites.add(d)
#game runs here
run = True
paused = False
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()
player.collision(debris_group)
player.stats()
#update all sprites
all_sprites.update()
all_sprites.draw(screen)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pause game
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
if paused == True:
continue
#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()
Move your
for event in pygame.event.get():
right below the while loop, and move your
if paused:
continue
below the for loop, like so:
run = True
paused = False
while run:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pause game
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
#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
if paused:
continue
#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()
player.collision(debris_group)
player.stats()
#update all sprites
all_sprites.update()
all_sprites.draw(screen)
#update the display
pygame.display.update()
pygame.display.flip()
clock.tick(FPS)
pygame.quit()
The reason only your car sprite paused when you pressed the q key is because, as you can see in your original code here:
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
#pause game
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_p:
paused = not paused
if paused == True:
continue
you put the
if paused == True:
continue
inside the for loop, not the while loop, so the lines that move the car would be blocked, but the rest of the code within the while loop will still get executed.

Pygame drawing a certain number of objects on the screen at a time

I wanted to make a simple dance game using pygame.
class Sword():
def __init__(self, image, speed, center):
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.center = center
self.speed = speed # object speed
self.alive = True # draw object if not hit the enemey
def live(self, enemy): # check if hit the enemy
if self.rect.colliderect(enemy):
self.alive = False
mixer.music.play()
def update(self): # move object
self.rect.x += self.speed
def draw(self, surface): # draw object
if self.alive:
surface.blit(self.image, (self.rect))
I can draw and move my object on the screen.
sword = [Sword('sword.png',1,(100,420))],
[Sword('sword.png',5,(200,420))],
[Sword('sword.png',3,(300,420))]]
while (True):#Mainloop
kare = pygame.draw.rect(display,((255,0,0)), pygame.Rect(700,420,100,100))
for i, tile in sorted(enumerate(sword), reverse=True):
tile.update()
tile.live(kare)
tile.draw()
if not tile.live:
sword.pop(i)#delete object
my code works flawlessly so far;=)
but when I want to draw 4 objects on the screen every time, things don't go as I want.
def main():
extend = []
running = True
while running:
if len(extend) >= 3:
running = False
else:
for i in range(4):
extend.append(Sword('sword.png',1,(100,420)))
return extend
i have a simple function to draw 4 objects at a time to the screen but when i use this in my main loop my objects don't move
while True:
sword = main()#call function
kare = pygame.draw.rect(display,((255,0,0)), pygame.Rect(700,420,100,100))
for i, tile in sorted(enumerate(sword), reverse=True):
tile.update()
tile.live(kare)
tile.draw(display)
if not tile.alive:
sword.pop(i)
First of all, I apologize for this long post, but I wanted the topic to be clear, how do I edit my code so that there are 4 on the screen each time?
Thank you for your interest...
Here is the full code of the solution:
import pygame, sys, math, random
from pygame import mixer
from pygame.locals import *
mainClock = pygame.time.Clock()
pygame.init()
pygame.display.set_caption('game base')
WINDOW_SIZE = (980,570)
screen = pygame.display.set_mode((WINDOW_SIZE), 0, 32)
display = pygame.Surface((WINDOW_SIZE))
RIGHT_KEY,UP_KEY,LEFT_KEY,DOWN_KEY = False,False,False,False
class Sword():
def __init__(self, image, speed, center):
self.image = pygame.image.load(image)
self.rect = self.image.get_rect()
self.rect.center = center
self.speed = speed
self.alive = True
def live(self, enemy):
if self.rect.colliderect(enemy):
self.alive = False
#mixer.music.play()
def update(self):
self.rect.x += self.speed
def draw(self, surface):
if self.alive:
surface.blit(self.image, (self.rect))
def main():
extend = []
running = True
if len(extend) >= 3:
running = False
while running:
if len(extend) >= 4:
running = False
else:
for i in range(5):
extend.append(Sword('sword.png',i,(100,420)))
return extend
sword = main()
while True:
display.fill((250,198,14))
kare = pygame.draw.rect(display,((255,0,0)), pygame.Rect(700,420,100,100))
all_not_alive = True
for tile in sword:
tile.update()
tile.live(kare)
tile.draw(display)
if not tile.alive:
tile.rect.x = 100
tile.alive = True
# Buttons ------------------------------------------------ #
RIGHT_KEY,UP_KEY,LEFT_KEY,DOWN_KEY = False,False,False,False
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_UP:
UP_KEY = True
if event.key == pygame.K_DOWN:
DOWN_KEY = True
if event.key == pygame.K_LEFT:
LEFT_KEY = True
if event.key == pygame.K_RIGHT:
RIGHT_KEY = True
# Update -------------------------------------------------
screen.blit(display,(0,0))
pygame.display.update()
mainClock.tick(60)
The problem was that you have recreated all swords every time when the game updated. You should recreate the sword only if it is not alive.
EDIT
I have moved the code which generates the Swords out of the loop:
sword = main()
and I am moving all swords to the start after they collide:
if not tile.alive:
tile.rect.x = 100
tile.alive = True

Issue with Collision Detection in Pygame for simple 2D Platformer

This is my first post so please let me know if I should alter anything.
I am attempting to create a simple test 2D platformer, this is the first pygame project I've undertaken without a tutorial. The issue I am encountering is due to this can_jump variable. I am using this variable to stop the player from being able to jump when not on a platform or the bottom of the screen. I had attempted other methods but this is where I have ended up. I feel I've gotten so close.
The issue at the moment appears to be that the variable self.can_jump1 continuously swaps between True and False in the platform loop (at exactly 3 Falses for every True I have noticed) when standing on a platform, I have isolated it to the commented section in collision_check(). I don't know why this happens, and as far as I can tell the rest of the code works as I want it.
I would be happy to provide any extra information/clarification if need be.
Here's my code:
import pygame, sys, random
#---Classes----------------------------------------#
class Block(pygame.sprite.Sprite): #inherited by rest of classes
def __init__(self,path,x_pos,y_pos,size_x,size_y):
super().__init__()
self.image = pygame.transform.scale(pygame.image.load(path).convert_alpha(),(size_x,size_y))
self.rect = self.image.get_rect(center = (x_pos,y_pos))
class PlatformRect(pygame.sprite.Sprite):
def __init__(self,x_pos,y_pos,size_x,size_y,colour,players):
super().__init__()
self.center_x = x_pos
self.center_y = y_pos
self.size_x = size_x
self.size_y = size_y
self.colour = colour
self.players = players
self.can_jump1 = None
self.image = pygame.Surface((self.size_x,self.size_y))
self.image.fill(self.colour)
self.rect = self.image.get_rect(center = (self.center_x,self.center_y))
def collision_check(self):
if pygame.sprite.spritecollide(self,self.players,False):
collision_paddle = pygame.sprite.spritecollide(self,self.players,False)[0].rect
if abs(self.rect.top - collision_paddle.bottom) < 10:
collision_paddle.bottom = self.rect.top
player.movement_y = 0
self.can_jump1 = True
else: #error isolated to here,consistently fluctuates between True and False, not sure why
self.can_jump1 = False
print(self.can_jump1) #if standing on platform should only produce True, everywhere else produce False
def can_jump_check(self):
return self.can_jump1
def update(self):
self.collision_check()
class Player(Block):
def __init__(self,path,x_pos,y_pos,size_x,size_y,speed_x,acceleration_y):
super().__init__(path,x_pos,y_pos,size_x,size_y)
self.size_x = size_x
self.size_y = size_y
self.speed_x = speed_x
self.acceleration_y = acceleration_y
self.movement_x = 0
self.movement_y = 0
def screen_constrain(self):
if self.rect.bottom >= sresy:
self.rect.bottom = sresy
if self.rect.left <= 0:
self.rect.left = 0
if self.rect.right >= sresx:
self.rect.right = sresx
def update(self):
if abs(self.movement_y) <= 9:
self.movement_y += GRAVITY
self.rect.centery += self.movement_y
self.rect.centerx += self.movement_x
self.screen_constrain()
class GameManager:
def __init__(self,player_group,platform_group):
self.player_group = player_group
self.platform_group = platform_group
self.can_jump = True
def run_game(self):
#---drawing---#
self.player_group.draw(screen)
self.platform_group.draw(screen)
#---updating---#
self.player_group.update()
self.platform_group.update()
def game_checking(self):
#---checking---#
for sprite_platform in self.platform_group.sprites():
if not sprite_platform.can_jump_check() == True:
self.can_jump = False
else:
self.can_jump = True
return self.can_jump
#---Setup------------------------------------------#
#---constants-----#
global GRAVITY
GRAVITY = 0.25
#---Gamevariables-----#
can_jump = True
#---colour---#
bg_colour = (50,50,50)
white_colour = (255,255,255)
black_colour = (0,0,0)
#---res---#
resx = 900
resy = 675
sresx = resx - 50
sresy = resy - 50
#---start window-----#
clock = pygame.time.Clock()
screendisplay = pygame.display.set_mode((resx,resy))
screendisplay.fill(bg_colour)
pygame.display.set_caption("PlatformerGame1 started 09/02/2021")
screen = pygame.Surface((sresx,sresy))
screen.fill(white_colour)
#---startgame-----#
player = Player("assets\\pics\\TestPlayerFigure1_256512.png",100,100,32,64,10,30)
player_group = pygame.sprite.GroupSingle()
player_group.add(player)
platform1 = PlatformRect(100,550,100,10,black_colour,player_group)
platform_group = pygame.sprite.Group()
platform_group.add(platform1)
game_manager = GameManager(player_group,platform_group)
#---Loop--------------------------------------------#
while True:
#---events-----#
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
sys.exit()
if event.key == pygame.K_SPACE:
if can_jump == True:
player.movement_y = 0
player.movement_y -= player.acceleration_y * GRAVITY
if event.key == pygame.K_a:
player.movement_x -= player.speed_x
if event.key == pygame.K_d:
player.movement_x += player.speed_x
if event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
if player.movement_y < 0:
player.movement_y = 0
if event.key == pygame.K_a:
player.movement_x += player.speed_x
if event.key == pygame.K_d:
player.movement_x -= player.speed_x
#---background---#
screendisplay.blit(screen,(25,25))
screen.fill(white_colour)
#---running---#
game_manager.run_game()
if not game_manager.game_checking() == True and player.rect.bottom < sresy:
can_jump = False
else:
can_jump = True
#print(can_jump)
#---updating-----#
pygame.display.update()
clock.tick(60)
When the player is standing on the platform, the player does not collide with the platform and self.can_jump1 is set False. Test if the player is on the platform when the player does not collide with the platform:
self.can_jump1 = False
self.can_jump1 = self.rect.top == self.players.sprites()[0].rect.bottom
Method collision_check:
class PlatformRect(pygame.sprite.Sprite):
# [...]
def collision_check(self):
collide_list = pygame.sprite.spritecollide(self,self.players,False)
if collide_list:
collision_paddle = collide_list[0].rect
if abs(self.rect.top - collision_paddle.bottom) < 10:
collision_paddle.bottom = self.rect.top
player.movement_y = 0
self.can_jump1 = True
else:
self.can_jump1 = self.rect.top == self.players.sprites()[0].rect.bottom
GameManager.game_checking needs to check if the plyer collides with any platform:
class GameManager:
# [...]
def game_checking(self):
#---checking---#
self.can_jump = False
for sprite_platform in self.platform_group.sprites():
if sprite_platform.can_jump_check() == True:
self.can_jump = True
return self.can_jump
This can be simplified with the any function:
class PlatformRect(pygame.sprite.Sprite):
# [...]
def game_checking(self):
#---checking---#
self.can_jump = any(p.can_jump_check() for p in self.platform_group)
return self.can_jump

How do I make an enemy follow the player in Pygame?

So, I'm a beginner to Python and Pygame and would like some help. I'm trying to make a game similar to Asteroids and I've made most of this program by looking around at other examples on the internet. However, I'm stuck on this problem: how do I get an enemy sprite to follow the player sprite? I've googled how to do so and tried implementing the same thing on my code but the sprite just stays in one place and doesn't follow the player. I've used the vector stuff to create the player sprite and I still barely understand how that works. I'm sort of on a tight schedule so I don't have time to thoroughly understand this stuff but I intend to later. Sorry if I haven't explained my code properly but I'm still trying to understand how Pygame works and most of this code is basically just copied.
import random, math, pygame, time, sys
from pygame.locals import *
from pygame.math import Vector2
######Setting up Variables
class settings:
fps = 30
windowwidth = 590
windowheight = 332
class ship:
HEALTH = 3
SPEED = 4
SIZE = 25
class colours:
black = (0, 0, 0)
white = (255, 255, 255)
###########################
######################################################################################
class Player(pygame.sprite.Sprite):
def __init__(self,image_file , pos=(0,0)):
super(Player, self).__init__()
self.image = pygame.image.load(image_file)
self.image = pygame.transform.scale(self.image, (25,25))
self.original_image = self.image
self.rect = self.image.get_rect(center=pos)
self.position = Vector2(pos)
self.direction = Vector2(1, 0)
self.speed = 0
self.angle_speed = 0
self.angle = 0
def update(self):
if self.angle_speed != 0:
self.direction.rotate_ip(self.angle_speed)
self.angle += self.angle_speed
self.image = pygame.transform.rotate(self.original_image, -self.angle)
self.rect = self.image.get_rect(center=self.rect.center)
self.position += self.direction * self.speed
self.rect.center = self.position
class Enemy(pygame.sprite.Sprite):
def __init__(self, image_file, pos=(0,0)):
super(Enemy, self).__init__()
self.image = pygame.image.load(image_file)
self.image = pygame.transform.scale(self.image, (25, 25))
self.original_image = self.image
self.rect = self.image.get_rect(center=pos)
self.speed = 1
def move_towards_player(self, Player):
dx, dy = self.rect.x - Player.rect.x, self.rect.y - Player.rect.y
dist = math.hypot(dx, dy)
dx, dy = dx/dist, dy/dist
self.rect.x += dx * self.speed
self.rect.y += dy * self.speed
class Background(pygame.sprite.Sprite):
def __init__(self, image_file, location):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(image_file)
self.rect = self.image.get_rect()
self.rect.left, self.rect.top = location
def main():
pygame.init()
screen = pygame.display.set_mode((settings.windowwidth, settings.windowheight))
pygame.display.set_caption('Final Game')
background = Background('space.jpg',[0,0])
global player, enemy
player = Player('SpaceShip.png',[200,100])
playersprite = pygame.sprite.RenderPlain((player))
enemy = Enemy('Enemy Hexagon.png',[300,150])
enemysprite = pygame.sprite.RenderPlain((enemy))
fpsClock = pygame.time.Clock()
intro = True
while intro == True:
myFont = pygame.font.SysFont('freesansbold.ttf', 75)
otherFont = pygame.font.SysFont('freesansbold.ttf', 30)
SurfaceFont = myFont.render("Space Destroyer", True, (colours.white))
SurfaceFont2 = otherFont.render("Press Space to Start", True, (colours.white))
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_SPACE:
intro= False
screen.blit(SurfaceFont,(50,50))
screen.blit(SurfaceFont2,(125,125))
pygame.display.flip()
screen.blit(background.image, background.rect)
while intro == False:
fpsClock.tick(settings.fps)
for event in pygame.event.get():
enemy.rect.x
if event.type == QUIT:
pygame.quit()
sys.exit()
elif event.type == KEYDOWN:
if event.key == K_UP:
player.speed += 4
if event.key == K_LEFT:
player.angle_speed = -4
if event.key == K_RIGHT:
player.angle_speed = 4
if player.position.y < 0:
player.position = (player.position.x ,332)
elif player.position.y > settings.windowheight:
player.position = (player.position.x, 0)
elif player.position.x < 0:
player.position = (590, player.position.y)
elif player.position.x > settings.windowwidth:
player.position = (0, player.position.y)
elif event.type == KEYUP:
if event.key == K_LEFT:
player.angle_speed = 0
if event.key == K_RIGHT:
player.angle_speed = 0
if event.key == K_UP:
player.speed = 0
screen.fill(colours.white)
screen.blit(background.image, background.rect)
enemysprite.draw(screen)
enemysprite.update()
playersprite.draw(screen)
playersprite.update()
pygame.display.update()
playersprite.update()
pygame.display.flip()
if __name__ == '__main__': main()
You never called move_towards_player. Try adding this:
...
enemysprite.draw(screen)
enemysprite.update()
playersprite.draw(screen)
playersprite.update()
enemy.move_towards_player(player)
...

Pygame - Collisions With Floor/Walls

Hello I am currently making a survival shooter but cannot find any way on how to do my collisions! I am trying to make the player collide with the floors but with no success. Here is an image of the map (I want to collide with moon blocks):
http://i.stack.imgur.com/R2apz.jpg
Here is an image of moon blocks with no Background:
http://i.stack.imgur.com/W9H2m.png
And finally here is my code:
import pygame
import random
gravity = .5
jump_time = 2000
x = 0
y = 0
# Player
class Player(pygame.sprite.Sprite):
def __init__(self, x, y, gravity):
# Player dimensions and position
self.gravity = gravity
# Player image and animation
self.images = []
self.images.append(pygame.image.load('images/player.png'))
self.images.append(pygame.image.load('images/player2.png'))
#~ self.images.append(pygame.image.load('ball1.png'))
#~ self.images.append(pygame.image.load('ball2.png'))
self.maxImage = len(self.images)
self.currentImage = 0
#~ self.rect = pygame.Rect(x, y, 80, 80)
self.rect = self.images[0].get_rect()
self.rect.x = x
self.rect.y = y
self.timeTarget = 10
self.timeNum = 1
self.velX = 0
self.velY = 0
# Jump and gravity
self.vSpeed = 3
self.jumpForce = 15
self.maxVspeed = 3
self.isJumping = False
# Jump inputs
def handle_events(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
if not self.isJumping:
self.isJumping = True
elif event.key == pygame.K_a:
self.velX = -5
elif event.key == pygame.K_d:
self.velX = +5
elif event.type == pygame.KEYUP:
if event.key in (pygame.K_a, pygame.K_d):
self.velX = 0
# PLayer updates
def update(self, ground):
keys = pygame.key.get_pressed()
# Jumping
self.vSpeed += gravity
if self.vSpeed > self.maxVspeed:
self.vSpeed = self.maxVspeed
self.rect.y += self.vSpeed
if self.rect.y >= ground.y:
self.vSpeed = 0
self.rect.y = ground.y
self.isJumping = False
if keys[pygame.K_SPACE]:
if not self.isJumping:
self.isJumping = True
if self.isJumping:
if pygame.time.get_ticks() < jump_time:
self.isJumping == True
else:
self.isJumping = False
self.vSpeed -= self.jumpForce
#print "isJumping:", self.isJumping
# Animations
if self.timeNum == self.timeTarget:
self.currentImage += 1
if self.currentImage >= self.maxImage:
self.currentImage = 0
self.timeNum = 0
self.rect.centerx += self.velX
self.rect.centery += self.velY
# Screen wrap
if self.rect.right > 1280:
self.rect.left = 0
elif self.rect.left < 0:
self.rect.right = 1280
# Player rendering
def render(self, surface):
surface.blit(self.images[self.currentImage], self.rect)
#----------------------------------------------------------------------
class Zombie():
def __init__(self, x, y):
self.image = pygame.image.load('images/zombie.png')
#~ self.image = pygame.image.load('ball2.png')
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.direction_left = True
def update(self, surface_rect):
if self.direction_left:
self.rect.x -= 1
if self.rect.left <= surface_rect.left:
self.direction_left = not self.direction_left
else:
self.rect.x += 1
if self.rect.right >= surface_rect.right:
self.direction_left = not self.direction_left
def render(self, surface):
surface.blit(self.image, self.rect)
#----------------------------------------------------------------------
class Background():
def __init__(self):
self.image = pygame.image.load('images/arena2.jpg')
#~ self.image = pygame.image.load('background.jpg')
self.rect = self.image.get_rect()
def render(self, surface):
surface.blit(self.image, self.rect)
#----------------------------------------------------------------------
class Game():
def __init__(self):
pygame.init()
# A few variables
self.gravity = .50
self.ground = pygame.Rect(0, 640, 1280, 80)
# Screen
size = (1280, 720)
self.screen = pygame.display.set_mode(size)
pygame.display.set_caption('Moon Survival!')
# Moon / Background
self.moon = Background()
# Zombies
self.zombies = []
for i in range(10):
self.zombies.append( Zombie(random.randint(0,1280), random.randint(0,720)) )
# Player
self.player = Player(25, 320, self.gravity)
# Font for text
self.font = pygame.font.SysFont(None, 72)
# Pause - center on screen
self.pause_text = self.font.render("PAUSE", -1, (255,0,0))
self.pause_rect = self.pause_text.get_rect(center = self.screen.get_rect().center)
def run(self):
clock = pygame.time.Clock()
# "state machine"
RUNNING = True
PAUSED = False
GAME_OVER = False
# Game loop
while RUNNING:
# (all) Events
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = False
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
RUNNING = False
elif event.key == pygame.K_p:
PAUSED = not PAUSED
# Player/Zomies events
if not PAUSED and not GAME_OVER:
self.player.handle_events(event)
# (all) Movements / Updates
if not PAUSED and not GAME_OVER:
self.player.update(self.ground)
for z in self.zombies:
z.update(self.screen.get_rect())
# (all) Display updating
self.moon.render(self.screen)
for z in self.zombies:
z.render(self.screen)
self.player.render(self.screen)
if PAUSED:
self.screen.blit(self.pause_text, self.pause_rect)
pygame.display.update()
# FTP
clock.tick(100)
# --- the end ---
pygame.quit()
#---------------------------------------------------------------------
Game().run()
Any help will be greatly appreciated, thank you.
I made a very similar game for PyWeek #17 called "Miner".
Here's a screenshot: http://media.pyweek.org/dl/17/powrtoch/miner_ss.png (you can see the similarity!)
You can find the relevant code on GitHub here: https://github.com/marcusmoller/pyweek17-miner/blob/master/miner/engine.py#L202-L220:
def checkCollision(self, sprite, xVel, yVel):
for x in range(len(level.levelStructure)):
for y in range(len(level.levelStructure[x])):
block = level.levelStructure[x][y]
if block is not None:
if pygame.sprite.collide_rect(sprite, block):
if xVel < 0:
sprite.rect.x = block.rect.x + block.rect.w
if xVel > 0:
sprite.rect.x = block.rect.x - sprite.rect.w
if yVel < 0:
sprite.rect.y = block.rect.y + block.rect.h
if yVel > 0 and not sprite.onGround:
sprite.onGround = True
sprite.rect.y = block.rect.y - sprite.rect.h
You could do something like this using collide_rect:
# See if the Sprite block collides with anything in the Ground block_list
for block in groundBlocks:
if(collide_rect(player, block)):
# do not fall
else:
# fall
where all Block you can walk on are in the groundBlocks list.

Categories

Resources