3dc++双人枪战

张开发
2026/4/16 9:13:42 15 分钟阅读

分享文章

3dc++双人枪战
未完成 #includebits/stdc.h #includestack WHITE (255,255,255) BLACK (0,0,0) RED (255,50,50) BLUE (50,50,255) GREEN (0,255,0) GRAY (100,100,100) PLAYER_SIZE 30 BULLET_SPEED 15 BULLET_RADIUS 5 PLAYER_SPEED 5 MAX_HEALTH 100 class Player: def __init__(self, x, y, color, key_up, key_down, key_left, key_right, key_shoot): self.x x self.y y self.color color self.speed PLAYER_SPEED self.health MAX_HEALTH self.kills 0 self.key_up key_up self.key_down key_down self.key_left key_left self.key_right key_right self.key_shoot key_shoot self.angle 0 def move(self, keys): if keys[self.key_up] and self.y 0: self.y - self.speed if keys[self.key_down] and self.y HEIGHT - PLAYER_SIZE: self.y self.speed if keys[self.key_left] and self.x 0: self.x - self.speed if keys[self.key_right] and self.x WIDTH//2 - PLAYER_SIZE: self.x self.speed def update_angle_mouse(self): mx, my pygame.mouse.get_pos() dx mx - self.x dy my - self.y self.angle math.atan2(dy, dx) def update_angle_key(self): keys pygame.key.get_pressed() if keys[pygame.K_UP]: self.angle -math.pi/2 if keys[pygame.K_DOWN]: self.angle math.pi/2 if keys[pygame.K_LEFT]: self.angle math.pi if keys[pygame.K_RIGHT]: self.angle 0 def draw(self, surface): pygame.draw.rect(surface, self.color, (self.x, self.y, PLAYER_SIZE, PLAYER_SIZE)) end_x self.x PLAYER_SIZE//2 math.cos(self.angle) * 40 end_y self.y PLAYER_SIZE//2 math.sin(self.angle) * 40 pygame.draw.line(surface, WHITE, (self.xPLAYER_SIZE//2, self.yPLAYER_SIZE//2), (end_x, end_y), 2) def draw_health(self, surface, x, y): pygame.draw.rect(surface, RED, (x, y, 150, 20)) pygame.draw.rect(surface, GREEN, (x, y, 150 * (self.health/MAX_HEALTH), 20)) class Bullet: def __init__(self, x, y, angle, color, owner): self.x x self.y y self.angle angle self.color color self.speed BULLET_SPEED self.owner owner def update(self): self.x math.cos(self.angle) * self.speed self.y math.sin(self.angle) * self.speed def draw(self, surface): pygame.draw.circle(surface, self.color, (int(self.x), int(self.y)), BULLET_RADIUS) def check_collision(self, player): dist math.hypot(self.x - (player.xPLAYER_SIZE//2), self.y - (player.yPLAYER_SIZE//2)) return dist PLAYER_SIZE//2 BULLET_RADIUS p1 Player(100, HEIGHT//2, RED, pygame.K_w, pygame.K_s, pygame.K_a, pygame.K_d, pygame.BUTTON_LEFT) p2 Player(WIDTH-100-PLAYER_SIZE, HEIGHT//2, BLUE, pygame.K_UP, pygame.K_DOWN, pygame.K_LEFT, pygame.K_RIGHT, pygame.K_RCTRL) bullets [] running True shoot_cooldown 300 last_shoot_p1 0 last_shoot_p2 0 while running: clock.tick(FPS) screen.fill(BLACK) keys pygame.key.get_pressed() current_time pygame.time.get_ticks() for event in pygame.event.get(): if event.type pygame.QUIT: running False p1.move(keys) p1.update_angle_mouse() if pygame.mouse.get_pressed()[0] and current_time - last_shoot_p1 shoot_cooldown: last_shoot_p1 current_time bullet Bullet(p1.xPLAYER_SIZE//2, p1.yPLAYER_SIZE//2, p1.angle, RED, p1) bullets.append(bullet) p2.move(keys) p2.update_angle_key() if keys[pygame.K_RCTRL] and current_time - last_shoot_p2 shoot_cooldown: last_shoot_p2 current_time bullet Bullet(p2.xPLAYER_SIZE//2, p2.yPLAYER_SIZE//2, p2.angle, BLUE, p2) bullets.append(bullet) for bullet in bullets[:]: bullet.update() if bullet.x 0 or bullet.x WIDTH or bullet.y 0 or bullet.y HEIGHT: bullets.remove(bullet) continue if bullet.owner p1 and bullet.check_collision(p2): p2.health - 10 bullets.remove(bullet) if p2.health 0: p1.kills 1 p2.health MAX_HEALTH p2.x, p2.y WIDTH-100-PLAYER_SIZE, HEIGHT//2 if bullet.owner p2 and bullet.check_collision(p1): p1.health - 10 bullets.remove(bullet) if p1.health 0: p2.kills 1 p1.health MAX_HEALTH p1.x, p1.y 100, HEIGHT//2 pygame.draw.line(screen, GRAY, (WIDTH//2, 0), (WIDTH//2, HEIGHT), 3) p1.draw(screen) p2.draw(screen) for b in bullets: b.draw(screen) p1.draw_health(screen, 20, 20) p2.draw_health(screen, WIDTH-170, 20) font pygame.font.SysFont(None, 40) screen.blit(font.render(f击杀: {p1.kills}, True, RED), (20, 50)) screen.blit(font.render(f击杀: {p2.kills}, True, BLUE), (WIDTH-170, 50)) pygame.display.flip() pygame.quit()

更多文章