-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtwo_player_game.py
More file actions
101 lines (81 loc) · 2.58 KB
/
two_player_game.py
File metadata and controls
101 lines (81 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# Copyright (c) 2017 Jon Cooper
#
# This file is part of pygame-xbox360controller.
# Documentation, related files, and licensing can be found at
#
# <https://github.com/joncoop/pygame-xbox360controller>.
import pygame
import xbox360_controller
pygame.init()
# define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
RED = (255, 0, 0)
# window settings
size = [600, 600]
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Simple Game")
FPS = 60
clock = pygame.time.Clock()
# make a controller
controller1 = xbox360_controller.Controller()
controller2 = xbox360_controller.Controller()
print(controller1.get_id(), controller2.get_id())
# make a ball
ball_1_pos = [250, 290]
ball_1_radius = 10
ball_1_color = WHITE
ball_2_pos = [330, 290]
ball_2_radius = 10
ball_2_color = WHITE
# settings
MAX_SPEED = 5
# game loop
playing = False
running = True
while running:
# event handling
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
if event.type == pygame.JOYBUTTONDOWN:
# handle events for all controllers
if not playing:
if event.button == xbox360_controller.START:
playing = True
else:
if event.button == xbox360_controller.BACK:
playing = False
ball_1_pos = [250, 290]
ball_2_pos = [330, 290]
# handle events for specific controllers
if event.joy == controller1.get_id():
if event.button == xbox360_controller.A:
if ball_1_color == WHITE:
ball_1_color = RED
else:
ball_1_color = WHITE
elif event.joy == controller2.get_id():
if event.button == xbox360_controller.A:
if ball_2_color == WHITE:
ball_2_color = RED
else:
ball_2_color = WHITE
# handle joysticks
x1, y1 = controller1.get_left_stick()
x2, y2 = controller2.get_left_stick()
# game logic
if playing:
ball_1_pos[0] += int(x1 * MAX_SPEED)
ball_1_pos[1] += int(y1 * MAX_SPEED)
ball_2_pos[0] += int(x2 * MAX_SPEED)
ball_2_pos[1] += int(y2 * MAX_SPEED)
# drawing
screen.fill(BLACK)
pygame.draw.circle(screen, ball_1_color, ball_1_pos, ball_1_radius)
pygame.draw.circle(screen, ball_2_color, ball_2_pos, ball_2_radius)
# update screen
pygame.display.flip()
clock.tick(FPS)
# close window on quit
pygame.quit ()