-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgamegpt.html
185 lines (157 loc) · 5.66 KB
/
gamegpt.html
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Simple Game</title>
<style>
body {
margin: 0;
overflow: hidden;
}
canvas {
background-color: #ccc;
display: block;
}
</style>
</head>
<body>
<canvas id="gameCanvas"></canvas>
<script>
const canvas = document.getElementById('gameCanvas');
const ctx = canvas.getContext('2d');
const player = {
x: 50,
y: 50,
speed: 2,
radius: 10
};
const camera = {
x: 0,
y: 0,
width: canvas.width,
height: canvas.height,
boundingBox: {
left: canvas.width * 0.3,
right: canvas.width * 0.7,
top: canvas.height * 0.3,
bottom: canvas.height * 0.7
}
};
const elements = []; // Array to store other elements
// Add some random elements to the scene
for (let i = 0; i < 20; i++) {
elements.push({
x: Math.random() * 500,
y: Math.random() * 500,
size: Math.random() * 20 + 10,
visible: false // Initially not visible
});
}
function updateCamera() {
// Calculate the camera's position to keep the player centered
camera.x = player.x - camera.width / 2;
camera.y = player.y - camera.height / 2;
// Check if the player is near the edges of the bounding box
if (player.x < camera.boundingLeft) {
camera.x = player.x - camera.boundingLeft;
} else if (player.x > camera.boundingRight) {
camera.x = player.x - (camera.width - camera.boundingRight);
}
if (player.y < camera.boundingTop) {
camera.y = player.y - camera.boundingTop;
} else if (player.y > camera.boundingBottom) {
camera.y = player.y - (camera.height - camera.boundingBottom);
}
}
function draw() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Translate the canvas to adjust for the camera position
ctx.save();
ctx.translate(-camera.x, -camera.y);
// Draw visible elements in the scene
ctx.fillStyle = 'green';
elements.forEach(element => {
if (isElementVisible(element)) {
ctx.fillRect(element.x, element.y, element.size, element.size);
// Check for collision with visible elements
if (isColliding(player, element)) {
// Implement the bounce effect
const angle = Math.atan2(player.y - element.y, player.x - element.x);
player.x += Math.cos(angle) * player.speed;
player.y += Math.sin(angle) * player.speed;
}
}
});
// Draw the player at its world position
ctx.beginPath();
ctx.arc(player.x, player.y, player.radius, 0, Math.PI * 2);
ctx.fillStyle = 'blue';
ctx.fill();
ctx.closePath();
// Restore the canvas transformation
ctx.restore();
}
function isElementVisible(element) {
return (
element.x + element.size > camera.x &&
element.x < camera.x + camera.width &&
element.y + element.size > camera.y &&
element.y < camera.y + camera.height
);
}
function isColliding(obj1, obj2) {
const dx = obj1.x - obj2.x;
const dy = obj1.y - obj2.y;
const distance = Math.sqrt(dx * dx + dy * dy);
return distance < obj1.radius + obj2.size / 2;
}
function gameLoop() {
updateCamera();
draw();
if (isMoving) {
movePlayer();
}
requestAnimationFrame(gameLoop);
}
let isMoving = false;
var mouseX = 0;
var mouseY = 0;
canvas.addEventListener('mousedown', () => {
isMoving = true;
});
canvas.addEventListener('touchstart', (e) => {
isMoving = true;
const touch = e.touches[0];
mouseX = touch.clientX - canvas.getBoundingClientRect().left + camera.x;
mouseY = touch.clientY - canvas.getBoundingClientRect().top + camera.y;
});
canvas.addEventListener('touchend', () => {
isMoving = false;
});
canvas.addEventListener('mouseup', () => {
isMoving = false;
});
canvas.addEventListener('mousemove', (e) => {
mouseX = e.clientX - canvas.getBoundingClientRect().left + camera.x;
mouseY = e.clientY - canvas.getBoundingClientRect().top + camera.y;
if (isMoving) {
movePlayer();
}
});
canvas.addEventListener('touchmove', (e) => {
const touch = e.touches[0];
mouseX = touch.clientX - canvas.getBoundingClientRect().left + camera.x;
mouseY = touch.clientY - canvas.getBoundingClientRect().top + camera.y;
if (isMoving) {
movePlayer();
}
});
function movePlayer() {
const angle = Math.atan2(mouseY - player.y, mouseX - player.x);
player.x += Math.cos(angle) * player.speed;
player.y += Math.sin(angle) * player.speed;
}
gameLoop();
</script>
</body>
</html>