ãåŸ ã£ãŠãäœïŒã-ã»ãšãã©ã®èªè ã¯ãã®ããã«ã¿ã€ãã«ã«åå¿ãããšæããŸãã
ããã©ãŠã¶ã§Pythonã䜿ãã ãããšããæå³ã§ããïŒ
JavaScriptã ãããã©ãŠã¶ã§æ©èœããããšã¯èª°ããç¥ã£ãŠããŸãã
ããŠãäžèšã¯ç§ã®å人ãµã€ãã®ãœãŒã¹ã³ãŒãã®ã¹ã¯ãªãŒã³ã·ã§ããã§ããèŠãŠãã ãããå€åããªãã¯ããªãèªèº«ã®ããã«äœãæ°ãããã®ãèŠãã§ãããã
ã¯ããPythonã§ãïŒ
ããã§ã¯ããããã©ã®ããã«ããããŠã©ãã ãããŸãæ©èœãããã«ã€ããŠè©±ãããŸãä»ã®ããã€ãã®JavaScriptã®ä»£æ¿æ¡ã«ã€ããŠã話ãåããŸãããã
ãã©ã€ãã³ã®çŽ¹ä»
Brythonã¯ãããªãããŠã§ãçšã®Pythonã³ãŒããæžãããšãã§ããŸãã®python3ã®JavaScriptå®è£ ã§ãã
åºæ¬çã«ãããã¯Pythonã³ãŒããåçã®JSã«å€æããå®è¡æã«å®è¡ããJavaScriptã©ã€ãã©ãªã§ãã
Pythonã§ãã©ãŠã¶ã³ãŒããæžãã®ã¯ãã£ããããšæãã®ã§ãè©ŠããŠã¿ãããšã«ããŸããã
ãã©ã€ãã³ã§ã®ãã¹ããŒã¯ãã®éçº
ããã¯ãSnakeã®JavaScriptããŒãžã§ã³ãšBrythonããŒãžã§ã³ãè©Šãããšãã§ããç§ã®ãµã€ããžã®ãªã³ã¯ã§ãããããŠãããã«ãœãŒã¹ã³ãŒããå«ãGitHubãžã®ãªã³ã¯ããããŸãã
ãã©ã€ãã³ãè©Šãããã«ãç§ã¯å€å žçãªã¹ããŒã¯ãæžãããšã«ããŸããã
ç§ã¯HTMLCanvasã®å°é家ã§ãã²ãŒã éçºè ã§ããªãã®ã§ããã®JavaScriptå®è£ ãåºçºç¹ãšããŠäœ¿çšããããšã«ããŸããã Canvasã«åºã¥ããŠãSnakeããäœæããããšããããŸããããã®å®è£ ã¯ãããã£ãããšã³ã³ãã¯ãã«ãªã£ãŠããŸããèè ã¯ãŸãã5åæªæºã§ãããæžããŸãããã¯ãªã¹ã»ãã¬ãªã³ã®å瞟ãèªããªããã°ãªããŸããããšãŠãå°è±¡çã§ãã
ããã§ãChrisã®å®è£ ã«ã¹ã³ã¢ãªã³ã°ãšæé«ã¹ã³ã¢ã®ä¿åãè¿œå ããã€ã³ã¿ãŒãã§ã€ã¹ããããã«æ¹åããŸããïŒäžæåæ¢ãã¿ã³ãšæ瀺ä»ãã®ãã¿ã³ãè¿œå ããŸããïŒã次ã«ãã²ãŒã ãBrythonã«ç§»æ€ããŸãããã¯ãªã¹ã®å®è£ ã¯æé»ã®ã°ããŒãã«å€æ°ã®ãããªãã®ã䜿çš
ããŠããã®ã§ãç§ã¯åœŒã®ã³ãŒããå€æŽããŠãã¢ãŒãã§åäœ
strict
ããããã«ããŸããã ïŒã BrythonãšJSã³ãŒããããæ¯èŒãããã£ãã®ã§ãã
JavaScriptã¯ãã®ãããªãã®ã§ããããšãå€æããŸãããããã§ã¯ãã®ã³ãŒããæçš¿ããªãã®ã§ãç§ãã¡ã®ç®æšã¯Brythonã«çŠç¹ãåœãŠãããšã§ãã
ã»ãšãã©ã®Brythonã³ãŒãã¯æåéãJSãã翻蚳ãããŸããããäžéšã®éšåïŒã¹ã³ã¢ãªã³ã°æ©èœãªã©ïŒã¯Brythonã§çŽæ¥èšè¿°ãããéãã確èªããããã«JSã«å®è£ ãããŸããã
æçµçµæã¯æ¬¡ã®ããã«ãªããŸãã
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brython Snake</title>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/brython@3.8.9/brython.min.js">
</script>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style> /* Removed to keep the snippet short. Find the full file here: */ </style>
</head>
<body onload="brython()">
<h1 class="text-center">Snake built with <a href="https://brython.info">Python!</a></h1>
<canvas id="game-board" width="400" height="400"></canvas>
<br>
<h3 id="score" class="text-center">Score: 0</h3>
<br>
<h6 id="high-score" class="text-center">High Score: 0</h6>
<br>
<div class="text-center">
<button id="instructions-btn" class="btn btn-info">Instructions</button>
</div>
<script type="text/python">
from browser import document, html, window
from javascript import Math
score = 0
high_score = 0
px = py = 10
gs = tc = 20
ax = ay = 15
xv = yv = 0
trail = []
tail = 5
pre_pause = [0,0]
paused = False
def game():
global px, py, tc, gs, ax, ay, trail, tail, score
px += xv
py += yv
if px < 0:
px = tc-1
if px > tc-1:
px = 0
if py < 0:
py = tc-1
if py > tc-1:
py = 0
ctx.fillStyle = "black"
ctx.fillRect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = "lime"
for i in range(len(trail)):
ctx.fillRect(trail[i][0]*gs, trail[i][1]*gs, gs-2, gs-2)
if trail[i][0] == px and trail[i][1] == py:
score = score if paused else 0
tail = 5
trail.insert(0, [px, py])
while len(trail) > tail:
trail.pop()
if ax == px and ay == py:
tail += 1
ax = Math.floor(Math.random()*tc)
ay = Math.floor(Math.random()*tc)
score += 1
update_score(score)
ctx.fillStyle = "red"
ctx.fillRect(ax*gs, ay*gs, gs-2, gs-2)
def update_score(new_score):
global high_score
document["score"].innerHTML = "Score: " + str(new_score)
if new_score > high_score:
document["high-score"].innerHTML = "High Score: " + str(new_score)
high_score = new_score
def key_push(evt):
global xv, yv, pre_pause, paused
key = evt.keyCode
if key == 37 and not paused:
xv = -1
yv = 0
elif key == 38 and not paused:
xv = 0
yv = -1
elif key == 39 and not paused:
xv = 1
yv = 0
elif key == 40 and not paused:
xv = 0
yv = 1
elif key == 32:
temp = [xv, yv]
xv = pre_pause[0]
yv = pre_pause[1]
pre_pause = [*temp]
paused = not paused
def show_instructions(evt):
window.alert("Use the arrow keys to move and press spacebar to pause the game.")
canvas = document["game-board"]
ctx = canvas.getContext("2d")
document.addEventListener("keydown", key_push)
game_loop = window.setInterval(game, 1000/15)
instructions_btn = document["instructions-btn"]
instructions_btn.addEventListener("click", show_instructions)
</script>
</body>
</html>
ãããã£ãŠããã®ã¹ããããã«åºã¥ããŠãããã€ãã®åºæ¬çãªBrythonã®æŠå¿µãç解ããŸãããã
Brython.jsæ¥ç¶
Brythonã䜿çšããããã«ã€ã³ã¹ããŒã«ã¯å¿ èŠãããŸãããäžã«ã¹ã¯ãªãããã€ã³ããŒãããã ã
head
ã§ãïŒ
<script type=âtext/javascriptâ src=âhttps://cdn.jsdelivr.net/npm/brython@3.8.9/brython.min.js">
ãã©ã€ãã³ãå®è¡ããŠããŸã
BrythonãPythonã³ãŒããJSã³ãŒãã§ãããã®ããã«å€æããŠå®è¡ãã
Brython
ã«ã¯ãããã¥ã¡ã³ãã®æ¬æãèªã¿èŸŒãŸãããšãã«åŒã³åºãå¿
èŠããããŸããããšãã°ã次ã®ããã«ãªããŸãã
<body onload=âbrython()â>
ãã®ã¿ã°ã¯ã
script
ã¿ã€ãã®ã¿ã°ãæ€çŽ¢ã"text/python"
ããããã®ã³ãŒããå®è¡ããŸãã
Webãæäœããããã®API
JavaScriptã¯ããã©ã«ãã§ãJSãããžã§ã¯ãã®ãããªãªããžã§ã¯ã
document
ãwindow
å¿
èŠãªãªããžã§ã¯ããžã®ã¢ã¯ã»ã¹ãæäŸããŸãããããã£ãŠããã©ã€ãã³ã圌ããšååã§ããã¯ãã§ãã
ãã®åé¡ã解決ããããã«ãBrythonã®äœæè ã¯ãéçºè ã«Pythonã³ãŒããããããã®ãªããžã§ã¯ãã«ã¢ã¯ã»ã¹ããæ©èœãäžããããšãã§ããŸãããããã¯ãããã¬ãŒã®å«ã³
undefined variable
ãšããã©ãŒãã³ã¹ã®äœäžã«ã€ãªãããŸãã
ãããã£ãŠããããã®APIã䜿çšããã«ã¯ãä»ã®Pythonã¢ãžã¥ãŒã«ãã€ã³ããŒãããã®ãšåãæ¹æ³ã§APIãã€ã³ããŒãããå¿ èŠããããŸãã
from browser import document, html, window
ãŸããã³ãã³ããå®è¡ããå¿ èŠã¯ãããŸãã
pip install
ãçµå±ã®ãšãããããªãã¯ããããã¹ãŠHTMLã«åã蟌ã¿ãŸãïŒå¿
èŠãªã€ã³ããŒããè¿œå ããã ãã§ãBrythonãæ®ããåŠçããŸãã
ïŒããã¯ã©ã®ããã«åäœããããã確èªããã«ã¯ãç§ã¯ãAPIã®Webã®ããã€ãã®ç°ãªãæ¹æ³ã䜿çšããŠã¿ãŸãã
alert
ãsetInterval
ãaddEventListener
ãªã© ãããã¯ãã¹ãŠæ£åžžã«æ©èœããŸããã
çµã¿èŸŒã¿ã®JavaScriptãªããžã§ã¯ããšã¡ãœãã
ã¹ããŒã¯ã§ã¯ãããããªã³ãŽãé£ã¹ããšããã«ãã©ã³ãã ãªå Žæã«æ°ãããªã³ãŽãçæããå¿ èŠããããŸãã
ãã ããPython *ã©ã€ãã©ãªã®ã©ã³ãã ã¢ãžã¥ãŒã«ã䜿çšããããšã¯ã§ããŸãããã§ã¯ãã©ãããã°ïŒç¬èªã®ã©ã€ãã©ãªãäœæããã«ïŒã©ã³ãã ãªçªå·ãçæã§ããŸããïŒ
Brythonã¯ç§ãæã£ãŠãããããå¹ åºãJavaScriptãµããŒããæã£ãŠããããšãããããŸãããèŠãïŒ
from javascript import Math
random_num = Math.floor(Math.random()*10)
ã¢ãžã¥ãŒã«ã®ãããã§ã
javascript
JSã䜿çšããŠã¢ã¯ã»ã¹ã§ãããªããžã§ã¯ããããå Žåã¯ãBrythonã䜿çšããŠã¢ã¯ã»ã¹ã§ããŸãã
JavaScriptã©ã€ãã©ãªïŒjQueryãBootstrapïŒãã€ã³ããŒããããã®ã¡ãœããã䜿çšãããå Žåã¯ãã䜿çšããŠå®è¡ã§ããŸã
from javascript import <>
ããã¡ãããDate
ãã®ãããªçµã¿èŸŒã¿ã®JSãªããžã§ã¯ãã䜿çšããããšãã§ããŸãString
ã
* Brythonã«ã¯ãJavaScriptã§çŽæ¥å®è£ ãããå€æ°ã®æšæºPythonã©ã€ãã©ãªãä»å±ããŠããããã§ããã¢ãžã¥ãŒã«ã«JSããŒãžã§ã³ããªãå Žåã§ããã€ã³ããŒãã§ããŸããBrythonã¯çŽç²ãªPythonããŒãžã§ã³ãååŸããã€ã³ããŒããããã¢ãžã¥ãŒã«ã³ãŒãã¯Brythonã³ãŒããšäžç·ã«æ©èœããŸãããã ããã©ã³ãã ã¢ãžã¥ãŒã«ã¯æ©èœããŸããã§ãããããã®çç±ã¯ç解ã§ããŸãã
ç¹å®ã®æ§é
Pythonã§ã¯ããªã¹ãã解åãããå Žåã¯ããšæžãããšãã§ããŸã
list2 = [*list1]
ããŸããäœããã®æ¡ä»¶ã«åºã¥ããŠå€æ°ã«å€ãå²ãåœãŠããå Žåã¯ããæžãããšãã§ããŸãfoo = 10 if condition else 20
ã
ãããã®ã³ã³ã¹ãã©ã¯ãã«ã¯ãJavaScriptã«çžåœããspreadïŒ
[...arr]
ïŒæŒç®åãšternaryïŒlet foo = condition ? 10 : 20
ïŒæŒç®åããããŸãã
ãããããã©ã€ãã³ã¯ãããããµããŒãããŠããŸããïŒ
ç§ã¯ããããè©ŠããŸããããããŠã圌ãã¯ããŸããããŸãããç§ã®ã³ãŒãã§ã¯ãPythonããã®ãªã¹ãã®ããã¯ã¹å解é€ãšæ¡ä»¶ä»ãå²ãåœãŠã䜿çšãããŠããããšãããããŸãã
ãããã°
æ£çŽãªãšããããã©ã€ãã³ã§ã®ãããã°ã¯ã²ã©ããã®ã ãšæããŸããã
å®éãããã»ã©æªãã¯ãããŸããã
ãã¡ãããç§ã¯éåžžã«å°ãããããã»ã©è€éã§ã¯ãªããããžã§ã¯ããäœæããŸããããBrythonã«ãã£ãŠã¹ããŒããããšã©ãŒã¯ã»ãšãã©æ£ç¢ºã§ãéåžžã«ç解ãããããã®ã§ããã
ããã¯ãå°ãªããšãæ§æãšã©ãŒã«ã€ããŠã¯åœãŠã¯ãŸããŸããPythonã©ã€ãã©ãªããã¢ãžã¥ãŒã«ãã€ã³ããŒãããããšã¯ããŸã£ããå¥ã®è©±ã§ãã
ããã©ãŒãã³ã¹
JavaScript Snake
Brython Snake
äºæ³éããBrythonã³ãŒãã¯JavaScriptãããäœéã§ããç§ã®å Žåãããã¯çŽ1.7åé ãã£ãã
ããè€éãªãããžã§ã¯ãã§ã¯ãBrythonã¯çŽç²ãªJSãããæ°åé ããªããšæããŸãã
ãã ããäºåã«Brythonã³ãŒãããã©ã³ã¹ãã€ã«ããããŒãžã§JavaScriptã®ã¿ã䜿çšããããšãã§ããŸãã
ç§ã¯å®éã«BrythonEditorã䜿çšããŠBrythonã³ãŒããJSã«å€æããçµæã®ã³ãŒããWebããŒãžã§å®è¡ããããšããŸãããããšã©ãŒãéåžžã«å€ããããä»ã®ãšããããããããããŸãããããããç§ã¯ããã«ããŸãåãå ¥ããŠããŸããã
ãã©ã€ãã³ã«é¢ããæçµçãªèã
æ£çŽããã©ã€ãã³ã«ã¯ãšãŠãæåããŸããããããç§èªèº«ã®èšèªã®çµéšããã®ããã€ãã®è³åŠäž¡è«ã§ãïŒ
è³åŠäž¡è«
- äžå¿ èŠãªæéããããã«ãSnakeããæžãããšãã§ããé©ãã»ã©ååããªãããã°äœéšãã§ããŸããã
- ç§ã®åçŽãªãããžã§ã¯ãã§ã¯ãBrythonã¯ããŒãžã§å©çšå¯èœãªãã€ãã£ãJavaScriptãªããžã§ã¯ããšã·ãŒã ã¬ã¹ã«å¯Ÿè©±ããŸãã
- ç§ã®ã³ãŒããPythonã§ãããã«èŠãããšããäºå®ã«æè¬ããŸãããŸãã䟿å©ãªPythonæ§é ã䜿çšããŠãã©ãŠã¶ãŒã³ãŒããèšè¿°ã§ããããšãæ°ã«å ¥ã£ãŠããŸãã
- ç§ã®ã²ãŒã ã®å ŽåãBrythonã®èªã¿èŸŒã¿ã¯JavaScriptãããé ããªããŸããããŠãŒã¶ãŒã¯ãã®éãã«æ°ã¥ããŸããã
- ç§ã®ãµã€ãã®ãœãŒã¹ã³ãŒãã«Pythonãå«ãŸããŠããã®ãèŠãŠããããæããŸãã
ãã€ãã¹
- ãã©ã€ãã³ã¯çŽç²ãªJSãããå€§å¹ ã«äœéã§ãã
- Brython JavaScript.
- Brython
- Brython .
äžè¬çã«ããã©ã€ãã³ã§ã®æåã®ãããžã§ã¯ããçµããã®ã§ããã€ããŸãããçŽããšèªä¿¡ãæã£ãŠèšããŸãã
ãã ããBrythonã¯ãJavaScriptãåŠç¿ããã«Webéçºãè¡ãããPythonéçºè ããããPythonã«ç²ŸéããŠããŠJSã«ããããããŠããJavaScriptéçºè ã«é©ããŠãããšæããŸãã
BrythonãšããŸãé£æºããã«ã¯ãJavaScriptãç解ããããšãäžå¯æ¬ ã ãšæããŸãããŸããæéããããŠJavaScriptãåŠç¿ããŠãBrythonã§ã®èšè¿°ã容æã«ããããšã«ããå Žåã¯ãJavaScriptã䜿çšã§ããŸãã
ä»ã®ãã©ãŠã¶JSã®ä»£æ¿
ç§ãBrythonãéžãã çç±ã¯ãç§ãæåã«ç¥ã£ãPythonããJSãžã®ç§»è¡ãªãã·ã§ã³ã®ã»ãšãã©ãåå ã§ãGitHubã§ç©æ¥µçã«éçºããŠããã®ã¯Brythonã ãã§ãããç§ãèŠãPythonããJavaScriptãžã®ãã©ã³ã¹ãã€ã©ãŒã®ã»ãšãã©ã¯ãæ°å¹Žéã³ããããããŠããŸããã
ãã ããä»ã®éžæè¢ããããŸããããšãã°ã
Pyodideã¯èå³æ·±ããªãã·ã§ã³ã®ããã§ãã PythonãïŒãã®ç§åŠã©ã€ãã©ãªãšãšãã«ïŒWebAssemblyã«ã³ã³ãã€ã«ãããã©ãŠã¶ã§å®è¡ã§ããããã«ããŸãã
WebAssemblyã¯ããã®ååã瀺ãããã«ãWebã®ã¢ã»ã³ãã©ãŒã§ããã³ã³ãã¥ãŒã¿ãŒäžã®ã¢ã»ã³ãã©ãŒãé«ã¬ãã«ã®èšèªãšãã·ã³ã³ãŒãã®éã®ä»²ä»åœ¹ãšããŠæ©èœã§ããããã«ãWebAssemblyã¯Webäžã§ãåãããšãè¡ããŸãã
ãããã£ãŠãPythonïŒãŸãã¯ãã®ä»ã®èšèªïŒãWebAssemblyã«å€æããŠããã©ãŠã¶ãŒã§å®è¡ã§ããããã«ããã³ã³ãã€ã©ãŒãäœæããããšãã§ããŸãã
ããã¯éå¿çã§ææãªãããžã§ã¯ãã§ãããJavaScriptã䜿çšããªãWebéçºããŸããŸãå¢ãããšããäºå®ã«ã€ãªããå¯èœæ§ããããŸãã
ãã ãããŸã åæ段éïŒã3幎ïŒã§ãããããJavaScriptãå®æçã«ä»ã®èšèªã«çœ®ãæãããããŸã§ã«ã¯ããããæéããããã§ãããã
ãããŠãç§ãã¡ãåŸ ã€éãæ¬åœã«JavaScriptãæ±ãããšãã§ããªãå Žåã¯ãBrythonã®ãããªããŒã«ã䜿çšããå¿ èŠããããŸãã
ãããæ£çŽãªãšãããããã¯è¯ãã¹ã¿ãŒãã§ãïŒ
SkillFactoryã®ææãªã³ã©ã€ã³ã³ãŒã¹ãåè¬ããŠã泚ç®ãéããè·æ¥ããŒãããååŸããæ¹æ³ããŸãã¯ã¹ãã«ãšçµŠäžãã¬ãã«ã¢ããããæ¹æ³ã®è©³çŽ°ãã芧ãã ããã
- - (8 )
- «Python -» (9 )
- UX- (9 )
- Web- (7 )
- Data Science (12 )
- (9 )
- Machine Learning (12 )
- DevOps (12 )