rect5 = pygame.Rect(rect5x, rect5y, 50, 50) rect6 = pygame.Rect(rect6x, rect6y, 50, 50) spaceship = pygame.Rect(300, 175, 50, 50) speed1 = 5 speed2 = 5 speed3 = 5 win = 0 while running and not done: window.fill((0, 0, 0)) rect1.x += speed1 rect2.x += speed2 rect3.x += speed3 pygame.draw.rect(window, (122, 122, 122), rect1) pygame.draw.rect(window, (122, 122, 122), rect2) pygame.draw.rect(window, (122, 122, 122), rect3) if tsk.get_key_pressed(pygame.K_UP): spaceship.y -= 10 if tsk.get_key_pressed(pygame.K_DOWN): spaceship.y += 10 if tsk.get_key_pressed(pygame.K_LEFT): spaceship.x -= 10 if tsk.get_key_pressed(pygame.K_RIGHT): spaceship.x += 10 for event in pygame.event.get(): if event.type == pygame.QUIT: done = False if spaceship.y < -30: spaceship.y += 380 if spaceship.y > 370: spaceship.y -= 380 if rect1.x > 400:
for enemy in enemies: # animate all enemies. enemy[0] += enemy[3] enemy[1] += enemy[4] if enemy[0] <= 0: enemy[3] *= -1 if enemy[0] >= SCREEN_WIDTH: enemy[3] *= -1 # draw enemies. pygame.draw.circle(w, (0, 0, 0), (int(enemy[0]), int(enemy[1])), 20) pygame.draw.circle(w, (255, 0, 0), (int(enemy[0]), int(enemy[1])), 10) # check if player is sprinting speedfactor = 1 if tsk.get_key_pressed(pygame.K_RSHIFT) or tsk.get_key_pressed(pygame.K_LSHIFT): speedfactor = 2 # check if player should be moved if tsk.get_key_pressed(pygame.K_a): if playerx >= 0: playerx -= 5 * speedfactor if tsk.get_key_pressed(pygame.K_d): if playerx <= SCREEN_WIDTH: playerx += 5 * speedfactor # draw the player playerRekt = pygame.Rect(playerx - 10, playery - 10, 40, 40) pygame.draw.rect(w, (150, 150, 150), playerRekt) # flip the display pygame.display.flip()
P1_plus="backward" elif P1_plus == "backward": P1_plus="up" elif P1_plus == "up": P1_plus="forward" if event.key ==pygame.K_SLASH: if P2_plus == "forward": P2_plus="down" elif P2_plus == "down": P2_plus="backward" elif P2_plus == "backward": P2_plus="up" elif P2_plus == "up": P2_plus="forward" # MOVING if tsk.get_key_pressed(pygame.K_d): P1.x+=10 if P1.x > 950: P1.x=950 if tsk.get_key_pressed(pygame.K_a): P1.x-=10 if P1.x < 0: P1.x=0 if tsk.get_key_pressed(pygame.K_w): P1.y-=10 if P1.y < 0: P1.y=0 if tsk.get_key_pressed(pygame.K_s): P1.y+=10 if P1.y > 950: P1.y=950
while running: for event in pygame.event.get(): if event.type == pygame.QUIT: running = False break w.fill((223, 223, 223)) # check for player movement commands. # recalculate robot pose based on commands. # if there is a collision with a wall (check each point), then undo the movement with the previous pose. cache_player_pose() robot_y_angle = playerrot - (math.pi / 4) robot_x_angle = playerrot + (math.pi / 4) half_player_diagonal = PLAYER_DIAGONAL_SIZE / (2.0 * FEET_PER_PIXEL) if tsk.get_key_pressed(pygame.K_o): playerrot -= MAX_ANGULAR_SPEED / fps if tsk.get_key_pressed(pygame.K_p): playerrot += MAX_ANGULAR_SPEED / fps if tsk.get_key_pressed(pygame.K_w): playerx += (MAX_Y_SPEED / (FEET_PER_PIXEL * fps)) * math.cos(robot_y_angle) playery += (MAX_Y_SPEED / (FEET_PER_PIXEL * fps)) * math.sin(robot_y_angle) elif tsk.get_key_pressed(pygame.K_s): playerx += (MAX_Y_SPEED / (FEET_PER_PIXEL * fps)) * math.cos(robot_y_angle - math.pi) playery += (MAX_Y_SPEED / (FEET_PER_PIXEL * fps)) * math.sin(robot_y_angle - math.pi) elif tsk.get_key_pressed(pygame.K_a): playerx += (MAX_X_SPEED /
score = 0 mistakes = 0 drawing = True while drawing: for event in pygame.event.get(): if event.type == pygame.QUIT: drawing = False if event.type == pygame.KEYDOWN: if tsk.get_key_pressed( pygame.K_RIGHT ) and arrow == "right" or tsk.get_key_pressed( pygame.K_LEFT) and arrow == "left" or tsk.get_key_pressed( pygame.K_DOWN ) and arrow == "down" or tsk.get_key_pressed( pygame.K_UP) and arrow == "up": arrow_color = "green" score += 1 else: mistakes += 1 if arrow_timer >= arrow_limit:
# Create an EVENT LOOP that gets all events for event in pygame.event.get(): # If the event TYPE is QUIT if event.type == pygame.QUIT: # Assign done the value True # ---> TEST AFTER THIS LINE <--- # done = True # --- Spacebar pressed --- # # Elif event.type is equal to KEYDOWN # and event.key is equal to SPACE elif tsk.get_key_pressed(pygame.KEYDOWN) and tsk.get_key_pressed( pygame.K_SPACE): # --- Check if fly is in the box --- # # Assign in_x the value False in_x = False # Assign in_y the value False in_y = False # If fly.x - box.x is greater than or equal to 0 # and fly.x - box.x is less than or equal to 80 if fly.x - box.x >= 0 and fly.x - box.x <= 80: # Assign in_x the value True
w.fill((0, 0, 0)) snail_1_y = 550 snail_1 = pygame.Rect(50, snail_1_y, 40, 50) pygame.draw.ellipse(w, (255, 0, 0), snail_1) snail_2_y = 550 snail_2 = pygame.Rect(200, snail_2_y, 40, 50) pygame.draw.ellipse(w, (0, 0, 255), snail_2) animating = True while animating: for event in pygame.event.get(): if event.type == pygame.QUIT: animating = False if tsk.get_key_pressed(pygame.K_w): snail_1_y -= 3 snail_1 = pygame.Rect(50, snail_1_y, 40, 50) pygame.draw.ellipse(w, (255, 0, 0), snail_1) if tsk.get_key_pressed(pygame.K_UP): snail_2_y -= 3 snail_2 = pygame.Rect(200, snail_2_y, 40, 50) pygame.draw.ellipse(w, (0, 0, 255), snail_2) pygame.time.wait(10) pygame.display.flip() if snail_1_y <= 1:
while wait: w.fill([255, 255, 255]) font.render_to(w, (0, 0), "Press W, A, S or D to start first wave.") font.render_to( w, (0, 20), "Use [W][A][S][D] keys to move, and the mouse to aim. Left-click to fire." ) font.render_to( w, (0, 40), "Hold [SHIFT] to sprint. Use [1] and [2] to switch fire modes.") for event in pygame.event.get(): if event.type == pygame.QUIT: wait = False running = False if tsk.get_key_pressed(pygame.K_w) or tsk.get_key_pressed( pygame.K_s) or tsk.get_key_pressed( pygame.K_a) or tsk.get_key_pressed(pygame.K_d): wait = False for enemy in enemies: # draw enemies. r = 0 g = 0 half_health = enemy[3] / 2 enemy_health = enemy[2] if enemy_health > half_health: g = 255 * ((enemy_health - half_health) / half_health) else: r = 255 * ((half_health - enemy_health) / half_health) pygame.draw.circle(w, (int(r), int(g), 0),