Notes
Notes - notes.io |
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)
BLUE = (0, 0, 225)
pygame.init()
# Set the width and height of the screen [width, height]
size = (1000, 700)
x_coord1 = 750
y_coord1 = 300
x_speed1 = 0
y_speed1 = 0
x_coord2 = 200
y_coord2 = 300
x_speed2 = 0
y_speed2 = 0
class BulletRed(pygame.sprite.Sprite):
""" This class represents the bullet . """
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([10, 4])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
def update(self):
""" Move the bullet. """
self.rect.x += -3
class BulletBlue(pygame.sprite.Sprite):
""" This class represents the bullet . """
def __init__(self):
# Call the parent class (Sprite) constructor
super().__init__()
self.image = pygame.Surface([10, 4])
self.image.fill(WHITE)
self.rect = self.image.get_rect()
def update(self):
""" Move the bullet. """
self.rect.x += 3
screen = pygame.display.set_mode(size)
pygame.display.set_caption("SquareFARE")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
# This is a list of every sprite. All blocks and the player block as well.
all_sprites_list = pygame.sprite.Group()
# List of each block in the game
block_list = pygame.sprite.Group()
# List of each bullet
bullet_list = pygame.sprite.Group()
# -------- Main Program Loop -----------
while not done:
# --- Game logic should go here
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
## elif event.type == pygame.K_SPACE:
## # Fire a bullet if the user clicks the mouse button
## bullet = Bullet()
## # Set the bullet so it is where the player is
## bullet.rect.x = x_coord2
## bullet.rect.y = y_coord2
## # Add the bullet to the lists
## all_sprites_list.add(bullet)
## bullet_list.add(bullet)
# User pressed down on a key
elif event.type == pygame.KEYDOWN:
# Figure out if it was an arrow key. If so
# adjust speed.
if event.key == pygame.K_LEFT:
x_speed1 = -3
elif event.key == pygame.K_RIGHT:
x_speed1 = 3
elif event.key == pygame.K_UP:
y_speed1 = -3
elif event.key == pygame.K_DOWN:
y_speed1 = 3
elif event.key == pygame.K_a:
x_speed2 = -3
elif event.key == pygame.K_d:
x_speed2 = 3
elif event.key == pygame.K_w:
y_speed2 = -3
elif event.key == pygame.K_s:
y_speed2 = 3
elif event.key == pygame.K_p:
# Fire a bullet if the user clicks the mouse button
bulletred = BulletRed()
# Set the bullet so it is where the player is
bulletred.rect.x = x_coord1
bulletred.rect.y = y_coord1
# Add the bullet to the lists
all_sprites_list.add(bulletred)
bullet_list.add(bulletred)
elif event.key == pygame.K_SPACE:
# Fire a bullet if the user clicks the mouse button
bulletblue = BulletBlue()
# Set the bullet so it is where the player is
bulletblue.rect.x = x_coord2
bulletblue.rect.y = y_coord2
# Add the bullet to the lists
all_sprites_list.add(bulletblue)
bullet_list.add(bulletblue)
# User let up on a key
elif event.type == pygame.KEYUP:
# If it is an arrow key, reset vector back to zero
if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
x_speed1 = 0
elif event.key == pygame.K_UP or event.key == pygame.K_DOWN:
y_speed1 = 0
if event.key == pygame.K_a or event.key == pygame.K_d:
x_speed2 = 0
elif event.key == pygame.K_w or event.key == pygame.K_s:
y_speed2 = 0
# Move the object according to the speed vector.
x_coord1 += x_speed1
y_coord1 += y_speed1
# Move the object according to the speed vector.
x_coord2 += x_speed2
y_coord2 += y_speed2
# --- Screen-clearing code goes here
# Here, we clear the screen to white. Don't put other drawing commands
# above this, or they will be erased with this command.
# If you want a background image, replace this clear with blit'ing the
# background image.
screen.fill(BLACK)
# Call the update() method on all the sprites
all_sprites_list.update()
all_sprites_list.draw(screen)
# --- Drawing code should go here
pygame.draw.rect(screen, RED, [x_coord1, y_coord1, 50, 50])
pygame.draw.rect(screen, BLUE, [x_coord2, y_coord2, 50, 50])
# --- Go ahead and update the screen with what we've drawn.
pygame.display.flip()
# --- Limit to 60 frames per second
clock.tick(60)
# Close the window and quit.
pygame.quit()
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team