Commit f5abf599 authored by Tomás Teijeiro Campo's avatar Tomás Teijeiro Campo

Minor changes for the final version

parent 0cdeb9aa
# -*- coding: utf-8 -*-
# pylint: disable-msg=
"""
Created on Tue May 3 14:01:45 2016
@author: T. Teijeiro
"""
#!/usr/bin/env python3
# Pong Example
# Written in 2013, 2014, 2015 by Julian Marchant <onpon4@riseup.net>
#
# To the extent possible under law, the author(s) have dedicated all
# copyright and related and neighboring rights to this software to the
# public domain worldwide. This software is distributed without any
# warranty.
#
# You should have received a copy of the CC0 Public Domain Dedication
# along with this software. If not, see
# <http://creativecommons.org/publicdomain/zero/1.0/>.
import random, os
import sge
DATA = os.path.join(os.path.dirname(__file__), "data")
PADDLE_XOFFSET = 32
PADDLE_SPEED = 4
PADDLE_VERTICAL_FORCE = 1 / 12.
BALL_START_SPEED = 2
BALL_ACCELERATION = 0.2
BALL_MAX_SPEED = 15
POINTS_TO_WIN = 6
TEXT_OFFSET = 16
game_in_progress = True
class Game(sge.dsp.Game):
def event_step(self, time_passed, delta_mult):
self.project_sprite(hud_sprite, 0, self.width / 2, 0)
def event_key_press(self, key, char):
global game_in_progress
if key == 'f8':
sge.gfx.Sprite.from_screenshot().save('screenshot.jpg')
elif key == 'f11':
self.fullscreen = not self.fullscreen
elif key == 'escape':
self.event_close()
elif key in ('p', 'enter'):
if game_in_progress:
self.pause()
else:
game_in_progress = True
self.current_room.start()
def event_close(self):
self.end()
def event_paused_key_press(self, key, char):
if key == 'escape':
# This allows the player to still exit while the game is
# paused, rather than having to unpause first.
self.event_close()
else:
self.unpause()
def event_paused_close(self):
# This allows the player to still exit while the game is paused,
# rather than having to unpause first.
self.event_close()
class Player(sge.dsp.Object):
score = 0
def __init__(self, player):
if player == 1:
self.joystick = 0
self.up_key = "w"
self.down_key = "s"
x = PADDLE_XOFFSET
self.hit_direction = 1
else:
self.joystick = 1
self.up_key = "up"
self.down_key = "down"
x = sge.game.width - PADDLE_XOFFSET
self.hit_direction = -1
y = sge.game.height / 2
super(Player, self).__init__(x, y, sprite=paddle_sprite,
checks_collisions=False)
def event_create(self):
self.score = 0
refresh_hud()
self.trackball_motion = 0
def event_step(self, time_passed, delta_mult):
# Movement
key_motion = (sge.keyboard.get_pressed(self.down_key) -
sge.keyboard.get_pressed(self.up_key))
axis_motion = sge.joystick.get_axis(self.joystick, 1)
if (abs(axis_motion) > abs(key_motion) and
abs(axis_motion) > abs(self.trackball_motion)):
self.yvelocity = axis_motion * PADDLE_SPEED
elif abs(self.trackball_motion) > abs(key_motion):
self.yvelocity = self.trackball_motion * PADDLE_SPEED
else:
self.yvelocity = key_motion * PADDLE_SPEED
self.trackball_motion = 0
# Keep the paddle inside the window
if self.bbox_top < 0:
self.bbox_top = 0
elif self.bbox_bottom > sge.game.current_room.height:
self.bbox_bottom = sge.game.current_room.height
def event_joystick_trackball_move(self, joystick, ball, x, y):
if joystick == self.joystick:
self.trackball_motion += y
class Ball(sge.dsp.Object):
def __init__(self):
x = sge.game.width / 2
y = sge.game.height / 2
super(Ball, self).__init__(x, y, sprite=ball_sprite)
def event_create(self):
self.serve()
def event_step(self, time_passed, delta_mult):
# Scoring
if self.bbox_right < 0:
player2.score += 1
refresh_hud()
#score_sound.play()
self.serve(-1)
elif self.bbox_left > sge.game.current_room.width:
player1.score += 1
refresh_hud()
#score_sound.play()
self.serve(1)
# Bouncing off of the edges
if self.bbox_bottom > sge.game.current_room.height:
self.bbox_bottom = sge.game.current_room.height
self.yvelocity = -abs(self.yvelocity)
#bounce_wall_sound.play()
elif self.bbox_top < 0:
self.bbox_top = 0
self.yvelocity = abs(self.yvelocity)
#bounce_wall_sound.play()
def event_collision(self, other, xdirection, ydirection):
if isinstance(other, Player):
if other.hit_direction == 1:
self.bbox_left = other.bbox_right + 1
else:
self.bbox_right = other.bbox_left - 1
self.xvelocity = min(abs(self.xvelocity) + BALL_ACCELERATION,
BALL_MAX_SPEED) * other.hit_direction
self.yvelocity += (self.y - other.y) * PADDLE_VERTICAL_FORCE
#bounce_sound.play()
def serve(self, direction=None):
global game_in_progress
if direction is None:
direction = random.choice([-1, 1])
self.x = self.xstart
self.y = self.ystart
if (player1.score < POINTS_TO_WIN and
player2.score < POINTS_TO_WIN):
# Next round
self.xvelocity = BALL_START_SPEED * direction
self.yvelocity = 0
else:
# Game Over!
self.xvelocity = 0
self.yvelocity = 0
hud_sprite.draw_clear()
x = hud_sprite.width / 2
p1text = "WIN" if player1.score > player2.score else "LOSE"
p2text = "WIN" if player2.score > player1.score else "LOSE"
hud_sprite.draw_text(hud_font, p1text, x - TEXT_OFFSET,
TEXT_OFFSET, color=sge.gfx.Color("white"),
halign="right", valign="top")
hud_sprite.draw_text(hud_font, p2text, x + TEXT_OFFSET,
TEXT_OFFSET, color=sge.gfx.Color("white"),
halign="left", valign="top")
game_in_progress = False
def create_room():
global player1
global player2
player1 = Player(1)
player2 = Player(2)
ball = Ball()
return sge.dsp.Room([player1, player2, ball], background=background)
def refresh_hud():
# This fixes the HUD sprite so that it displays the correct score.
hud_sprite.draw_clear()
x = hud_sprite.width / 2
hud_sprite.draw_text(hud_font, str(player1.score), x - TEXT_OFFSET,
TEXT_OFFSET, color=sge.gfx.Color("white"),
halign="right", valign="top")
hud_sprite.draw_text(hud_font, str(player2.score), x + TEXT_OFFSET,
TEXT_OFFSET, color=sge.gfx.Color("white"),
halign="left", valign="top")
# Create Game object
Game(width=1024, height=768, fps=120, window_text="Pong")
# Load sprites
paddle_sprite = sge.gfx.Sprite(width=8, height=48, origin_x=4, origin_y=24)
ball_sprite = sge.gfx.Sprite(width=8, height=8, origin_x=4, origin_y=4)
paddle_sprite.draw_rectangle(0, 0, paddle_sprite.width, paddle_sprite.height,
fill=sge.gfx.Color("white"))
ball_sprite.draw_rectangle(0, 0, ball_sprite.width, ball_sprite.height,
fill=sge.gfx.Color("white"))
hud_sprite = sge.gfx.Sprite(width=320, height=120, origin_x=160, origin_y=0)
# Load backgrounds
layers = [sge.gfx.BackgroundLayer(paddle_sprite, sge.game.width / 2, 0, -10000,
repeat_up=True, repeat_down=True)]
background = sge.gfx.Background(layers, sge.gfx.Color("black"))
# Load fonts
hud_font = sge.gfx.Font("Droid Sans Mono", size=48)
# Load sounds
#bounce_sound = sge.snd.Sound(os.path.join(DATA, 'bounce.wav'))
#bounce_wall_sound = sge.snd.Sound(os.path.join(DATA, 'bounce_wall.wav'))
#score_sound = sge.snd.Sound(os.path.join(DATA, 'score.wav'))
# Create rooms
sge.game.start_room = create_room()
sge.game.mouse.visible = False
if __name__ == '__main__':
sge.game.start()
\ No newline at end of file
# -*- coding: utf-8 -*-
# pylint: disable-msg=
"""
Created on Thu Apr 28 12:35:57 2016
@author: T. Teijeiro
"""
import sys, pygame
pygame.init()
size = width, height = 1320, 940
speed = [2, 2]
black = 0, 0, 0
screen = pygame.display.set_mode(size)
ball = pygame.image.load("ball.gif")
ballrect = ball.get_rect()
while 1:
for event in pygame.event.get():
if event.type == pygame.QUIT: sys.exit()
ballrect = ballrect.move(speed)
if ballrect.left < 0 or ballrect.right > width:
speed[0] = -speed[0]
if ballrect.top < 0 or ballrect.bottom > height:
speed[1] = -speed[1]
screen.fill(black)
screen.blit(ball, ballrect)
pygame.display.flip()
\ No newline at end of file
# -*- coding: utf-8 -*-
# pylint: disable-msg=
"""
Created on Thu Apr 28 12:26:24 2016
Example of the Pygame-SGE API: http://stellarengine.nongnu.org/index.html
@author: T. Teijeiro
"""
import random
import sge
PADDLE_XOFFSET = 32
PADDLE_SPEED = 4
PADDLE_VERTICAL_FORCE = 1 / 12.
BALL_START_SPEED = 2
BALL_ACCELERATION = 0.2
BALL_MAX_SPEED = 15
class Game(sge.dsp.Game):
def event_key_press(self, key, char):
global game_in_progress
if key == 'f8':
sge.gfx.Sprite.from_screenshot().save('screenshot.jpg')
elif key == 'f11':
self.fullscreen = not self.fullscreen
elif key == 'escape':
self.event_close()
elif key in ('p', 'enter'):
self.pause()
def event_close(self):
self.end()
def event_paused_key_press(self, key, char):
if key == 'escape':
# This allows the player to still exit while the game is
# paused, rather than having to unpause first.
self.event_close()
else:
self.unpause()
def event_paused_close(self):
# This allows the player to still exit while the game is paused,
# rather than having to unpause first.
self.event_close()
class Player(sge.dsp.Object):
def __init__(self, player):
if player == 1:
self.up_key = "w"
self.down_key = "s"
x = PADDLE_XOFFSET
self.hit_direction = 1
else:
self.up_key = "up"
self.down_key = "down"
x = sge.game.width - PADDLE_XOFFSET
self.hit_direction = -1
y = sge.game.height / 2
super(Player, self).__init__(x, y, sprite=paddle_sprite, checks_collisions=False)
def event_step(self, time_passed, delta_mult):
# Movement
key_motion = (sge.keyboard.get_pressed(self.down_key) -
sge.keyboard.get_pressed(self.up_key))
self.yvelocity = key_motion * PADDLE_SPEED
# Keep the paddle inside the window
if self.bbox_top < 0:
self.bbox_top = 0
elif self.bbox_bottom > sge.game.current_room.height:
self.bbox_bottom = sge.game.current_room.height
class Ball(sge.dsp.Object):
def __init__(self):
x = sge.game.width / 2
y = sge.game.height / 2
super(Ball, self).__init__(x, y, sprite=ball_sprite)
def event_create(self):
self.serve()
def event_step(self, time_passed, delta_mult):
# Scoring
if self.bbox_right < 0:
self.serve(-1)
elif self.bbox_left > sge.game.current_room.width:
self.serve(1)
# Bouncing off of the edges
if self.bbox_bottom > sge.game.current_room.height:
self.bbox_bottom = sge.game.current_room.height
self.yvelocity = -abs(self.yvelocity)
elif self.bbox_top < 0:
self.bbox_top = 0
self.yvelocity = abs(self.yvelocity)
def event_collision(self, other, xdirection, ydirection):
if isinstance(other, Player):
if other.hit_direction == 1:
self.bbox_left = other.bbox_right + 1
else:
self.bbox_right = other.bbox_left - 1
self.xvelocity = min(abs(self.xvelocity) + BALL_ACCELERATION,
BALL_MAX_SPEED) * other.hit_direction
self.yvelocity += (self.y - other.y) * PADDLE_VERTICAL_FORCE
def serve(self, direction=None):
if direction is None:
direction = random.choice([-1, 1])
self.x = self.xstart
self.y = self.ystart
# Next round
self.xvelocity = BALL_START_SPEED * direction
self.yvelocity = 0
# Create Game object
Game(width=640, height=480, fps=120, window_text="Pong")
# Load sprites
paddle_sprite = sge.gfx.Sprite(width=8, height=48, origin_x=4, origin_y=24)
ball_sprite = sge.gfx.Sprite(width=8, height=8, origin_x=4, origin_y=4)
paddle_sprite.draw_rectangle(0, 0, paddle_sprite.width, paddle_sprite.height,
fill=sge.gfx.Color("white"))
ball_sprite.draw_rectangle(0, 0, ball_sprite.width, ball_sprite.height,
fill=sge.gfx.Color("white"))
# Load backgrounds
layers = [sge.gfx.BackgroundLayer(paddle_sprite, sge.game.width / 2, 0, -10000,
repeat_up=True, repeat_down=True)]
background = sge.gfx.Background(layers, sge.gfx.Color("black"))
# Create objects
player1 = Player(1)
player2 = Player(2)
ball = Ball()
objects = [player1, player2, ball]
# Create rooms
sge.game.start_room = sge.dsp.Room(objects, background=background)
sge.game.mouse.visible = False
if __name__ == '__main__':
sge.game.start()
\ No newline at end of file
......@@ -58,12 +58,11 @@ class InvadersGame(sge.dsp.Game):
def show_hud(self):
self.clock.tick()
fps = int(self.clock.get_fps())
hud_string = 'SCORE: {0:03d} INVADERS: {1:03d} FPS: {2:03d}'
hud_string = 'SCORE: {0:03d} INVADERS: {1:03d}'
num_invaders = sum(1 for o in
self.current_room.objects if isinstance(o, objects.Invader))
self.project_text(self.hud_font, hud_string.format(self.score,
num_invaders, fps), 5, 5, anti_alias=False)
num_invaders), 5, 5, anti_alias=False)
if self.game_over:
self.project_text(sge.gfx.Font('minecraftia.ttf', size=70),
'Game\nOver', RESX/2, RESY/2 - 140, halign='center',
......
......@@ -9,8 +9,10 @@ Created on Tue May 3 18:34:45 2016
import sge
import game
import objects
import os
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.realpath("__file__")))
# Create Game object
game.InvadersGame()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment