Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Python pong example #179

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>org.yakindu.sct.examples.trafficlight.python</name>
<name>com.yakindu.sct.examples.pong.python</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.xtext.ui.shared.xtextBuilder</name>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
Expand All @@ -22,8 +22,8 @@
</buildCommand>
</buildSpec>
<natures>
<nature>org.yakindu.sct.builder.SCTNature</nature>
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
<nature>org.python.pydev.pythonNature</nature>
<nature>org.eclipse.xtext.ui.shared.xtextNature</nature>
<nature>org.yakindu.sct.builder.SCTNature</nature>
</natures>
</projectDescription>
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_pathproperty name="org.python.pydev.PROJECT_SOURCE_PATH">
<path>/${PROJECT_DIR_NAME}</path>
</pydev_pathproperty>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python 2.7</pydev_property>
</pydev_project>
10 changes: 10 additions & 0 deletions com.yakindu.sct.examples.pong.python/code_gen.sgen
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GeneratorModel for yakindu::python {

statechart pong_npc {

feature Outlet {
targetProject = "com.yakindu.sct.examples.pong.python"
targetFolder = ""
}
}
}
58 changes: 58 additions & 0 deletions com.yakindu.sct.examples.pong.python/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import pygame
from pong.pong_ui import PongUI
from pong.pong_game import PongGame
from pong_npc.pong_npc_statemachine import Pong_npc

if __name__ == "__main__":
pong_game = PongGame()
ui = PongUI(pygame, pong_game)
pong_npc = Pong_npc()
pong_npc.init()
pong_npc.enter()

def loop():
pressed_up_w = False
pressed_down_w = False
while True:
for event in pygame.event.get():
if event.type is pygame.QUIT:
return False
if event.type is pygame.KEYDOWN:
if event.key is pygame.K_w:
pong_game.move(pong_game.UP, pong_game.PLAYER_ONE)
pressed_up_w = True
if event.key is pygame.K_s:
pong_game.move(pong_game.DOWN, pong_game.PLAYER_ONE)
pressed_down_w = True

keys = pygame.key.get_pressed()
if pressed_up_w:
pressed_up_w = False
else:
if keys[pygame.K_w]:
pong_game.move(pong_game.UP, pong_game.PLAYER_ONE)

if pressed_down_w:
pressed_down_w = False
else:
if keys[pygame.K_s]:
pong_game.move(pong_game.DOWN, pong_game.PLAYER_ONE)

pong_npc.sci_ball.direction = pong_game.ball_direction
pong_npc.sci_ball.position = pong_game.ball_pos[1] + PongUI.BALL_RADIUS/2
pong_npc.sci_player.position = pong_game.player2_position + PongUI.PLAYER_HEIGHT / 2

print(pong_npc.sci_player.position)

pong_npc.run_cycle()

if(pong_npc.sci_interface.is_raised_up()):
pong_game.move(pong_game.UP, pong_game.PLAYER_TWO)
if(pong_npc.sci_interface.is_raised_down()):
pong_game.move(pong_game.DOWN, pong_game.PLAYER_TWO)

pong_game.update_ball()
ui.update()

loop()
pygame.quit()
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
75 changes: 75 additions & 0 deletions com.yakindu.sct.examples.pong.python/pong/pong_game.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
from pong.pong_ui import PongUI
from random import uniform
import math


class PongGame:
UP = 'up'
DOWN = 'down'
PLAYER_ONE = 1
PLAYER_TWO = 2
RIGHT = 'right'
LEFT = 'left'

def __init__(self):
starting_position = PongUI.get_starting_position()
self.player1_position = starting_position
self.player2_position = starting_position
self.ball_pos = [int((PongUI.WINDOW_WIDTH / 2.)), int(PongUI.WINDOW_HEIGHT / 2)]
self.ball_direction = self.RIGHT
self.ball_angle = 0
self.ball_speed = 20

def move(self, direction, player):
if player == self.PLAYER_ONE and not self.border_collision(self.player1_position, direction):
if direction == self.UP:
self.player1_position -= 10
if direction == self.DOWN:
self.player1_position += 10

if player == self.PLAYER_TWO and not self.border_collision(self.player2_position, direction):
if direction == self.UP:
self.player2_position -= 10
if direction == self.DOWN:
self.player2_position += 10

def border_collision(self, position, direction):
if position == 0 and direction == self.UP:
return True
if position == PongUI.WINDOW_HEIGHT - PongUI.PLAYER_HEIGHT and direction == self.DOWN:
return True
return False

def check_for_collisions(self):
if self.ball_pos[1] - PongUI.BALL_RADIUS <= 0 or self.ball_pos[1] + PongUI.BALL_RADIUS >= PongUI.WINDOW_HEIGHT:
self.ball_angle *= -1
if self.ball_direction == self.RIGHT:
if self.player2_position <= self.ball_pos[1] <= self.player2_position + PongUI.PLAYER_HEIGHT:
if PongUI.WINDOW_WIDTH - PongUI.PLAYER_OFFSET >= self.ball_pos[0] >= \
PongUI.WINDOW_WIDTH - PongUI.PLAYER_OFFSET - PongUI.PLAYER_WIDTH:
self.ball_direction = self.LEFT
self.update_angle_after_hit()

if self.ball_direction == self.LEFT:
if self.player1_position <= self.ball_pos[1] <= self.player1_position + PongUI.PLAYER_HEIGHT:
if PongUI.PLAYER_OFFSET <= self.ball_pos[0] <= PongUI.PLAYER_OFFSET + PongUI.PLAYER_WIDTH:
self.ball_direction = self.RIGHT
self.update_angle_after_hit()

def update_angle_after_hit(self):
self.ball_angle = int(uniform(-30, 30))

def update_ball(self):
self.check_for_collisions()
angle_r = math.radians(self.ball_angle)
dx = int(math.cos(angle_r) * self.ball_speed)
dy = int(math.sin(angle_r) * self.ball_speed)
if self.ball_direction == self.RIGHT:
self.ball_pos[0] = self.ball_pos[0] + dx
self.ball_pos[1] = self.ball_pos[1] - dy
if self.ball_direction == self.LEFT:
self.ball_pos[0] = self.ball_pos[0] - dx
self.ball_pos[1] = self.ball_pos[1] + dy

def restart(self):
self.__init__()
44 changes: 44 additions & 0 deletions com.yakindu.sct.examples.pong.python/pong/pong_ui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class PongUI:
WHITE = (255, 255, 255)
BLACK = (0, 0, 0)
WINDOW_WIDTH = 1200
WINDOW_HEIGHT = 700
PLAYER_WIDTH = 20
PLAYER_HEIGHT = 100
PLAYER_OFFSET = 100
BALL_RADIUS = 20
SIZE = (WINDOW_WIDTH, WINDOW_HEIGHT)

def __init__(self, pygame, pong_game):
self.pygame = pygame
self.window = pygame.display.set_mode(self.SIZE)
self.timer = pygame.time.Clock()
self.pong_game = pong_game
self.rect_player1 = None
self.rect_player2 = None

def draw_player(self):
self.rect_player1 = (self.PLAYER_OFFSET - self.PLAYER_WIDTH/2, self.pong_game.player1_position, self.PLAYER_WIDTH, self.PLAYER_HEIGHT)
self.pygame.draw.rect(self.window, self.WHITE, self.rect_player1)

self.rect_player2 = (
self.WINDOW_WIDTH - self.PLAYER_OFFSET - self.PLAYER_WIDTH/2, self.pong_game.player2_position, self.PLAYER_WIDTH,
self.PLAYER_HEIGHT)
self.pygame.draw.rect(self.window, self.WHITE, self.rect_player2)

def draw_ball(self):
self.pygame.draw.circle(self.window, self.WHITE, self.pong_game.ball_pos, self.BALL_RADIUS)

@staticmethod
def get_starting_position():
return PongUI.WINDOW_HEIGHT / 2 - PongUI.PLAYER_HEIGHT / 2

def update(self):
self.window.fill(self.BLACK)
self.draw_player()
self.draw_ball()
self.pygame.display.update()
self.timer.tick(30)

def restart(self):
self.__init__()
Binary file added com.yakindu.sct.examples.pong.python/pong_npc.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading