Pygame Sprite not updating immediately - python

EDIT: I have fixed it, there should be a self infront of the return for invader_position_x
I am making a space invaders game. However I have encountered strange behavior.
When I press the right key on my keyboard, the sprite does not move but after holding it down for a few seconds, it jumps to the right boundary.
The space invader game is using classes and this is the main class:
# IMPORTS
import pygame, sys
from pygame.locals import *
import invader
pygame.init()
###################
#IMAGE SIZE AND FPS
width = 800
height = 600
fps = 30
fpsClock = pygame.time.Clock()
DISPLAY = pygame.display.set_mode((width,height))
#################
#OTHER VARIABLES
pygame.display.set_caption('space invaders!')
white = (225,225,225)# The colour white
invader_sprite = pygame.image.load("cross.png")
invader_lenght = 40
invader_position_x = 400
invader_position_y = 560
right_boundary = width- invader_lenght
keypress = ""
my_invader = invader.Invader(invader_position_x,right_boundary,keypress)# Initialising invader
while True: # main game loop
DISPLAY.fill(white)
DISPLAY.blit(invader_sprite,(invader_position_x,invader_position_y))
keypress = pygame.key.get_pressed()
invader_position_x=my_invader.invader_move(keypress,right_boundary,invader_position_x)
print(invader_position_x)
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
sys.exit()
pygame.display.update()
fpsClock.tick(fps)
This is my invader class in a seperate file:
import pygame, sys
from pygame.locals import *
pygame.init()
class Invader():
def __init__(self,invader_position_x,right_boundary,keypress):
self.invader_position_x= invader_position_x
self.right_boundary=right_boundary
self.keypress= keypress
def invader_move(self,keypress,right_boundary,invader_position_x):
if self.invader_position_x> right_boundary:#Right boundary
invader_position_x=right_boundary-5
if self.invader_position_x<0:
invader_position_x= 5
if keypress[K_RIGHT]:# right
self.invader_position_x = self.invader_position_x+ 5
elif keypress[K_LEFT]:# left
self.invader_position_x =self.invader_position_x- 5
return invader_position_x
So what is the solution to fixing this weird behaviour?

I think problem is that you have two variables to keep position . But I didn't check this - I changed code to make it better organized and problem disappeared.
#!/usr/bin/env python3
import pygame
# --- constants ---
WIDTH = 800
HEIGHT = 600
FPS = 30
WHITE = (225, 225, 225)
# --- classes ---
class Invader():
def __init__(self, x, y):
self.image = pygame.image.load("cross.png")
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def draw(self, screen):
screen.blit(self.image, self.rect)
def update(self, screen_rect, keypress):#EDIT Replaced screen with screem_rect
if keypress[pygame.K_RIGHT]: # right
self.rect.x += 15
elif keypress[pygame.K_LEFT]: # left
self.rect.x -= 15
if self.rect.right > screen_rect.right: # right boundary
self.rect.right = screen_rect.right
if self.rect.left < screen_rect.left:
self.rect.left = screen_rect.left
# --- main ---
# - init -
pygame.init()
screen = pygame.display.set_mode((WIDTH, HEIGHT))
screen_rect = screen.get_rect()
pygame.display.set_caption('space invaders!')
# - objects -
my_invader = Invader(400, 560)
# - mainloop -
fps_clock = pygame.time.Clock()
while True: # main game loop
# - events -
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
quit()
keypress = pygame.key.get_pressed()
# - updates -
my_invader.update(screen_rect, keypress)
# - draws -
screen.fill(WHITE)
my_invader.draw(screen)
pygame.display.update()
# - FPS -
fps_clock.tick(FPS)

Related

Create raindrops onto screen using pygame

This is an exercise from Crash Course - at this stage I am trying to create a row of raindrops, but I believe in my for loop, something is broken.. I am updating the image's rect position at each iteration and then adding that into sprite Group, why will this not draw() onto the screen?
import sys
import pygame
from raindrops import Raindrop
from pygame.sprite import Group
def let_it_rain():
'''initialize pygame, settings, and screen object'''
pygame.init()
screen_width = 1200
screen_height = 800
bg_color = (144, 177, 226)
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption("Let It Rain")
raindrop = Raindrop(screen)
raindrops = Group()
#number of drops in a row
spacex = screen_width - (2 * raindrop.rect.width)
raindrop_number_x = int(spacex / (2 * raindrop.rect.width))
#start window for raindrops
while True:
screen.fill(bg_color)
for event in pygame.event.get():
if event.type == pygame.QUIT:
sys.exit()
for raindrop_number in range(raindrop_number_x):
raindrop = Raindrop(screen)
raindrop.rect.x = raindrop.rect.x + 2 * raindrop.rect.x * raindrop_number_x
raindrops.add(raindrop)
raindrops.draw(screen)
pygame.display.flip()
let_it_rain()
and here's my raindrops class in another module
import pygame
from pygame.sprite import Sprite
class Raindrop(Sprite):
def __init__(self, screen):
#load image
super(Raindrop, self).__init__()
self.screen = screen
self.pic = pygame.image.load('rain.png')
self.image = pygame.transform.smoothscale(self.pic,(50,60))
self.rect = self.image.get_rect()
#starting position
self.rect.x = self.rect.width
self.rect.y = self.rect.height
def blit(self):
raindrops.draw(screen)
# self.screen.blit(self.image, self.rect)
Feel like this has something to do with blit vs. draw OR my positions are not updating somehow
In your main loop, when setting the drop rectangles, use the loop variable instead of the drop count:
for raindrop_number in range(raindrop_number_x):
raindrop = Raindrop(screen)
raindrop.rect.x = raindrop.rect.x + 2 * raindrop.rect.x * raindrop_number # use loop variable
raindrops.add(raindrop)

Enemies won't display

I'm trying to create a top down shooter, but my enemies won't spawn in. I'm trying to have the enemy move in a diagonal pattern but also be able to bounce off of the wall kind of like a top down bouncing ball. I'm also trying to have a new enemy spawn every 10 seconds, but when I run the program nothing shows up, there is no error either. Can someone please help.
import pygame
pygame.init()
import random
import time
inPlay = True
width=900
height=700
screen = pygame.display.set_mode((width, height))
#enemy
enemyFrequency=10
enemyPause=enemyFrequency
killEnemy=True
#------------------------------#
# classes #
#------------------------------#
######################################################
class Enemy(pygame.sprite.Group):
def __init__(self,ballX,ballY,ballSpeed,picture2=None):
pygame.sprite.Group.__init__(self)
self.ballX=ballX
self.ballY=ballY
self.ballSpeed=ballSpeed
self.image2=pygame.image.load(picture2)
def move(self):
for enemys in self:
self.ballX+=self.ballSpeed
self.ballY+=self.ballSpeed
def decay(self):
for enemys in self:
if enemys.y > height:
self.remove(enemys)
#------------------------------#
# functions #
#------------------------------#
def redraw_game_window():
screen.fill((30, 30, 30))
enemys.draw(screen)
pygame.display.update()
#------------------------------#
# main program #
#------------------------------#
RB=width-player.rect.width
CEILING = 3*player.rect.height # for ship movement
FLOOR = height - player.rect.height #
#enemy
enemies=pygame.sprite.Group()
enemySpeed=3
eX=random.randint(0,RB)
eY=random.randint(-height,0)
enemys=Enemy (eX,eY,enemySpeed,'asteroid.png')
t0 = pygame.time.clock()
dt = 0
enemyCount = 0
clock = pygame.time.Clock()
while inPlay:
redraw_game_window()
for event in pygame.event.get():
if event.type == pygame.QUIT:
inPlay=False
#enemies spawning
if dt < nSeconds:
t1 = time.process_time()
dt = t1 - t0
enemies.add(enemys)
else:
enemyInstance = Enemy()
enemyCount += 1
t0 = time.clock()
dt = 0
enemys.move()
enemys.decay()
clock.tick(30)
pygame.quit()
I hacked your code to the point where it seemed to be doing what the code describes, and what matches your question. There were a lot of things missing in this code, maybe you cut them out before posting to SO to make the code shorter.
The crux of the problem is the handling of the enemy sprites. I don't know if it was by design, but the original code was defining what seemed like a sprite, but based on a sprite group.
So I modified the code based on the idea that enemies should be a global sprite group, and went from there "fixing" things. There was no Player class defined, I added one. I could not follow the timing code, it was using pygame.time.clock() (which is an object) as a time value. It seemed like this code was keeping a delta, and counting time to respawn a new enemy, I had to re-write this bit to get it to work - my apologies.
The other comment I want to make - sprites can define an update() function. This function should handle the position changes and appearance of the sprite. PyGame sprite groups will handle the calling of this function automatically (if it's defined) in the sprite group update() meta-call. This gives the program a clean and simple way to handle sprite animation.
import pygame
pygame.init()
import random
import time
inPlay = True
width=900
height=700
screen = pygame.display.set_mode((width, height))
#enemy
enemies = pygame.sprite.Group() # Holds all enemy sprites
enemyFrequency = 1000 # milliseconds between enemy spawn
killEnemy = True
enemyCount = 0
#------------------------------#
# classes #
#------------------------------#
######################################################
class Player(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load('player.png').convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = (100,100) # TODO - position properly
def update(self):
# TODO
pass
class Enemy(pygame.sprite.Sprite):
def __init__(self, ballX, ballY, ballSpeed, picture2=None):
pygame.sprite.Sprite.__init__(self)
self.ballSpeed = ballSpeed
self.image = pygame.image.load(picture2).convert_alpha()
self.rect = self.image.get_rect()
self.rect.center = ( ballX, ballY )
def update(self):
global enemies # group of all enemy sprites (to which this sprite belongs)
global height # window height
self.rect.x += self.ballSpeed
self.rect.y += self.ballSpeed
# decay the enemy
if ( self.rect.y > height ):
enemies.remove(self) # went off screen, delete it
#------------------------------#
# functions #
#------------------------------#
def redraw_game_window():
screen.fill((30, 30, 30))
enemies.draw(screen)
pygame.display.update()
#------------------------------#
# main program #
#------------------------------#
player = Player()
RB=width-player.rect.width
CEILING = 3*player.rect.height # for ship movement
FLOOR = height - player.rect.height #
# start with 3 enemies
for i in range( 3 ):
enemySpeed = 3
eX=random.randint(0,RB)
eY=random.randint(-height,0)
enemies.add( Enemy(eX,eY,enemySpeed,'asteroid.png') )
clock = pygame.time.Clock()
last_enemy_spawn_time = 0
while inPlay:
time_now = pygame.time.get_ticks()
for event in pygame.event.get():
if event.type == pygame.QUIT:
inPlay=False
# is it time to spawn a new enemy?
if ( time_now - last_enemy_spawn_time > enemyFrequency ):
last_enemy_spawn_time = time_now # reset timer
#enemies spawning
eX=random.randint(0,RB)
eY=random.randint(-height,0)
enemies.add( Enemy(eX,eY,enemySpeed,'asteroid.png') )
enemyCount += 1
enemies.update() # call the update() of every sprite
redraw_game_window()
#enemies.decay() -- MOVED INTO Enemy.update()
clock.tick(30)
pygame.quit()

How to get enemy to appear?

When the game first starts both the player and the enemy appear but when the player moves the enemy disappears.
import pygame
import os
import random
from pygame.locals import * # Constants
import math
import sys
import random
pygame.init()
screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255)) # fill the background white
White = (255,255,255)
screen.blit(background, (0, 0))
class Player(pygame.sprite.Sprite):
def __init__(self):
self.rect = pygame.draw.rect(screen, (0,0,128), (50,560,50,25)) #(colour)(x-position,y-position,width,height)
self.dist = 100
def draw_rect(self,x,y): # This is my code which should make the player move
screen.blit(background, (0, 0)) #If this isn't included then when the rectangle moves it's old positon will still be on the screen
self.rect = self.rect.move(x*self.dist, y*self.dist); pygame.draw.rect(screen, (0, 0, 128), self.rect)
pygame.display.update()
def handle_keys(self): # code to make the character move when the arrow keys are pressed
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.draw_rect(-0.05,0)
elif keys[K_RIGHT]:
self.draw_rect(0.05,0)
elif keys[K_UP]:
self.draw_rect(0,-0.05)
elif keys[K_DOWN]:
self.draw_rect(0,0.05)
elif keys[K_SPACE]:
self.draw_rect(0.4,-0.9)
if self.rect.right > 1280: # These are to stop the player from going out of the boundary
self.rect.right = 1280
if self.rect.left < 0:
self.rect.left = 0
if self.rect.bottom > 720:
self.rect.bottom = 720
if self.rect.top < 0:
self.rect.top = 0
The movement of the player works perfectly fine.
class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
x = random.randint(50,450)
self.rect = pygame.draw.rect(screen, (128,0,0), (300,x,50,25))
The enemy just spawns at a random place and should still appear when the player moves around. I tried to do something but it didn't work.
clock = pygame.time.Clock() # A clock to limit the frame rate.
player = Player()
enemy = Enemy()
def main(): #my main loop
running = True
while running:
player.handle_keys()
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
clock.tick(60) # Limit the frame rate to 60 FPS.
pygame.display.flip() #updates the whole screen
if __name__ == '__main__': main()
It's because you draw the Rect of the enemy only once: in the __init__ function of Enemy. Also, you do a lot of drawing in different places of the Player class, like clearing the screen. Stop doing that.
You already subclass Sprite, so use this class as intended. Sprites have an image and rect that defines how they look like and where they are, and you add them to a Group that takes care of calling their update function and drawing them to the screen.
Your code should look like this:
import pygame
import random
from pygame.locals import * # Constants
pygame.init()
screen=pygame.display.set_mode((1280,720)) #(length,height)
screen_rect=screen.get_rect()
background = pygame.Surface(screen.get_size())
background.fill((255,255,255)) # fill the background white
class Player(pygame.sprite.Sprite):
def __init__(self):
super().__init__()
self.image = pygame.Surface((50,25))
self.image.fill((0,0,128))
self.rect = self.image.get_rect(topleft=(50,560))
self.dist = 100
def update(self): # code to make the character move when the arrow keys are pressed
keys = pygame.key.get_pressed()
if keys[K_LEFT]:
self.rect.move_ip(-1,0)
elif keys[K_RIGHT]:
self.rect.move_ip(1,0)
elif keys[K_UP]:
self.rect.move_ip(0,-1)
elif keys[K_DOWN]:
self.rect.move_ip(0,1)
self.rect.clamp_ip(screen_rect)
class Enemy(pygame.sprite.Sprite): # the enemy class which works fine
def __init__(self):
super().__init__()
x = random.randint(50,450)
self.image = pygame.Surface((50,25))
self.image.fill((128,0,0))
self.rect = self.image.get_rect(topleft=(300, x))
clock = pygame.time.Clock() # A clock to limit the frame rate.
player = Player()
enemy = Enemy()
sprites = pygame.sprite.Group(player, enemy)
def main(): #my main loop
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
sprites.update()
screen.blit(background, (0, 0))
sprites.draw(screen)
clock.tick(60) # Limit the frame rate to 60 FPS.
pygame.display.flip() #updates the whole screen
if __name__ == '__main__':
main()

Pygame - sprite movement causes layers

Im trying to make a rain effect with pygame but it seems as if the background is not cleaning up before updating the sprites.
this is what it looks like when i execute the code..
I wonder if there's a way to fix this problem.
rain.py (main file)
#!/usr/bin/python
VERSION = "0.1"
import os, sys, raindrop
from os import path
try:
import pygame
from pygame.locals import *
except ImportError, err:
print 'Could not load module %s' % (err)
sys.exit(2)
# main variables
WIDTH, HEIGHT, FPS = 300, 300, 30
# initialize game
pygame.init()
screen = pygame.display.set_mode((WIDTH,HEIGHT))
pygame.display.set_caption("Rain and Rain")
# background
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((40,44,52))
# blitting
screen.blit(background,(0,0))
pygame.display.flip()
# clock for FPS settings
clock = pygame.time.Clock()
def main():
raindrops = pygame.sprite.Group()
# a function to create new drops
def newDrop():
nd = raindrop.Raindrop()
raindrops.add(nd)
# creating 10 rain drops
for x in range(0,9): newDrop()
# variable for main loop
running = True
# event loop
while running:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
raindrops.update()
screen.blit(background,(100,100))
raindrops.draw(screen)
pygame.display.flip()
pygame.quit()
if __name__ == '__main__': main()
raindrop.py ( class for raindrops )
import pygame
from pygame.locals import *
from os import path
from random import randint
from rain import HEIGHT
img_dir = path.join(path.dirname(__file__), 'img')
class Raindrop(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.width = randint(32, 64)
self.height = self.width + 33
self.image = pygame.image.load(path.join(img_dir, "raindrop.png")).convert_alpha()
self.image = pygame.transform.scale(self.image, (self.width, self.height))
self.speedy = 5 #randint(1, 8)
self.rect = self.image.get_rect()
self.rect.x = randint(0, 290)
self.rect.y = -self.height
def update(self):
self.rect.y += self.speedy
if self.rect.y == HEIGHT:
self.rect.y = -self.height
self.rect.x = randint(0, 290)
This is the line you use to clear the screen:
screen.blit(background, (100, 100))
In other words; you're clearing the screen starting at x=100, y=100. Since pygame coordinates starts from topleft and extends to the right and downwards, you're not clearing the screen left of x=100 and above y=100.
Simple fix is blitting at 0, 0 as you did at the start of the program.
screen.blit(background, (0, 0))

How to add a Collision in Pygame

i'm making a game about a car trying not to collide with pedestrian car.
I'm trying to add a collision to the user_car(aka Player class) with enemy(aka pedestrian_cars class), but i'm not exactly sure where(while loop?) and how to do it. Some variables maybe be bad but I will fix them later.
My program:
import pygame, random, math, sys
from pygame.locals import *
class Player(pygame.sprite.Sprite):
def __init__(self, starty):
pygame.sprite.Sprite.__init__(self)
# Images
self.aliveImage = pygame.image.load("playercar.png").convert_alpha()
#self.deadImage = pygame.image.load("data/PlayerExplode.png").convert_alpha()
self.image = self.aliveImage
self.rect = self.image.get_rect()
self.rect.x = 200
self.rect.y = starty - self.rect.height
self.speed = 7
self.dead = False
# Explode if you get hit, lose a life
def explode(self):
if not self.dead:
self.dead = True
self.image = self.deadImage
pygame.mixer.stop()
self.channel = self.explodeSound.play()
game.playerShots.empty()
game.enemyShots.empty()
game.wave.mship.empty()
game.lives.update(-1)
class pedestrian_cars(pygame.sprite.Sprite):
def __init__(self, starty,startx):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load("pedcar.png").convert_alpha()
self.rect = self.image.get_rect()
self.rect.y = starty - self.rect.height
self.rect.x = startx - self.rect.width
self.delta_y = 5 # 5
self.gravity = .5 #.5
self.has_spawned = False
def update(self):
self.rect.y += self.delta_y
def spawn(self):
if self.rect.y == 480 or self.has_spawned == False:
self.has_spawned = True
self.rect.x = random.randint(60,300)
self.rect.y = -10
def main():
""" Set up the game and run the main game loop """
pygame.mixer.pre_init(44100, -16, 2, 2048)
pygame.init() # prepare the pygame module for use
surfaceSz = 480 # Desired physical surface size, in pixels.
# Create surface of (width, height), and its window.
main_surface = pygame.display.set_mode((surfaceSz, surfaceSz))
#SPRITES###############################################################
user_car = Player(450)
enemy = pedestrian_cars(10,200)
#SPRITES################################################################
background_image = pygame.image.load("background2.png")
all_sprites = pygame.sprite.Group()
user_car.add(all_sprites)
enemy.add(all_sprites)
clock = pygame.time.Clock()
b1 = "background2.png"
back = pygame.image.load(b1).convert()
back2 = pygame.image.load(b1).convert()
y = 0
screenWidth = 600
screenHeight = 480
#Sound/Music#####################################
pygame.mixer.music.load("stilldre.wav")
pygame.mixer.music.play(-1)
#-################################################
while True:
ev = pygame.event.poll() # look for any event
if ev.type == pygame.QUIT: # window close button clicked?
break # ... leave game loop
sys.exit()
if not user_car.dead:
# Move the player
keys = pygame.key.get_pressed()
if keys[pygame.K_LEFT]:
user_car.rect.x = max(user_car.rect.x - user_car.speed, 116-user_car.rect.width)
elif keys[pygame.K_RIGHT]:
user_car.rect.x = min(user_car.rect.x + user_car.speed, 395-user_car.rect.width)
else:
# Go back to playing after the explosion sound finishes
if not self.channel.get_busy():
self.image = self.aliveImage
self.dead = False
self.rect.x = 200
# Update your game objects and data structures here...
all_sprites.update()
enemy.spawn()
main_surface.fill((0,200,255))
main_surface.blit(background_image, (0, 0))
main_surface.blit(back, (0,y))
main_surface.blit(back2,(0,y-screenHeight))
y = y + 8
if y == screenWidth:
y = 0
## if enemy.alive.x ==
## msElapsed = clock.tick(100)
## pygame.display.flip()
all_sprites.draw(main_surface)
# Now the surface is ready, tell pygame to display it!
pygame.display.flip()
clock.tick(200)
msElapsed = clock.tick(100)
pygame.quit() # once we leave the loop, close the window.
main()
You can simply check if the rects of your objects overlap with colliderect:
while True:
...
if user_car.rect.colliderect(enemy.rect):
do_something()
...

Categories