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

3 error when switching from pygames to renpy #5

Merged
merged 15 commits into from
Mar 26, 2023
Merged
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
26 changes: 23 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
IMPORTANT: This is a continuation of a project not mine, abandoned from 2008 [Renpygame](https://renpy.org/wiki/renpy/frameworks/Renpygame)

# Renpygame

**IMPORTANT**: This is a continuation of a project not mine, abandoned from 2008 [Renpygame](https://renpy.org/wiki/renpy/frameworks/Renpygame)

----

Renpygame is a framework that allows pygame games to be integrated with Ren'Py. It's intended for people who are capable programmers. The philosophy of renpygame is to provide a minimal layer over pygame, and to rely on the programmer to reset things he changes.

To try out renpygame, download the renpygame demo from the [Frameworks](https://www.renpy.org/wiki/renpy/Frameworks) page, and unpack it inside a Ren'Py You can then select the renpygame-demo project from the launcher, launch it, and see an example of Ren'Py integrated with one of the pygame demo games.

To use renpygame in your own project, copy the renpygame directory from the base directory of the renpygame-demo project into the base directory of your own project. The base directory is the directory above the game directory, the directory that contains the game directory.

## Porting Pygame Code

To use renpygame, you need to change imports of pygame to imports of renpygame. For example, the code:

```renpy
Expand Down Expand Up @@ -45,6 +49,7 @@ init python:
```

## Supported Modules

The following modules are supported:

* renpygame.color
Expand All @@ -65,6 +70,21 @@ The following modules are supported:
* renpygame.time
* renpygame.transform


Functions that take a file have been modified so that the files are searched for in archives (except for Fonts), and in the game directory.

# Install Pygame

```bash
# install pip
sudo apt install python3-pip
# install dependencies https://www.pygame.org/wiki/CompileUbuntu?parent=
sudo apt-get install git python3-dev python3-setuptools python3-numpy python3-opengl \
libsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsmpeg-dev \
libsdl1.2-dev libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev \
libtiff5-dev libx11-6 libx11-dev fluid-soundfont-gm timgm6mb-soundfont \
xfonts-base xfonts-100dpi xfonts-75dpi xfonts-cyrillic fontconfig fonts-freefont-ttf libfreetype6-dev
# install pygame
pip install pygame
pip install Cython
# dowload https://github.com/renpy/pygame_sdl2/tree/master/src/pygame_sdl2
```
166 changes: 117 additions & 49 deletions game/aliens.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import random, os.path

#import basic pygame modules
import renpygame as pygame
from renpygame.locals import *
#import basic pygame_sdl2 modules
import pygame_sdl2 as pygame
pygame.import_as_pygame()
pygame._optional_imports()
from pygame_sdl2.rect import Rect

import renpy.store as store
import renpy.exports as renpy
import renpy.display.transform as transform

#see if we can load more than standard BMP
if not pygame.image.get_extended():
raise (SystemExit, "Sorry, extended image module required")

import math

def os_path_join(a, b):
return a + "/" + b
Expand All @@ -26,11 +30,9 @@ def os_path_join(a, b):

def load_image(file):
"loads an image, prepares it for play"
file = os_path_join('data', file)
try:
surface = pygame.image.load(file)
except pygame.error:
raise (SystemExit, 'Could not load image "%s" %s'%(file, pygame.get_error()))
file = os_path_join('images', file)
file = renpy.open_file(file)
surface = pygame.image.load(file)
return surface.convert()

def load_images(*files):
Expand All @@ -43,16 +45,6 @@ def load_images(*files):
class dummysound:
def play(self): pass

def load_sound(file):
if not pygame.mixer: return dummysound()
file = os_path_join('data', file)
try:
sound = pygame.mixer.Sound(file)
return sound
except pygame.error:
print ('Warning, unable to load,', file)
return dummysound()



# each type of game object gets an init and an
Expand Down Expand Up @@ -190,10 +182,6 @@ def main(winstyle = 0):
# Initialize pygame
pygame.init()

if pygame.mixer and not pygame.mixer.get_init():
print ('Warning, no sound')
pygame.mixer = None

# Set the display mode
if store._preferences.fullscreen:
winstyle = FULLSCREEN
Expand Down Expand Up @@ -227,14 +215,6 @@ def main(winstyle = 0):
screen.blit(background, (0,0))
pygame.display.flip()

#load the sound effects
boom_sound = load_sound('boom.wav')
shoot_sound = load_sound('car_door.wav')
if pygame.mixer:
music = os_path_join('data', 'house_lo.wav')
pygame.mixer.music.load(music)
pygame.mixer.music.play(-1)

# Initialize Game Groups
aliens = pygame.sprite.Group()
shots = pygame.sprite.Group()
Expand Down Expand Up @@ -269,27 +249,29 @@ def main(winstyle = 0):

while player.alive():

# TODO: has been commented pe make it work
#get input
for event in pygame.event.get():
if event.type == QUIT or \
(event.type == KEYDOWN and event.key == K_ESCAPE):
return SCORE
keystate = pygame.key.get_pressed()
# for event in pygame.event.get():
# if event.type == QUIT or \
# (event.type == KEYDOWN and event.key == K_ESCAPE):
# return SCORE
# keystate = pygame.key.get_pressed()

# clear/erase the last drawn sprites
all.clear(screen, background)

#update all the sprites
all.update()

# TODO: has been commented pe make it work
#handle player input
direction = keystate[K_RIGHT] - keystate[K_LEFT]
player.move(direction)
firing = keystate[K_SPACE]
if not player.reloading and firing and len(shots) < MAX_SHOTS:
Shot(player.gunpos())
shoot_sound.play()
player.reloading = firing
# direction = keystate[K_RIGHT] - keystate[K_LEFT]
# player.move(direction)
# firing = keystate[K_SPACE]
# if not player.reloading and firing and len(shots) < MAX_SHOTS:
# Shot(player.gunpos())
# shoot_sound.play()
# player.reloading = firing

# Create new alien
if alienreload:
Expand All @@ -304,19 +286,19 @@ def main(winstyle = 0):

# Detect collisions
for alien in pygame.sprite.spritecollide(player, aliens, 1):
boom_sound.play()
# boom_sound.play()
Explosion(alien)
Explosion(player)
SCORE = SCORE + 1
player.kill()

for alien in pygame.sprite.groupcollide(shots, aliens, 1, 1).keys():
boom_sound.play()
# boom_sound.play()
Explosion(alien)
SCORE = SCORE + 1

for bomb in pygame.sprite.spritecollide(player, bombs, 1):
boom_sound.play()
# boom_sound.play()
Explosion(player)
Explosion(bomb)
player.kill()
Expand All @@ -328,13 +310,99 @@ def main(winstyle = 0):
#cap the framerate
clock.tick(40)

if pygame.mixer:
pygame.mixer.music.fadeout(1000)
pygame.time.wait(1000)

# ! It's not work
# * https://github.com/DRincs-Productions/Renpygame/issues/3
# * renpytom tell me to use:
renpy.display_reset() # but not work

# ! It's not work:
# ! because when renpy try to edit the screen not find it
renpy.call("start")
# * It's work
# It's work
renpy.call("retry")

return SCORE







class Appearing(renpy.Displayable):

def __init__(
self,
image,
opaque_distance,
transparent_distance,
**kwargs
):

# Pass additional properties on to the renpy.Displayable
# constructor.
super(Appearing, self).__init__(**kwargs)

# The child.
self.image = renpy.displayable(image)

# The distance at which the child will become fully opaque, and
# where it will become fully transparent. The former must be less
# than the latter.
self.opaque_distance = opaque_distance
self.transparent_distance = transparent_distance

# The alpha channel of the child.
self.alpha = 0.0

# The width and height of us, and our child.
self.width = 0
self.height = 0

def render(self, width, height, st, at):

# Create a transform, that can adjust the alpha channel of the
# child.
t = transform.Transform(child=self.image, alpha=self.alpha)

# Create a render from the child.
child_render = renpy.render(t, width, height, st, at)

# Get the size of the child.
self.width, self.height = child_render.get_size()

# Create the render we will return.
render = renpy.Render(self.width, self.height)

# Blit (draw) the child's render to our render.
render.blit(child_render, (0, 0))

# Return the render.
return render

def event(self, ev, x, y, st):

# Compute the distance between the center of this displayable and
# the mouse pointer. The mouse pointer is supplied in x and y,
# relative to the upper-left corner of the displayable.
distance = math.hypot(x - (self.width / 2), y - (self.height / 2))

# Base on the distance, figure out an alpha.
if distance <= self.opaque_distance:
alpha = 1.0
elif distance >= self.transparent_distance:
alpha = 0.0
else:
alpha = 1.0 - 1.0 * (distance - self.opaque_distance) / (self.transparent_distance - self.opaque_distance)

# If the alpha has changed, trigger a redraw event.
if alpha != self.alpha:
self.alpha = alpha
renpy.redraw(self, 0)

# Pass the event to our child.
return self.image.event(ev, x, y, st)

def visit(self):
return [ self.image ]
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
9 changes: 9 additions & 0 deletions game/script.rpy
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ label start:
scene bg m64
show eileen happy at left

show screen alpha_magic

e "Welcome!"

e "You're here to defend the moon from invaders from the M-64 galaxy."
Expand Down Expand Up @@ -65,3 +67,10 @@ label retry:






screen alpha_magic:
add aliens.Appearing("background.gif", 100, 200):
xalign 0.5
yalign 0.5
Loading