I´m creating simple game where I play as aeroplane and i´m dodging rockets (rockets appear from right side), I did something like barrier which block enemies and kill them, but it´s too OP, so I want to nerf it, so I´m going to spawn enemies from both, right side, and top side.
But I really don´t know how to spawn them from top.
I would really appreciate any help, thanks.
P.S. class named Enemy should spawn enemies from right (this is working correctly)
class AnotherEnemy should spawn enemies from top (this is not working correctly)
My code:
from pygame.locals import(
RLEACCEL,
K_LEFT,
K_DOWN,
K_UP,
K_RIGHT,
K_ESCAPE,
K_SPACE,
K_w,
K_a,
K_s,
K_d,
KEYDOWN,
QUIT,
)
SCREEN_WIDTH = 700
SCREEN_HEIGHT = 700
class Player(pygame.sprite.Sprite):
def __init__(self):
super(Player, self).__init__()
self.surf = pygame.image.load('C:\Sprites\jet.png').convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect()
def update(self, pressed_keys):
if pressed_keys[K_w]:
self.rect.move_ip(0, -8)
if pressed_keys[K_s]:
self.rect.move_ip(0, 8)
if pressed_keys[K_a]:
self.rect.move_ip(-8, 0)
if pressed_keys[K_d]:
self.rect.move_ip(8, 0)
if pressed_keys[K_UP]:
self.rect.move_ip(0, -8)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 8)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-8, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(8, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
self.surf = pygame.image.load('C:\Sprites\missile.png').convert()
self.surf.set_colorkey((255, 255, 255), RLEACCEL)
self.rect = self.surf.get_rect(
center =(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(5, 17)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
self.kill()
class AnotherEnemy(pygame.sprite.Sprite):
def __init__(self):
super(AnotherEnemy, self).__init__()
self.surf = pygame.image.load('C:\Sprites\missile2.png').convert()
self.surf.set_colorkey((255, 255, 255))
self.rect = self.surf.get_rect(
center =(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT)
)
)
self.speed = random.randint(8, 17)
def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.bottom > SCREEN_HEIGHT:
self.kill()
class Cloud(pygame.sprite.Sprite):
def __init__(self):
super(Cloud, self).__init__()
self.surf = pygame.image.load('C:\Sprites\cloud.png').convert()
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
self.rect = self.surf.get_rect(
center =(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
def update(self):
self.rect.move_ip(-6, 0)
if self.rect.right < 0:
self.kill()
class Sun(pygame.sprite.Sprite):
def __init__(self):
super(Sun, self).__init__()
self.surf = pygame.image.load('C:\Sprites\sunnn.png').convert()
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
self.rect = self.surf.get_rect()
class Ballon(pygame.sprite.Sprite):
def __init__(self):
super(Ballon, self).__init__()
self.surf = pygame.image.load('C:\Sprites\gula.png').convert()
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
self.rect = self.surf.get_rect(
center =(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
def update(self):
self.rect.move_ip(-6, 0)
if self.rect.right < 0:
self.kill()
class Barrier(pygame.sprite.Sprite):
def __init__(self):
super(Barrier, self).__init__()
self.surf = pygame.image.load('C:\Sprites\shield.png').convert()
self.surf.set_colorkey((0, 0, 0), RLEACCEL)
self.rect = self.surf.get_rect()
def update(self):
if pressed_keys[K_w]:
self.rect.move_ip(0, -8)
if pressed_keys[K_s]:
self.rect.move_ip(0, 8)
if pressed_keys[K_a]:
self.rect.move_ip(-8 , 0)
if pressed_keys[K_d]:
self.rect.move_ip(8 , 0)
pygame.init()
pygame.display.set_caption('Simple game')
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 250)
ADDANOTHERENEMY = pygame.USEREVENT + 4
pygame.time.set_timer(ADDANOTHERENEMY, 250)
ADDCLOUD = pygame.USEREVENT + 2
pygame.time.set_timer(ADDCLOUD, 3100)
ADDBALLON = pygame.USEREVENT + 3
pygame.time.set_timer(ADDBALLON, 4500)
enemy = Enemy()
anotherenemy = AnotherEnemy()
player = Player()
barrier = Barrier()
sun = Sun()
ballon = Ballon()
enemies = pygame.sprite.Group()
anotherenemies = pygame.sprite.Group()
clouds = pygame.sprite.Group()
ballons = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
all_sprites.add(sun)
all_sprites.add(enemy)
all_sprites.add(anotherenemy)
Time = 0
clock = pygame.time.Clock()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.USEREVENT:
Time += 0.25
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.key == K_SPACE:
all_sprites.add(barrier)
elif event.type == QUIT:
running = False
elif event.type == ADDENEMY:
new_enemy = Enemy()
enemies.add(new_enemy)
all_sprites.add(new_enemy)
elif event.type == ADDANOTHERENEMY:
new_anotherenemy = AnotherEnemy()
anotherenemies.add(new_anotherenemy)
all_sprites.add(new_anotherenemy)
elif event.type == ADDCLOUD:
new_cloud = Cloud()
clouds.add(new_cloud)
all_sprites.add(new_cloud)
elif event.type == ADDBALLON:
new_ballon = Ballon()
ballons.add(new_ballon)
all_sprites.add(new_ballon)
elif Time < -10000000:
running = False
clock.tick(30)
screen.blit(player.surf, player.rect)
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
anotherenemies.update()
enemies.update()
clouds.update()
ballons.update()
barrier.update()
screen.fill((135, 206, 250))
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
pygame.display.flip()
if pygame.sprite.spritecollideany(player, enemies):
player.kill()
running = False
collided_enemy = pygame.sprite.spritecollideany(barrier, enemies)
if collided_enemy != None:
collided_enemy.kill()
Time -= 25
collided_anotherenemy = pygame.sprite.spritecollideany(barrier, anotherenemies)
if collided_anotherenemy != None:
collided_anotherenemy.kill()
counter, text = str(Time), ' Score: ' + str(Time)
pygame.time.set_timer(pygame.USEREVENT, 1000)
font = pygame.font.SysFont('Consolas', 30)
screen.blit(font.render(text, True, (0, 0, 0)), (32, 48))
pygame.display.flip()
pygame.quit()
You just have to adjust your position and speed logic to have the movement and variable part of the position be in the y component. Because the top of the screen is 0, you have to start at a negative y value and move in positive steps instead of negative. You change the kill check to kill the enemy when it goes off the bottom of the screen (> screen height). Here's an enemy definition that comes from the top of the screen and moves down:
class AnotherEnemy(pygame.sprite.Sprite):
def __init__(self):
super(AnotherEnemy, self).__init__()
self.surf = pygame.image.load(image3).convert()
self.surf.set_colorkey((255, 255, 255))
self.rect = self.surf.get_rect(
center=(
random.randint(0, SCREEN_WIDTH),
-100
)
)
self.speed = random.randint(8, 17)
def update(self):
self.rect.move_ip(0, self.speed)
if self.rect.top > SCREEN_HEIGHT:
self.kill()
Related
My partner and I have been working on this Tron game, but we have gotten stuck on add a trail behind the 2 squares. We have looked through other forms and haven't gotten and good information, if anyone can look at this code and help up get a trail it will be greatly appreciated.
We are making a Tron game. If one of the opponents run into their own or oppositions trail they die and the other wins.
import pygame
from pygame.locals import (K_i, K_k, K_j, K_l, K_ESCAPE, K_w, K_a, K_d, K_s, KEYDOWN, QUIT,)
p1_movement = 0
p2_movement = 0
pygame.init()
BLACK = (0, 0, 0)
green = (0,255,0)
WIDTHTWO = 1
HEIGHTTWO = 1000
WIDTHONE = 2000
HEIGHTONE = 1000
pygame.display.set_caption("Tron")
screen = pygame.display.set_mode((1000,1000))
clock = pygame.time.Clock()
clock.tick(60)
screen.fill((0,0,0))
class PlayerOne(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerOne, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((255, 0, 0))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHONE / 2, HEIGHTONE / 2)
def update(self, pressed_keys):
if pressed_keys[K_i]:
self.rect.move_ip(0, -1)
if pressed_keys[K_k]:
self.rect.move_ip(0, 1)
if pressed_keys[K_j]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_l]:
self.rect.move_ip(1, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
class PlayerTwo(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerTwo, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((0, 255, 255))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHTWO / 2, HEIGHTTWO / 2)
def update(self, pressed_keys):
if pressed_keys[K_w]:
self.rect.move_ip(0, -1)
if pressed_keys[K_s]:
self.rect.move_ip(0, 1)
if pressed_keys[K_a]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_d]:
self.rect.move_ip(1, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
player = PlayerOne()
playertwo = PlayerTwo()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
playertwo.update(pressed_keys)
screen.fill((0, 0, 0))
screen.blit(player.surf, player.rect)
screen.blit(playertwo.surf, playertwo.rect)
pygame.display.flip()
pygame.quit()
This is a job for pygame.draw.lines.
This method will take in a list of coordinates, and connect the dots.
How would we construct the list of coordinates?
Simply as self.trail = [] to the __init__ function to store the coordinates,
and append the coordinates to the list in the update() functions.
So in the update() functions, add self.trail.append(self.rect.center):
pos = self.rect.center
if self.trail:
if self.trail[-1] != pos:
self.trail.append(pos)
else:
self.trail = [pos, pos]
and in the main game loop, add the pygame.draw.lines parts.
Full working code:
from pygame.locals import (K_i, K_k, K_j, K_l, K_ESCAPE, K_w, K_a, K_d, K_s, KEYDOWN, QUIT,)
import pygame
p1_movement = 0
p2_movement = 0
pygame.init()
BLACK = (0, 0, 0)
green = (0,255,0)
WIDTHTWO = 1
HEIGHTTWO = 1000
WIDTHONE = 2000
HEIGHTONE = 1000
pygame.display.set_caption("Tron")
screen = pygame.display.set_mode((1000,1000))
clock = pygame.time.Clock()
clock.tick(60)
screen.fill((0,0,0))
class PlayerOne(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerOne, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((255, 0, 0))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHONE / 2, HEIGHTONE / 2)
self.trail = []
def update(self, pressed_keys):
if pressed_keys[K_i]:
self.rect.move_ip(0, -1)
if pressed_keys[K_k]:
self.rect.move_ip(0, 1)
if pressed_keys[K_j]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_l]:
self.rect.move_ip(1, 0)
pos = self.rect.center
if self.trail:
if self.trail[-1] != pos:
self.trail.append(pos)
else:
self.trail = [pos, pos]
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
class PlayerTwo(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerTwo, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((0, 255, 255))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHTWO / 2, HEIGHTTWO / 2)
self.trail = set()
def update(self, pressed_keys):
if pressed_keys[K_w]:
self.rect.move_ip(0, -1)
if pressed_keys[K_s]:
self.rect.move_ip(0, 1)
if pressed_keys[K_a]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_d]:
self.rect.move_ip(1, 0)
pos = self.rect.center
if self.trail:
if self.trail[-1] != pos:
self.trail.append(pos)
else:
self.trail = [pos, pos]
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
player = PlayerOne()
playertwo = PlayerTwo()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
playertwo.update(pressed_keys)
screen.fill((0, 0, 0))
pygame.draw.lines(screen, (255, 255, 255), False, player.trail)
pygame.draw.lines(screen, (255, 255, 255), False, playertwo.trail)
screen.blit(player.surf, player.rect)
screen.blit(playertwo.surf, playertwo.rect)
pygame.display.flip()
pygame.quit()
Output:
Add an attribute rect_list to the class PalyereOne and append a copy of the current rectangle to the list when the player is moved:
class PlayerOne(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
# [...]
self.rect_list = []
def update(self, pressed_keys):
self.rect_list.append(self.rect.copy())
# [...]
Draw the list of rectangles in the main application loop:
while running:
# [...]
screen.fill((0, 0, 0))
for rect in player.rect_list:
screen.blit(player.surf, rect)
# [...]
Do the same for PlayerTwo.
My game is almost complete! Unfortunately, there are some little details I need to fix. The main one is that after you play for a bit, some of the sprites will get stuck on the front of the screen. Can anybody please help me out.
import pygame, random
from pygame.locals import (
K_UP,
K_DOWN,
K_LEFT,
K_RIGHT,
KEYDOWN,
QUIT,
)
print('Use the window above to play')
pygame.init()
pygame.font.init()
clock = pygame.time.Clock()
score = 0
display_width = 800
display_height = 600
X = 400
Y = 400
SCREEN_WIDTH = display_width
SCREEN_HEIGHT = display_height
screen = pygame.display.set_mode((display_width,display_height))
def death():
global alive
print('You died')
myfont = pygame.font.SysFont('Comic Sans MS', 100)
textsurface = myfont.render('You Died.', False, (0, 0, 0))
screen.blit(textsurface,(0,0))
class Player(pygame.sprite.Sprite):
def __init__(self, x, y):
super(Player, self).__init__()
self.surf = pygame.image.load("Plane.png").convert()
self.rect = self.surf.get_rect(topleft = (x, y))
def update(self, pressed_keys):
if pressed_keys[K_UP]:
self.rect.move_ip(0, -5)
if pressed_keys[K_DOWN]:
self.rect.move_ip(0, 5)
if pressed_keys[K_LEFT]:
self.rect.move_ip(-5, 0)
if pressed_keys[K_RIGHT]:
self.rect.move_ip(5, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > SCREEN_WIDTH:
self.rect.right = SCREEN_WIDTH
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= SCREEN_HEIGHT:
self.rect.bottom = SCREEN_HEIGHT
class Enemy(pygame.sprite.Sprite):
def __init__(self):
super(Enemy, self).__init__()
self.surf = pygame.Surface((20, 10))
self.surf = pygame.image.load("Missile.png").convert()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(5, 10)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 1
self.kill()
class Bomb(pygame.sprite.Sprite):
def __init__(self):
super(Bomb, self).__init__()
self.surf = pygame.Surface((50, 25))
self.surf = pygame.image.load("Bomber.png").convert()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(2, 5)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 2
self.kill()
class Blimp(pygame.sprite.Sprite):
def __init__(self):
super(Blimp, self).__init__()
self.surf = pygame.Surface((75, 35))
self.surf = pygame.image.load("Blimp.png").convert()
self.rect = self.surf.get_rect(
center=(
random.randint(SCREEN_WIDTH + 20, SCREEN_WIDTH + 100),
random.randint(0, SCREEN_HEIGHT),
)
)
self.speed = random.randint(1, 3)
def update(self):
self.rect.move_ip(-self.speed, 0)
if self.rect.right < 0:
global score
score += 3
self.kill()
ADDENEMY = pygame.USEREVENT + 1
pygame.time.set_timer(ADDENEMY, 950)
player = Player(display_width // 2, display_height // 2)
enemies = pygame.sprite.Group()
bomb = pygame.sprite.Group()
blimp = pygame.sprite.Group()
all_sprites = pygame.sprite.Group()
all_sprites.add(player)
alive = True
while alive:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
alive = False
elif event.type == ADDENEMY:
new_enemy = Enemy()
new_bomb = Bomb()
new_blimp = Blimp()
spawn = random.randint(1,3)
if spawn == 1:
enemies.add(new_enemy)
if spawn == 2:
enemies.add(new_blimp)
if spawn == 3:
enemies.add(new_bomb)
all_sprites.add(new_enemy)
all_sprites.add(new_bomb)
all_sprites.add(new_blimp)
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
enemies.update()
screen.fill((50,50,160))
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
if pygame.sprite.spritecollideany(player, enemies):
player.kill()
alive = False
death()
screen.blit(player.surf, player.rect)
pygame.display.flip()
pygame.display.update()
clock.tick(60)
Press the arrow keys to move. The text box (not in the new window) will tell you your score.
You update all the enemies which are on the Group enemies
enemies.update()
And you draw all the Sprites in the group all_sprites.
for entity in all_sprites:
screen.blit(entity.surf, entity.rect)
But not all enemies which are in the Group all_sprites are in the Group enemies, too:
new_enemy = Enemy()
new_bomb = Bomb()
new_blimp = Blimp()
spawn = random.randint(1,3)
if spawn == 1:
enemies.add(new_enemy)
if spawn == 2:
enemies.add(new_blimp)
if spawn == 3:
enemies.add(new_bomb)
all_sprites.add(new_enemy)
all_sprites.add(new_bomb)
all_sprites.add(new_blimp)
All the enemies which are in the Group all_sprites, but not in the Group enemies are drawn but don't move.
Create a random enemy type and add it to both Gourps:
alive = True
while alive:
for event in pygame.event.get():
print(event)
if event.type == pygame.QUIT:
alive = False
elif event.type == ADDENEMY:
new_opponent = random.choice([Enemy(), Bomb(), Blimp()])
enemies.add(new_opponent)
all_sprites.add(new_opponent)
This question already has an answer here:
pygame.sprite.groupcollide() does not work when trying to implement collision in pygame [duplicate]
(1 answer)
Closed 2 years ago.
I made an enemy class and a player class for a pygame game and I'm trying to figure out how to detect collisions between them. I've tried a lot of different methods, and they don't work, because they need you to use a Rect object and I use a Surface object. Can you tell me what code to add to detect collisions between to Surface Objects? Here is my code:
import pygame
from pygame.locals import *
from sys import exit
import time
import random
pygame.init()
SCREEN_SIZE = (800, 600)
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
background = pygame.Surface(screen.get_size())
background = background.convert()
background.fill((250, 250, 250))
screen.blit(background, (0, 0))
class Box(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self._1 = random.randint(10, 50)
self.image = pygame.Surface((self._1, self._1))
self.image.fill((random.randint(1, 255), random.randint(1, 255), random.randint(1, 255)))
self.rect = self.image.get_rect()
self.dx = random.randint(5, 15)
self.dy = random.randint(5, 15)
self.direction = self.dx, self.dy
def update(self):
pass
class Player(Box):
def __init__(self):
Box.__init__(self)
self.x = 400
self.y = 300
self.image = pygame.Surface((30, 30))
self.image.fill((0, 255, 0))
screen.fill((255, 255, 255))
self.rect.centerx = 400
self.rect.centery = 300
def update(self):
self.rect.centerx = self.x
self.rect.centery = self.y
class Enemy(Box):
def __init__(self):
Box.__init__(self)
self.image = pygame.Surface((30, 30))
self.image.fill((0, 255, 0))
self.image.fill((255, 0, 0))
def update(self):
self.dx, self.dy = self.direction
self.rect.centerx += self.dx
self.rect.centery += self.dy
if self.rect[0] >= 800 or self.rect[0] + self._1 <= 0:
self.direction = -self.dx, self.dy
if self.rect[1] >= 600 or self._1 + self.rect[1] <= 0:
self.direction = self.dx, -self.dy
allSprites = pygame.sprite.Group()
player = Player()
enemy1 = Enemy()
enemy2 = Enemy()
enemy3 = Enemy()
allSprites.add(enemy1)
allSprites.add(enemy2)
allSprites.add(enemy3)
allSprites.add(player)
while True:
for event in pygame.event.get():
if event.type == QUIT:
pygame.quit()
exit()
if event.type == KEYDOWN:
if event.key == K_LEFT:
player.x -= 10
if event.key == K_RIGHT:
player.x += 10
if event.key == K_UP:
player.y -= 10
if event.key == K_DOWN:
player.y += 10
screen.fill((255, 255, 255))
allSprites.clear(screen, background)
allSprites.update()
allSprites.draw(screen)
pygame.display.flip()
time.sleep(50.0 / 1000.0)
Add the enemies to a pygame.sprite.Group (enemies) and detect if the Sprite player collides with an element from the Group enemies by pygame.sprite.spritecollide(). e.g.:
player = Player()
enemy1 = Enemy()
enemy2 = Enemy()
enemy3 = Enemy()
allSprites = pygame.sprite.Group()
allSprites.add([enemy1, enemy2, enemy3, player])
enemies = pygame.sprite.Group()
enemies.add([enemy1, enemy2, enemy3])
while True:
# [...]
allSprites.update()
if pygame.sprite.spritecollide(player, enemies, False):
print("collide")
# [...]
But note the .rect attributes of the objects Player and Enemy have just a size of 1x1. Ensure that the rectangles have the proper size:
class Player(Box):
def __init__(self):
Box.__init__(self)
self.x = 400
self.y = 300
self.image = pygame.Surface((30, 30))
self.image.fill((0, 255, 0))
screen.fill((255, 255, 255))
self.rect = self.image.get_rect(center=(400, 300)) # <---
class Enemy(Box):
def __init__(self):
Box.__init__(self)
self.image = pygame.Surface((30, 30))
self.rect = self.image.get_rect() # <---
self.image.fill((0, 255, 0))
self.image.fill((255, 0, 0))
My partner and I have been working on this Tron game, but we have gotten stuck on add a trail behind the 2 squares. We have looked through other forms and haven't gotten and good information, if anyone can look at this code and help up get a trail it will be greatly appreciated.
We are making a Tron game. If one of the opponents run into their own or oppositions trail they die and the other wins.
import pygame
from pygame.locals import (K_i, K_k, K_j, K_l, K_ESCAPE, K_w, K_a, K_d, K_s, KEYDOWN, QUIT,)
p1_movement = 0
p2_movement = 0
pygame.init()
BLACK = (0, 0, 0)
green = (0,255,0)
WIDTHTWO = 1
HEIGHTTWO = 1000
WIDTHONE = 2000
HEIGHTONE = 1000
pygame.display.set_caption("Tron")
screen = pygame.display.set_mode((1000,1000))
clock = pygame.time.Clock()
clock.tick(60)
screen.fill((0,0,0))
class PlayerOne(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerOne, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((255, 0, 0))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHONE / 2, HEIGHTONE / 2)
def update(self, pressed_keys):
if pressed_keys[K_i]:
self.rect.move_ip(0, -1)
if pressed_keys[K_k]:
self.rect.move_ip(0, 1)
if pressed_keys[K_j]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_l]:
self.rect.move_ip(1, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
class PlayerTwo(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerTwo, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((0, 255, 255))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHTWO / 2, HEIGHTTWO / 2)
def update(self, pressed_keys):
if pressed_keys[K_w]:
self.rect.move_ip(0, -1)
if pressed_keys[K_s]:
self.rect.move_ip(0, 1)
if pressed_keys[K_a]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_d]:
self.rect.move_ip(1, 0)
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
player = PlayerOne()
playertwo = PlayerTwo()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
playertwo.update(pressed_keys)
screen.fill((0, 0, 0))
screen.blit(player.surf, player.rect)
screen.blit(playertwo.surf, playertwo.rect)
pygame.display.flip()
pygame.quit()
This is a job for pygame.draw.lines.
This method will take in a list of coordinates, and connect the dots.
How would we construct the list of coordinates?
Simply as self.trail = [] to the __init__ function to store the coordinates,
and append the coordinates to the list in the update() functions.
So in the update() functions, add self.trail.append(self.rect.center):
pos = self.rect.center
if self.trail:
if self.trail[-1] != pos:
self.trail.append(pos)
else:
self.trail = [pos, pos]
and in the main game loop, add the pygame.draw.lines parts.
Full working code:
from pygame.locals import (K_i, K_k, K_j, K_l, K_ESCAPE, K_w, K_a, K_d, K_s, KEYDOWN, QUIT,)
import pygame
p1_movement = 0
p2_movement = 0
pygame.init()
BLACK = (0, 0, 0)
green = (0,255,0)
WIDTHTWO = 1
HEIGHTTWO = 1000
WIDTHONE = 2000
HEIGHTONE = 1000
pygame.display.set_caption("Tron")
screen = pygame.display.set_mode((1000,1000))
clock = pygame.time.Clock()
clock.tick(60)
screen.fill((0,0,0))
class PlayerOne(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerOne, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((255, 0, 0))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHONE / 2, HEIGHTONE / 2)
self.trail = []
def update(self, pressed_keys):
if pressed_keys[K_i]:
self.rect.move_ip(0, -1)
if pressed_keys[K_k]:
self.rect.move_ip(0, 1)
if pressed_keys[K_j]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_l]:
self.rect.move_ip(1, 0)
pos = self.rect.center
if self.trail:
if self.trail[-1] != pos:
self.trail.append(pos)
else:
self.trail = [pos, pos]
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
class PlayerTwo(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
super(PlayerTwo, self).__init__()
self.surf = pygame.Surface((15, 10))
self.surf.fill((0, 255, 255))
self.rect = self.surf.get_rect()
self.rect.center = (WIDTHTWO / 2, HEIGHTTWO / 2)
self.trail = set()
def update(self, pressed_keys):
if pressed_keys[K_w]:
self.rect.move_ip(0, -1)
if pressed_keys[K_s]:
self.rect.move_ip(0, 1)
if pressed_keys[K_a]:
self.rect.move_ip(-1, 0)
if pressed_keys[K_d]:
self.rect.move_ip(1, 0)
pos = self.rect.center
if self.trail:
if self.trail[-1] != pos:
self.trail.append(pos)
else:
self.trail = [pos, pos]
if self.rect.left < 0:
self.rect.left = 0
if self.rect.right > 1000:
self.rect.right = 1000
if self.rect.top <= 0:
self.rect.top = 0
if self.rect.bottom >= 1000:
self.rect.bottom = 1000
player = PlayerOne()
playertwo = PlayerTwo()
running = True
while running:
for event in pygame.event.get():
if event.type == KEYDOWN:
if event.key == K_ESCAPE:
running = False
elif event.type == QUIT:
running = False
pressed_keys = pygame.key.get_pressed()
player.update(pressed_keys)
playertwo.update(pressed_keys)
screen.fill((0, 0, 0))
pygame.draw.lines(screen, (255, 255, 255), False, player.trail)
pygame.draw.lines(screen, (255, 255, 255), False, playertwo.trail)
screen.blit(player.surf, player.rect)
screen.blit(playertwo.surf, playertwo.rect)
pygame.display.flip()
pygame.quit()
Output:
Add an attribute rect_list to the class PalyereOne and append a copy of the current rectangle to the list when the player is moved:
class PlayerOne(pygame.sprite.Sprite):
p1_movement = 0
def __init__(self):
# [...]
self.rect_list = []
def update(self, pressed_keys):
self.rect_list.append(self.rect.copy())
# [...]
Draw the list of rectangles in the main application loop:
while running:
# [...]
screen.fill((0, 0, 0))
for rect in player.rect_list:
screen.blit(player.surf, rect)
# [...]
Do the same for PlayerTwo.
I wanted to add a sorta ammo clip thing in my shooter game, its very simple all I do is blit I * the ammount of ammo, but I wanted there to be three lines to show the clip like so:
IIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIII
IIIIIIIIIIIIIIIIIIIIII
Each line has 35 ammo symbols but if the player has more than 95 ammo I wanted it to blit that anount as a number next to the ammo clip, but no matter how much ammo I have it never blits the number and no matter how much ammo I have at the start it never blits more than this at the start until I collect a ammo box:
IIIIIIIIII
I really cant find out why it is doing this, I've tryed changing the amount it blits and many other things but nothing seems to work, so hear is my code:
#wave font
font = pygame.font.SysFont("", 34)
self.text_pause = font.render("WAVE " + str(self.wave) * self.blitwave, -1, RED)
self.text_pause_rect = self.text_pause.get_rect(center=self.screen.get_rect().center) # center text
texthealth = int(self.health / 10)
#health font
self.text_health = font.render("o" * texthealth, -1, RED)
#score font
self.text_score = font.render("SCORE " + str(self.score), -1, BLACK)
#cash font
self.text_cash = font.render(str(self.cash), -1, GREEN)
#ammo font
self.text_ammoS = font.render("I" * 35, -1, RED)
self.text_ammo = font.render("I" * self.ammo_amount, -1, RED)
self.text_ammoW = font.render("I" * (self.ammo_amount - 35), -1, RED)
self.text_ammoE = font.render("I" * (self.ammo_amount - 70), -1, RED)
self.text_ammoN = font.render(str(self.ammo_amount), -1, RED)
# send event to player
self.player.event_handler(event)
if not PAUSED:
self.all_sprites_list.update()
self.bullets_update()
player_rect = pygame.Rect(self.player.rect.x, self.player.rect.y, 20, 20)
block_rect = pygame.Rect(block.rect.x, block.rect.y, 20, 20)
I thought i could make a rect that follows the blocks and if that rect collides witht he player they die, pretty much using the same system as the ammo, coz that works instantly
if pygame.sprite.collide_rect(self.player, block):
self.player.health =- 00.0000000003
print('hit')
if self.ammo and player_rect.colliderect(self.ammo.rect):
self.ammo_amount += 70
self.all_sprites_list.remove(self.ammo)
self.ammo = None
#if self.Bomb and player_rect.colliderect(self.ammo.rect):
#print('nuke')
#self.all_sprites_list.remove(self.ammo)
#self.Bomb = None
self.crosshair.update()
# --- draws ---
self.background.draw(self.screen)
self.all_sprites_list.draw(self.screen)
#must be last
self.screen.blit(self.text_pause, (10, 610))
self.screen.blit(self.text_score, (700, 10))
self.screen.blit(self.text_cash, (740, 500))
#self.screen.blit(self.text_ammo, (450, 610))
if self.ammo_amount > 0 and self.ammo_amount < 36:
self.screen.blit(self.text_ammo, (600, 540))
if self.ammo_amount > 35 and self.ammo_amount < 71:
self.screen.blit(self.text_ammoS, (600, 540))
self.screen.blit(self.text_ammoW, (600, 560))
if self.ammo_amount > 70 and self.ammo_amount < 96:
self.screen.blit(self.text_ammoS, (600, 540))
self.screen.blit(self.text_ammoS, (600, 560))
self.screen.blit(self.text_ammoE, (600, 580))
if self.ammo_amount > 95:
self.screen.blit(self.text_ammoS, (600, 540))
self.screen.blit(self.text_ammoS, (600, 560))
self.screen.blit(self.text_ammoS, (600, 580))
self.screen.blit(self.text_ammoN, (550, 580))
self.screen.blit(self.text_health, (5, 5))
self.crosshair.draw(self.screen)
pygame.display.update() # use flip() OR update()
# --- FPS ---
clock.tick(70)
# --- quit ---
pygame.quit()
#----------------------------------------------------------------------
Game().run()
That is the important part but if you would like to see the whole code here it is:
import pygame
from pygame import *
import sys
import math
import random
import cmath
#----------------------------------------------------------------------
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
YELLOW = (255, 255, 0)
GREEN = (154 ,205, 50)
#images
IMAGE_GRASS = "grass_shit.png"
IMAGE_PLAYER = "shithead.png"
IMAGE_ALI = "shit_head2.png"
IMAGE_DEAD_SCREEN = "dead_shit.png"
IMAGE_CROSSHAIR = "crosshair.png"
IMAGE_PLAYBUTTON = "playbutton.png"
IMAGE_AMMO = "ammoshit.png"
#----------------------------------------------------------------------
class Block(pygame.sprite.Sprite):
def __init__(self, color, x, y, player = None):
pygame.sprite.Sprite.__init__(self)
self.player = player
self.image = pygame.Surface([20, 20])
self.image.fill(color)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.move_x = self.move_y = 0
def update(self):
if self.player:
player_x, player_y = self.player.rect.center
if self.rect.x < player_x:
self.rect.x += 1
elif self.rect.x > player_x:
self.rect.x -= 1
if self.rect.y < player_y:
self.rect.y += 1
elif self.rect.y > player_y:
self.rect.y -= 1
#----------------------------------------------------------------------
class Player(pygame.sprite.Sprite):
def __init__(self, screen_rect, x=0, y=0):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20,20])
self.image.fill(RED)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
self.min_x = screen_rect.left
self.min_y = screen_rect.top
self.max_x = screen_rect.right
self.max_y = screen_rect.bottom
self.move_x = self.move_y = 0
self.health = 138
def update(self):
pos = pygame.mouse.get_pos()
self.rect.x += self.move_x
self.rect.y += self.move_y
if self.rect.top < self.min_x:
self.rect.top = self.min_x
elif self.rect.bottom > self.max_y:
self.rect.bottom = self.max_y
if self.rect.left < self.min_x:
self.rect.left = self.min_x
elif self.rect.right > self.max_x:
self.rect.right = self.max_x
def event_handler(self, event):
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_a:
self.move_x = -8
elif event.key == pygame.K_d:
self.move_x = +8
elif event.key == pygame.K_w:
self.move_y = -8
elif event.key == pygame.K_s:
self.move_y = +8
if event.type == pygame.KEYUP:
if event.key in (pygame.K_a, pygame.K_d):
self.move_x = 0
elif event.key in (pygame.K_w, pygame.K_s):
self.move_y = 0
#----------------------------------------------------------------------
class Bullet(pygame.sprite.Sprite):
def __init__(self, start_pos, mouse_pos):
pygame.sprite.Sprite.__init__(self)
self.start_rect = start_pos.rect.copy()
self.mouse_x, self.mouse_y = mouse_pos # mouse[0], mouse[1]
self.image = pygame.Surface([5, 5])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.centerx = self.start_rect.centerx
self.rect.centery = self.start_rect.centery
self.speed = 20
self.max_range = 100
self.current_range = 0
distance_x = self.mouse_x - self.start_rect.centerx
distance_y = self.mouse_y - self.start_rect.centery
norm = math.sqrt(distance_x ** 2 + distance_y ** 2)
direction_x = distance_x / norm
direction_y = distance_y / norm
self.bullet_vector_x = direction_x * self.speed
self.bullet_vector_y = direction_y * self.speed
def update(self):
self.current_range += 1
if self.current_range < self.max_range:
#print self.start_rect.centerx + (self.bullet_vector_x*self.current_range),
#print self.rect.centerx + self.bullet_vector_x,
#self.rect.centerx += self.bullet_vector_x
self.rect.centerx = self.start_rect.centerx + (self.bullet_vector_x*self.current_range)
#print self.rect.centerx
#self.rect.centery += self.bullet_vector_y
self.rect.centery = self.start_rect.centery + (self.bullet_vector_y*self.current_range)
else:
self.kill()
#----------------------------------------------------------------------
class Crosshair(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(IMAGE_CROSSHAIR).convert_alpha()
self.rect = self.image.get_rect()
def update(self):
mouse_x, mouse_y = pygame.mouse.get_pos()
self.rect.centerx = mouse_x
self.rect.centery = mouse_y
def draw(self, screen):
screen.blit(self.image,self.rect.topleft)
#----------------------------------------------------------------------
class Background(pygame.sprite.Sprite):
def __init__(self):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.image.load(IMAGE_GRASS).convert_alpha()
self.rect = self.image.get_rect()
def draw(self, screen):
screen.fill((128,128,128))
screen.blit(self.image,(0,0))
#----------------------------------------------------------------------
class Ammo(pygame.sprite.Sprite):
def __init__(self, color, x, y, player = None):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 20])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
pass
class Bomb(pygame.sprite.Sprite):
def __init__(self, color, x, y, player = None):
pygame.sprite.Sprite.__init__(self)
self.image = pygame.Surface([20, 20])
self.image.fill(BLACK)
self.rect = self.image.get_rect()
self.rect.x = x
self.rect.y = y
def update(self):
pass
class Game():
def __init__(self):
pygame.init()
screen_width = 850
screen_height = 640
place_ammo = False
self.screen = pygame.display.set_mode( (screen_width,screen_height) )
pygame.mouse.set_visible(False)
#-----
self.all_sprites_list = pygame.sprite.Group()
self.block_list = pygame.sprite.Group()
self.bullet_list = pygame.sprite.Group()
self.blitwave = 1
# --- create sprites ---
self.background = Background()
self.player = Player(self.screen.get_rect(), 0, 370)
self.all_sprites_list.add(self.player)
self.ammo = Ammo(self.screen.get_rect(),random.randrange(10, 750),random.randint(10,600 - 10))
self.all_sprites_list.add(self.ammo)
self.ammo_amount = 100
self.on_screen = 1
self.score = 0
self.crosshair = Crosshair()
def bullet_create(self, start_pos, mouse_pos):
bullet = Bullet(start_pos, mouse_pos)
self.all_sprites_list.add(bullet)
self.bullet_list.add(bullet)
def bullets_update(self):
for bullet in self.bullet_list:
block_hit_list = pygame.sprite.spritecollide(bullet, self.block_list, True)
screen_width = 850
screen_height = 640
for block in block_hit_list:
self.bullet_list.remove(bullet)
self.all_sprites_list.remove(bullet)
self.score += 1
self.on_screen -= 1
self.ammo_chance = self.ammo_amount * 5
if self.ammo_chance > 0:
self.drop = random.randint(1, self.ammo_chance)
print(self.drop)
if self.drop > 0 and self.drop < 2:
print('ammo drop')
self.ammo = Ammo(self.screen.get_rect(),random.randrange(10,screen_width),random.randint(10,screen_height - 10))
self.all_sprites_list.add(self.ammo)
if bullet.rect.y < -10:
self.bullet_list.remove(bullet)
self.all_sprites_list.remove(bullet)
# -------- Main Program Loop -----------
def run(self):
screen_width = 850
screen_height = 640
#wave
self.wave = 1
self.wave_no = 2
self.wave_running = True
block = Block(BLUE, random.randrange(100, screen_width), random.randrange(10, screen_height-10), self.player)
self.block_list.add(block)
self.all_sprites_list.add(block)
clock = pygame.time.Clock()
self.cash = 1
self.health = 100
self.ammo_amount = 10
RUNNING = True
PAUSED = False
while RUNNING:
# --- events ---
if self.player.health <= 0:
RUNNING = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
RUNNING = PAUSED
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
PAUSED = True
elif event.type == pygame.KEYUP:
if event.key == pygame.K_SPACE:
PAUSED = not PAUSED
elif event.type == pygame.MOUSEBUTTONDOWN and self.ammo_amount > 0:
self.bullet_create(self.player, event.pos)
self.ammo_amount -= 1
self.cash = self.score * 5
if self.on_screen == 0:
for i in range(self.wave_no):
block = Block(BLUE, random.randrange(100, screen_width), random.randrange(10, screen_height-10), self.player)
self.block_list.add(block)
self.all_sprites_list.add(block)
self.on_screen += 1
self.wave_div = int(self.wave_no / 2)
self.wave_no += self.wave_div
self.wave += 1
#wave font
font = pygame.font.SysFont("", 34)
self.text_pause = font.render("WAVE " + str(self.wave) * self.blitwave, -1, RED)
self.text_pause_rect = self.text_pause.get_rect(center=self.screen.get_rect().center) # center text
texthealth = int(self.health / 10)
#health font
self.text_health = font.render("o" * texthealth, -1, RED)
#score font
self.text_score = font.render("SCORE " + str(self.score), -1, BLACK)
#cash font
self.text_cash = font.render(str(self.cash), -1, GREEN)
#ammo font
self.text_ammoS = font.render("I" * 35, -1, RED)
self.text_ammo = font.render("I" * self.ammo_amount, -1, RED)
self.text_ammoW = font.render("I" * (self.ammo_amount - 35), -1, RED)
self.text_ammoE = font.render("I" * (self.ammo_amount - 70), -1, RED)
self.text_ammoN = font.render(str(self.ammo_amount), -1, RED)
# send event to player
self.player.event_handler(event)
if not PAUSED:
self.all_sprites_list.update()
self.bullets_update()
player_rect = pygame.Rect(self.player.rect.x, self.player.rect.y, 20, 20)
block_rect = pygame.Rect(block.rect.x, block.rect.y, 20, 20)
#I thought i could make a rect that follows the blocks and if that rect collides witht he player they die, pretty much using the same system as the ammo, coz that works instantly
if pygame.sprite.collide_rect(self.player, block):
self.player.health =- 00.0000000003
print('hit')
if self.ammo and player_rect.colliderect(self.ammo.rect):
self.ammo_amount += 70
self.all_sprites_list.remove(self.ammo)
self.ammo = None
#if self.Bomb and player_rect.colliderect(self.ammo.rect):
#print('nuke')
#self.all_sprites_list.remove(self.ammo)
#self.Bomb = None
self.crosshair.update()
# --- draws ---
self.background.draw(self.screen)
self.all_sprites_list.draw(self.screen)
#must be last
self.screen.blit(self.text_pause, (10, 610))
self.screen.blit(self.text_score, (700, 10))
self.screen.blit(self.text_cash, (740, 500))
#self.screen.blit(self.text_ammo, (450, 610))
if self.ammo_amount > 0 and self.ammo_amount < 36:
self.screen.blit(self.text_ammo, (600, 540))
if self.ammo_amount > 35 and self.ammo_amount < 71:
self.screen.blit(self.text_ammoS, (600, 540))
self.screen.blit(self.text_ammoW, (600, 560))
if self.ammo_amount > 70 and self.ammo_amount < 96:
self.screen.blit(self.text_ammoS, (600, 540))
self.screen.blit(self.text_ammoS, (600, 560))
self.screen.blit(self.text_ammoE, (600, 580))
if self.ammo_amount > 95:
self.screen.blit(self.text_ammoS, (600, 540))
self.screen.blit(self.text_ammoS, (600, 560))
self.screen.blit(self.text_ammoS, (600, 580))
self.screen.blit(self.text_ammoN, (550, 580))
self.screen.blit(self.text_health, (5, 5))
self.crosshair.draw(self.screen)
pygame.display.update() # use flip() OR update()
# --- FPS ---
clock.tick(70)
# --- quit ---
pygame.quit()
#----------------------------------------------------------------------
Game().run()
Thank you so much for your time and help.
You don't blit ammo number - see # in line:
#self.screen.blit(self.text_ammo, (450, 610))
And it has to be text_ammoN not text_ammo
By the way:
You have to much code in main loop - put some code in functions like update_wave_text() , update_ammo_text(), etc. , blit_wave_text(), blit_ammo_tex(), etc.
Use self.: in __init__ set
self.screen_width = 850
self.screen_height = 640
and than use self.screen_width, self.screen_height and you will not have to repeat
screen_width = 850
screen_height = 640