Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spaceship
#4
Thanks for the support, but now what doesnt run is the Handle_movement() function

Can you please help me, thanks for the support!

thanks!


(May-06-2021, 10:16 PM)BashBedlam Wrote: What you have so far has been corrected and now runs as expected. You can shoot at each other but that's about it. It looks like a good start though Smile

import pygame
import os
 
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)
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 44
 
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)
 
FPS = 60
 
def draw_window(red, yellow, red_bullets, yellow_bullets):
	WIN.fill(WHITE)
	pygame.draw.rect(WIN, BLACK, BORDER)
	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

		draw_window (red, yellow, red_bullets, yellow_bullets)
 
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 = pygame.Rect(700,300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
			red_bullets.remove(bullet)
		elif bullet.x < 0:
			red_bullets.remove(bullet)
 
		keys_pressed=pygame.key.get_pressed()
		yellow_handle_movement(keys_pressed, yellow)
		red_handle_movement(keys_pressed, red)
		draw_window(red ,yellow, red_bullets, yellow_bullets)

def main():
	red = pygame.Rect(700, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
	yellow = pygame.Rect(100, 300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
 
	red_bullets = []
	yellow_bullets = []
	 
	clock=pygame.time.Clock()
	run= True
	while run:
		clock.tick(FPS)
		for event in pygame.event.get():
			if event.type == pygame.QUIT:
				run = False
		 
			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)
									 
				if event.key == pygame.K_RCTRL and len(red_bullets) < MAX_BULLETS:
					bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)
					red_bullets.append(bullet)

		draw_window (red, yellow, red_bullets, yellow_bullets)
		handle_bullets (yellow_bullets, red_bullets, yellow, red)
 
	pygame.quit()
 
if __name__ == "__main__":
	main ()
Reply


Messages In This Thread
Spaceship - by abscorpy - May-06-2021, 07:05 PM
RE: Spaceship - by BashBedlam - May-06-2021, 10:16 PM
RE: Spaceship - by abscorpy - May-11-2021, 09:08 PM
RE: Spaceship - by metulburr - May-08-2021, 11:37 PM
RE: Spaceship - by BashBedlam - May-12-2021, 02:26 PM
RE: Spaceship - by Windspar - May-15-2021, 01:49 PM
RE: Spaceship - by abscorpy - May-22-2021, 11:25 AM
RE: Spaceship - by SheeppOSU - May-24-2021, 09:56 PM
RE: Spaceship - by BashBedlam - Jun-01-2021, 02:24 PM
RE: Spaceship - by abscorpy - Jul-23-2021, 09:25 AM
RE: Spaceship - by metulburr - Jul-23-2021, 07:00 PM
RE: Spaceship - by abscorpy - Jul-26-2021, 07:15 AM
RE: Spaceship - by SheeppOSU - Aug-06-2021, 07:53 PM
RE: Spaceship - by abscorpy - Aug-07-2021, 11:25 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020