Friday, February 10, 2023

Playable

I've removed the border: it's playable.


import random
import time
import pygame
import os
from os import remove
pygame.font.init()
pygame.mixer.init()

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game!")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)

FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 45, 45

BALLOON_WIDTH, BALLOON_HEIGHT = 60, 60
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
BALLOON_HIT_Y = pygame.USEREVENT + 3
BALLOON_HIT_R = pygame.USEREVENT + 4
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))

BALLOON = pygame.image.load('WBalloon.gif')
BALLOON_RECT = BALLOON.get_rect()

balloon = pygame.sprite.Sprite()
obstacles = pygame.sprite.Group()
obstacles.add(balloon)
def draw_window(red, yellow, red_bullets, yellow_bullets, red_health,
yellow_health, balloon):
WIN.blit(SPACE, (0, 0))


red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))

WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))


WIN.blit(BALLOON, (balloon.x, balloon.y))


for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)

for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)



pygame.display.update()


def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < 900: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height \
< HEIGHT - 15: # DOWN
yellow.y += VEL


def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > 0: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < 900: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL


def handle_bullets(yellow_bullets, red_bullets, yellow, red, balloon,
yellow_health, red_health):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif balloon.colliderect(bullet):
pygame.event.post(pygame.event.Event(BALLOON_HIT_Y))
yellow_bullets.remove(bullet)

elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)

for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)

elif balloon.colliderect(bullet):
pygame.event.post(pygame.event.Event(BALLOON_HIT_R))
red_bullets.remove(bullet)



elif bullet.x < 0:
red_bullets.remove(bullet)


def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)


def main():
BALLOON_X = random.randrange(100, 800, 10)
BALLOON_Y = random.randrange(30, 470, 10)
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
balloon = pygame.Rect(BALLOON_X, BALLOON_Y, BALLOON_WIDTH,
BALLOON_HEIGHT)

red_bullets = []
yellow_bullets = []


red_health = 10
yellow_health = 10

clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_z and len(yellow_bullets) \
< MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y +
yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_yellow", time.time())

if event.key == pygame.K_SLASH and len(red_bullets) <\
MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_red", time.time())


if event.type == RED_HIT:
red_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == YELLOW_HIT:
yellow_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == BALLOON_HIT_Y:
yellow_health -= 1


if event.type == BALLOON_HIT_R:
red_health -= 1


winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"

if yellow_health <= 0:
winner_text = "Red Wins!"

if winner_text != "":
draw_winner(winner_text)
break

keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)

handle_bullets(yellow_bullets, red_bullets, yellow, red, balloon,
yellow_health,
red_health)

draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health, balloon)



main()


if __name__ == "__main__":
main()

An alternative might be to keep the border, and have the balloon saunter across the screen.

It works for me, but even at 1 pixel per frame it is fast. Pygame doen't respond to floats so I ended up 

halving the FPS on the game.
  
Under main:
                                                                          
  
At the top, with the global variables:

                                                                            




Thursday, February 9, 2023

Really‽

 For and against:

Why does Ukraine want Western jets—and will it get them? | The Economist

https://www.theguardian.com/commentisfree/2023/feb/09/zelenskiy-jets-west-ukraine-battlefield-peace

                                                          *     *     *

So currently, the balloon can appear anywhere on the screen; and whoever shoots at the balloon

looses a life. All well and good. But this also means that the game is stalled, because no one

wants to move and expose themselves. So either the baloon moves, or the spaceships each get

access to the whole board!!

                                                                       


import random
import time
import pygame
import os
pygame.font.init()
pygame.mixer.init()

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game!")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)

#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')

HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)

FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 45, 45

BALLOON_WIDTH, BALLOON_HEIGHT = 60, 60
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
BALLOON_HIT_Y = pygame.USEREVENT + 3
BALLOON_HIT_R = pygame.USEREVENT + 4
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))

BALLOON = pygame.image.load('WBalloon.gif')
#B_RECT = BALLOON.get_rect()

def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health, balloon):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)

red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))

WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
WIN.blit(BALLOON, (balloon.x, balloon.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)

for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)



pygame.display.update()


def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL


def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL


def handle_bullets(yellow_bullets, red_bullets, yellow, red, balloon, yellow_health, red_health):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif balloon.colliderect(bullet):
pygame.event.post(pygame.event.Event(BALLOON_HIT_Y))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)

for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif balloon.colliderect(bullet):
pygame.event.post(pygame.event.Event(BALLOON_HIT_R))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)


def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)


def main():
BALLOON_X = random.randrange(100, 800, 10)
BALLOON_Y = random.randrange(30, 470, 10)
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
balloon = pygame.Rect(BALLOON_X, BALLOON_Y, BALLOON_WIDTH, BALLOON_HEIGHT)
red_bullets = []
yellow_bullets = []

red_health = 10
yellow_health = 10

clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_z and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_yellow", time.time())

if event.key == pygame.K_SLASH and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_red", time.time())


if event.type == RED_HIT:
red_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == YELLOW_HIT:
yellow_health -= 1
#BULLET_HIT_SOUND.play()

elif event.type == BALLOON_HIT_Y:
yellow_health -= 1


elif event.type == BALLOON_HIT_R:
red_health -= 1


winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"

if yellow_health <= 0:
winner_text = "Red Wins!"

if winner_text != "":
draw_winner(winner_text)
break

keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)

handle_bullets(yellow_bullets, red_bullets, yellow, red, balloon, yellow_health, red_health)

draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health, balloon)

main()


if __name__ == "__main__":
main()

Wednesday, February 8, 2023

Case 1

 It was mentally painful, but now the worst is done. I hav

coded the health of Yellow to go down 1 if he hits the weather

balloon with a bullet.

                              


The rest of the code ahouldn't be too difficult.

import random
import time
import pygame
import os
pygame.font.init()
pygame.mixer.init()

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game!")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)

#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')

HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)

FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 45, 45

BALLOON_WIDTH, BALLOON_HEIGHT = 60, 60
YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2
BALLOON_HIT = pygame.USEREVENT + 3
YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))

BALLOON = pygame.image.load('WBalloon.gif')
#B_RECT = BALLOON.get_rect()

def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health, balloon):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)

red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))

WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))
WIN.blit(BALLOON, (balloon.x, balloon.y))
for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)

for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)



pygame.display.update()


def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL


def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL


def handle_bullets(yellow_bullets, red_bullets, yellow, red, balloon):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif balloon.colliderect(bullet):
pygame.event.post(pygame.event.Event(BALLOON_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)

for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)


def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)


def main():
BALLOON_X = random.randrange(20, 800, 10)
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
balloon = pygame.Rect(BALLOON_X, 30, BALLOON_WIDTH, BALLOON_HEIGHT)
red_bullets = []
yellow_bullets = []

red_health = 10
yellow_health = 10

clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_z and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_yellow", time.time())

if event.key == pygame.K_SLASH and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_red", time.time())


if event.type == RED_HIT:
red_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == YELLOW_HIT:
yellow_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == BALLOON_HIT:
yellow_health -= 1

winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"

if yellow_health <= 0:
winner_text = "Red Wins!"

if winner_text != "":
draw_winner(winner_text)
break

keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)

handle_bullets(yellow_bullets, red_bullets, yellow, red, balloon)

draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health, balloon)

main()


if __name__ == "__main__":
main()

                                                                     *     *     *

Found the following useful for understanding collision detection in pygame:



                                            *     *     *
As for BLIT:

Not everything is redrawn, but only that which has changed. Groups of bits move

as one in reaction to a mask of  logical operators.

Tuesday, February 7, 2023

Playing

 The Tech with Tim game I am playing:

import time
import pygame
import os
pygame.font.init()
pygame.mixer.init()

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game!")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)

#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')

HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)

FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 45, 45

YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2

YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))


def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)

red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))

WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))

for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)

for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)

pygame.display.update()


def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL


def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL


def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)

for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)


def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)


def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)

red_bullets = []
yellow_bullets = []

red_health = 10
yellow_health = 10

clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()

if event.type == pygame.KEYDOWN:

if event.key == pygame.K_z and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_yellow", time.time())

if event.key == pygame.K_SLASH and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()
print("Bullet_red", time.time())


if event.type == RED_HIT:
red_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == YELLOW_HIT:
yellow_health -= 1
#BULLET_HIT_SOUND.play()

winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"

if yellow_health <= 0:
winner_text = "Red Wins!"

if winner_text != "":
draw_winner(winner_text)
break

keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)

handle_bullets(yellow_bullets, red_bullets, yellow, red)

draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health)

main()


if __name__ == "__main__":
main()

https://www.infoworld.com/article/3632284/python-concurrency-and-parallelism-explained.html

                                                                  *    *     *

Not getting political here, but I am thinking of adding a or some weather balloons slowly

traversing the game space. If either spaceship touches it with either self or bullet, they loose a life

and the balloon disappears. Should add a level of complexity to the game.


Now I need to work on the balloon size, and all that collision detection: tomorrow...


                                                                    

                                                                        *     *     *

Out of curiosity: the jet stream circles the Earth between 5.6 to 7.5 miles

above the Earth. This is also the height at which commercial aircraft fly.

A weather balloon can reach up to 25 miles. At 60 000 feet, the Chinese Weather

balloon was at 11 miles.     


Image from Wikipedia: 
                       




                               

Monday, February 6, 2023

Quit

 Started  looking at pygame, yesterday. Big problem; how to quit.

The window for the little snake game I found just would not close.

Adding pygame.quit() at the end allows it to close, but only from the

PyCharm page.

import pygame
import random

pygame.init()

green = (0, 255, 0)
blue = (50, 153, 213)

dis_width = 400
dis_height = 400

dis = pygame.display.set_mode((dis_width, dis_height))

clock = pygame.time.Clock()

def run():
x1 = dis_width / 2
y1 = dis_height / 2
dir = [0, 0]
snake_list = []
snake_len = 1
foodx = 30
foody = 30


while True:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LEFT:
dir = [-10, 0]
elif event.key == pygame.K_RIGHT:
dir = [10, 0]
elif event.key == pygame.K_UP:
dir = [0, -10]
elif event.key == pygame.K_DOWN:
dir = [0, 10]

x1 += dir[0]
y1 += dir[1]
dis.fill(blue)
pygame.draw.rect(dis, green, [foodx, foody, 10, 10])

snake_list.append([x1, y1])
if len(snake_list) > snake_len:
del snake_list[0]

if ([x1, y1] in snake_list[:-1]):
run()

if (x1 >= dis_width or x1 < 0 or y1 >= dis_height or y1 < 0):
run()

for x in snake_list:
pygame.draw.rect(dis, green, [x[0], x[1], 10, 10])

if x1 == foodx and y1 == foody:
foodx = round(random.randrange(0, dis_width - 10) / 10.0) * 10.0
foody = round(random.randrange(0, dis_height - 10) / 10.0) * 10.0
snake_len += 1

pygame.display.update()
clock.tick(10)

run()
pygame.quit()


The Tech with Tim intro to Pygame starts with precisely how-to quit.

                                                     


*     *     *

Got the tutorial code running. Only change I made was replacing the RCRTL key

with SLASH because that is a key I don't have on my smaller computer... As for enabling

the sound effects, these are loud and rather dramatic... All in all, a classic game, and helpfully

explained.

import pygame
import os
pygame.font.init()
pygame.mixer.init()

WIDTH, HEIGHT = 900, 500
WIN = pygame.display.set_mode((WIDTH, HEIGHT))
pygame.display.set_caption("First Game!")

WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
RED = (255, 0, 0)
YELLOW = (255, 255, 0)

BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)

#BULLET_HIT_SOUND = pygame.mixer.Sound('Assets/Grenade+1.mp3')
#BULLET_FIRE_SOUND = pygame.mixer.Sound('Assets/Gun+Silencer.mp3')

HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)

FPS = 60
VEL = 5
BULLET_VEL = 7
MAX_BULLETS = 3
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 40

YELLOW_HIT = pygame.USEREVENT + 1
RED_HIT = pygame.USEREVENT + 2

YELLOW_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_yellow.png'))
YELLOW_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
YELLOW_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 90)

RED_SPACESHIP_IMAGE = pygame.image.load(
os.path.join('Assets', 'spaceship_red.png'))
RED_SPACESHIP = pygame.transform.rotate(pygame.transform.scale(
RED_SPACESHIP_IMAGE, (SPACESHIP_WIDTH, SPACESHIP_HEIGHT)), 270)

SPACE = pygame.transform.scale(pygame.image.load(
os.path.join('Assets', 'space.png')), (WIDTH, HEIGHT))


def draw_window(red, yellow, red_bullets, yellow_bullets, red_health, yellow_health):
WIN.blit(SPACE, (0, 0))
pygame.draw.rect(WIN, BLACK, BORDER)

red_health_text = HEALTH_FONT.render(
"Health: " + str(red_health), 1, WHITE)
yellow_health_text = HEALTH_FONT.render(
"Health: " + str(yellow_health), 1, WHITE)
WIN.blit(red_health_text, (WIDTH - red_health_text.get_width() - 10, 10))
WIN.blit(yellow_health_text, (10, 10))

WIN.blit(YELLOW_SPACESHIP, (yellow.x, yellow.y))
WIN.blit(RED_SPACESHIP, (red.x, red.y))

for bullet in red_bullets:
pygame.draw.rect(WIN, RED, bullet)

for bullet in yellow_bullets:
pygame.draw.rect(WIN, YELLOW, bullet)

pygame.display.update()


def yellow_handle_movement(keys_pressed, yellow):
if keys_pressed[pygame.K_a] and yellow.x - VEL > 0: # LEFT
yellow.x -= VEL
if keys_pressed[pygame.K_d] and yellow.x + VEL + yellow.width < BORDER.x: # RIGHT
yellow.x += VEL
if keys_pressed[pygame.K_w] and yellow.y - VEL > 0: # UP
yellow.y -= VEL
if keys_pressed[pygame.K_s] and yellow.y + VEL + yellow.height < HEIGHT - 15: # DOWN
yellow.y += VEL


def red_handle_movement(keys_pressed, red):
if keys_pressed[pygame.K_LEFT] and red.x - VEL > BORDER.x + BORDER.width: # LEFT
red.x -= VEL
if keys_pressed[pygame.K_RIGHT] and red.x + VEL + red.width < WIDTH: # RIGHT
red.x += VEL
if keys_pressed[pygame.K_UP] and red.y - VEL > 0: # UP
red.y -= VEL
if keys_pressed[pygame.K_DOWN] and red.y + VEL + red.height < HEIGHT - 15: # DOWN
red.y += VEL


def handle_bullets(yellow_bullets, red_bullets, yellow, red):
for bullet in yellow_bullets:
bullet.x += BULLET_VEL
if red.colliderect(bullet):
pygame.event.post(pygame.event.Event(RED_HIT))
yellow_bullets.remove(bullet)
elif bullet.x > WIDTH:
yellow_bullets.remove(bullet)

for bullet in red_bullets:
bullet.x -= BULLET_VEL
if yellow.colliderect(bullet):
pygame.event.post(pygame.event.Event(YELLOW_HIT))
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)


def draw_winner(text):
draw_text = WINNER_FONT.render(text, 1, WHITE)
WIN.blit(draw_text, (WIDTH/2 - draw_text.get_width() /
2, HEIGHT/2 - draw_text.get_height()/2))
pygame.display.update()
pygame.time.delay(5000)


def main():
red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)

red_bullets = []
yellow_bullets = []

red_health = 10
yellow_health = 10

clock = pygame.time.Clock()
run = True
while run:
clock.tick(FPS)
for event in pygame.event.get():
if event.type == pygame.QUIT:
run = False
pygame.quit()

if event.type == pygame.KEYDOWN:
if event.key == pygame.K_LCTRL and len(yellow_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
yellow.x + yellow.width, yellow.y + yellow.height//2 - 2, 10, 5)
yellow_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()

if event.key == pygame.K_SLASH and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(
red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
#BULLET_FIRE_SOUND.play()

if event.type == RED_HIT:
red_health -= 1
#BULLET_HIT_SOUND.play()

if event.type == YELLOW_HIT:
yellow_health -= 1
#BULLET_HIT_SOUND.play()

winner_text = ""
if red_health <= 0:
winner_text = "Yellow Wins!"

if yellow_health <= 0:
winner_text = "Red Wins!"

if winner_text != "":
draw_winner(winner_text)
break

keys_pressed = pygame.key.get_pressed()
yellow_handle_movement(keys_pressed, yellow)
red_handle_movement(keys_pressed, red)

handle_bullets(yellow_bullets, red_bullets, yellow, red)

draw_window(red, yellow, red_bullets, yellow_bullets,
red_health, yellow_health)

main()


if __name__ == "__main__":
main()

Pygame itself is rather impressive: one can move the two spaceships at the same time. Or so

it seems...