Posts: 30
Threads: 13
Joined: Jan 2018
May-06-2021, 07:05 PM
(This post was last modified: May-24-2021, 08:23 PM by nilamo.)
Im trying to tun this code which is a shooter game, if anyone can help me i would really appreciate it, i attach images.
https://github.com/abscorpy/Spaceships/b...ip_red.png
https://github.com/abscorpy/Spaceships/b...yellow.png
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
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)
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)
pygame.display.update()
pygame.quit()
if __name__ == "__main__":
main
Thanks for the support
Attached Files
Thumbnail(s)
Posts: 371
Threads: 2
Joined: Jan 2021
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
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 ()
Posts: 5,150
Threads: 396
Joined: Sep 2016
May-08-2021, 11:37 PM
(This post was last modified: May-08-2021, 11:37 PM by metulburr.)
Your at a fork in the road where you can go either way. One way leads to spaghetti code. While the other organizes. However the organization uses classes. I would highly recommend learning them if you have not yet. Of course in one's learning experience, you have to go down the route of spaghetti code to understand why its so unorganized to begin with. It depends on your age, and knowledge of coding, and how far you actually want to take it. None of which i know the answer to. So take it with a grain of salt.
a few opinions: - There should only ever be one line in your entire game of
pygame.display.update() . If you have more than one, you are heading in the direction of spaghetti code.
- A Spaceship class and Bullet class would better organize this code and put sporadic lines with its corresponding class. Thus it is easier to start with the main game loop and spawn out objects from there.
The closest example i can think of is this example of the code a the bottom comparing the two programs of with classes and without. Also my tutorial series does a space invaders simple clone to illustrate proper coding techniques. In this particular part, i convert the code of the player into the Player class. In part 4, i make the player have a method of shooting (bullets).
SheeppOSU likes this post
Recommended Tutorials:
Posts: 30
Threads: 13
Joined: Jan 2018
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
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 ()
Posts: 371
Threads: 2
Joined: Jan 2021
You had the call to yellow_handle_movement () and red_handle_movement () outside of the main loop. That part is working now.
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 = 3
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)
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)
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)
handle_bullets (yellow_bullets, red_bullets, yellow, red)
pygame.quit()
if __name__ == "__main__":
main ()
Posts: 544
Threads: 15
Joined: Oct 2016
When iterating over a list. Then remove item from same list. Will cause next item to be skip in list.
Example. Try to remove 2 and 3. But 3 will never get check.
mylist = [1, 2, 3, 4, 5]
for i in mylist:
print(i)
if i in [2, 3]:
mylist.remove(i)
print(mylist) You have two options.
You can copy the list or you can create a list for deletion.
mylist = [1, 2, 3, 4, 5]
removelist = []
for i in mylist:
print(i)
if i in [2, 3]:
removelist.append(i)
for r in removelist:
mylist.remove(r)
print(mylist) mylist = [1, 2, 3, 4, 5]
# shallow copy
for i in mylist[:]:
print(i)
if i in [2, 3]:
mylist.remove(i)
print(mylist)
99 percent of computer problems exists between chair and keyboard.
Posts: 30
Threads: 13
Joined: Jan 2018
Now this is the code i have, it doesnt run again
Can anyone help me
Thanks for the support!
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)
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 44
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
FPS = 60
VEL = 3
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('First_Game', '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('First_Game', '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('First_Game', 'space.png')),(WIDTH, HEIGHT))
FPS = 60
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(red_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
# 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)
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)
if event.key == pygame.K_SPACE and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
if event.type == RED_HIT:
red_health -= 1
if event.type == YELLOW_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 != "":
pass #SOMEONE WON
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)
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)
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)
handle_bullets (yellow_bullets, red_bullets, yellow, red)
main()
pygame.quit()
if __name__ == "__main__":
main ()
Posts: 480
Threads: 86
Joined: Feb 2018
(May-22-2021, 11:25 AM)abscorpy Wrote: Now this is the code i have, it doesnt run again
Can anyone help me
Thanks for the support!
Not many people want to take the time to look through a huge amount of code like this to try and find where it's not working. You should try debugging yourself to corner it down to a much smaller section of code until you find the problem, and if you don't find the problem, then you should ask a forum. However, just posting a bunch of code and expecting someone to look through it for the error seems lazy. My recommendations for debugging are to go through the code as if you were running it yourself and try to see how the program may have gotten the wrong result. You can also put print statements all over the code to try and see what's happening while the code is running.
Posts: 371
Threads: 2
Joined: Jan 2021
You have remove the section of code that calls all of the screen updating functions so I put them back. Also in draw_window () you update yellow_health using the variable red_health .
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)
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 44
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
FPS = 60
VEL = 3
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('First_Game', '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('First_Game', '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('First_Game', 'space.png')),(WIDTH, HEIGHT))
FPS = 60
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 = pygame.Rect(700,300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
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()
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)
if event.key == pygame.K_SPACE and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
if event.type == RED_HIT:
red_health -= 1
if event.type == YELLOW_HIT:
print ('Yellow 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 != "":
pass #SOMEONE WON
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)
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)
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)
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)
handle_bullets (yellow_bullets, red_bullets, yellow, red)
main()
pygame.quit()
if __name__ == "__main__":
main ()
Posts: 30
Threads: 13
Joined: Jan 2018
Hello guys, me again.
I dont know why it throws me this error:
Traceback (most recent call last):
File "D:\Alan\Python\First_Game\First_Game_1.py", line 29, in <module>
YELLOW_SPACESHIP_IMAGE = pygame.image.load(os.path.join('First_Game', 'spaceship_yellow.png'))
pygame.error: Couldn't open First_Game\spaceship_yellow.png
>>>
I hope someone can help me. My image is in the correct folder, so i dont know why it shows me this error.
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)
SPACESHIP_WIDTH, SPACESHIP_HEIGHT = 55, 44
BORDER = pygame.Rect(WIDTH//2 - 5, 0, 10, HEIGHT)
HEALTH_FONT = pygame.font.SysFont('comicsans', 40)
WINNER_FONT = pygame.font.SysFont('comicsans', 100)
FPS = 60
VEL = 3
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('First_Game', '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('First_Game', '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('First_Game', 'space.png')),(WIDTH, HEIGHT))
FPS = 60
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 = pygame.Rect(700,300, SPACESHIP_WIDTH, SPACESHIP_HEIGHT)
red_bullets.remove(bullet)
elif bullet.x < 0:
red_bullets.remove(bullet)
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()
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)
if event.key == pygame.K_SPACE and len(red_bullets) < MAX_BULLETS:
bullet = pygame.Rect(red.x, red.y + red.height//2 - 2, 10, 5)
red_bullets.append(bullet)
if event.type == RED_HIT:
red_health -= 1
if event.type == YELLOW_HIT:
print ('Yellow 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 != "":
pass #SOMEONE WON
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)
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)
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)
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)
handle_bullets (yellow_bullets, red_bullets, yellow, red)
main()
pygame.quit()
if __name__ == "__main__":
main ()
|