Python `vis` module err . Python Solar System - python

Hi I am trying to get the following github repo : https://github.com/lukekulik/solar-system
to work on my laptop and have installed all the modules required but for some reason when i try and run i get error cannot import module vis in from vis import ....
It is in every file and i a using python 3.6.
The code for one file is below:
from vis import scene, label, vector, norm, cross
Error is here when trying to import vis and below when trying to import wx
#from pygame.mixer import music
from parameters import planets, n, am
from wx import Exit
lbl = label(text=" Simulation is paused. \n Press any key to continue.",
visible=False, box=False)
# key_check function translates user keyboard into commands for the program:
def key_check(ship_mode, s, v, a, dt, sw_lbl, lbl_off, predraw, earthpos, t,
ship):
if scene.kb.keys: # check if there are any keys pressed in the queue
key = scene.kb.getkey() # retrieve the pressed key
# change simulation speed: (steps are small on purpose; user has to
press and hold for smooth transition)
if key == 'q' and 1 <= dt < 125:
dt += 2
elif key == 'q' and dt <= 0.95:
dt += 0.05
elif key == 'a' and dt > 1:
dt -= 2
elif key == 'a' and 0.2 < dt <= 1:
dt -= 0.05
elif key == 'esc':
Exit() # exit the program
elif key == 'p':
lbl.visible = True # show pause label
lbl.pos = scene.center # center the label on the screen
scene.kb.getkey() # wait for key input
lbl.visible = False # hide pause label
elif key == 'x':
if scene.stereo == 'nostereo':
scene.stereo = 'redcyan' # turn on 3D rendering mode (red-cyan)
elif scene.stereo == 'redcyan':
scene.stereo = 'crosseyed' # turn on 3D rendering mode (cross-eyed)
else:
scene.stereo = 'nostereo' # turn off 3D rendering mode
elif key == 'l':
sw_lbl = not sw_lbl # planet/spaceship label switch
lbl_off = not lbl_off
elif key == 's':
#if ship_mode:
# music.fadeout(2500) # fade out music when quitting spaceship mode
# else:
# music.play(-1) # turn on music when entering spaceship mode
ship_mode = not ship_mode # spaceship mode switch
a = vector(0, 0, 0) # reset acceleration
sw_lbl = True # turn on spaceship label
# spaceship thrusters control:
elif key == 'up':
#a += vector(0, 0, am) # old cartesian steering
a += am*norm(v) # accelerate into the direction of motion
elif key == 'down':
a -= am*norm(v) # accelerate against the direction of motion
elif key == 'left':
a -= am*cross(norm(v),cross(norm(v),norm(s))) # accelerate to the left of the velocity
elif key == 'right':
a += am*cross(norm(v),cross(norm(v),norm(s))) # accelerate to the right of the velocity
elif key == 'd':
a += am*cross(norm(v),norm(s)) # accelerate to the 'top' of the velocity
elif key == 'c':
a -= am*cross(norm(v),norm(s)) # accelerate to the 'bottom' of the velocity
elif key == '0':
a = vector(0, 0, 0) # reset acceleration
# pre-programmed spaceship positions/scenarios:
elif key == '1': # Earth-Sun Lagrange point 3 (L3 - 'counter-Earth')
s = -earthpos
v0 = (planets[2][1][:, (int(-t + 1)) % n] - planets[2][1][:, (int(-t)) % n]) / (365.25 * 86400 / n)
v = vector(v0[0], v0[1], v0[2])
a = vector(0, 0, 0)
predraw = False
ship.trail.append(pos=s, retain=1)
elif key == '2': # polar orbit around the Sun
s = vector(3e+07, -2e+06, 2e+08)
v = vector(0, 24, 0)
a = vector(0, 0, 0)
predraw = False
ship.trail.append(pos=s, retain=1)
elif key == '3': # orbit of the probe Helios 2 (fastest man-made object to date - 0.02% speed of light)
s = vector(43e6, 0, 0) # perihelion
v = vector(0, 0, 70.22)
a = vector(0, 0, 0)
predraw = False
ship.trail.append(pos=s, retain=1)
elif key == '4': # Halley's comet (derived using specific orbital energy, 162.3 deg inclination)
s = vector(87813952, 0, 0) # perihelion
v = vector(0, 16.7, 52)
a = vector(0, 0, 0)
predraw = False
ship.trail.append(pos=s, retain=1)
return ship_mode, s, v, a, dt, sw_lbl, lbl_off, predraw, ship
def mouse_check(obj, sw_lbl, lbl_off, ship_mode):
if scene.mouse.clicked and not ship_mode: # check if there are any
clicks
in the queue
scene.mouse.getclick() # retrieve the click
obj_old = obj
obj = scene.mouse.pick # clicked object becomes main object
try:
r = obj.radius
except AttributeError:
obj = obj_old # if the clicked object is not a sphere (e.g. Saturn's rings), go back to old_obj
else:
if r > 30000000000: # if the clicked object is a very large sphere (celestial), go back to old_obj
obj = obj_old
if obj != obj_old and not lbl_off: # if different planet was clicked, switch planet label ON
sw_lbl = True
scene.center = obj.pos # center scene on the main object
return obj, sw_lbl`
Thanks in advance.

Related

TypeError: 'InputDevice' object is not iterable

Im going to paste my entire function here before the question.
def trackBoth():
#if event determination variable is 1 it has both K and M if it is 2 it is only K or M if 3 then none
x_max = 800 #defines largest position mouse can go (19th LED)
x_min = -800 #defines smallest position mouse can go (9th LED)
y_max = 800
y_min = -800
try:
keyboardEvent = evdev.InputDevice('/dev/input/event0') #setting keyboard to USB port
mouseEvent = evdev.InputDevice('/dev/input/event1') #setting mouse to USB port
event_det = 1
except:
try:
keyboardEvent = evdev.InputDevice('/dev/input/event0')
event_det = 2
except:
try:
keyboardEvent = evdev.InputDevice('/dev/input/event1')
event_det = 2
except:
event_det = 3
NoKeyboardMouse()
async def print_events(device):
global x, y #must be inside of loop or else nonlocal error is thrown
x = 0 #defines the start of x
y = 0 #defines the start of y
async for event in device.async_read_loop():
if event.type == ecodes.EV_REL: #if the event is a mouse moving
if event.code == ecodes.REL_X: #chechking for x direction
print("REL_X", event.value)
x += event.value #advance the cursor position
#every 80 increments advance 1 led
if event.code == ecodes.REL_Y: #checking for y direction
print("REL_Y", event.value)
y += event.value * -1 #advance the cursor position
print(x, y)
if event.type == ecodes.EV_KEY: #if the event is a button click
c = categorize(event)
if c.keystate == c.key_down: #only reading key for down press not up
print(c.keycode)
if "BTN_LEFT" in c.keycode: #if it is a left mouse click go to check where the cursor is
checkPosition()
if c.keycode == "BTN_RIGHT": #if it is a right click override the current display
rightClick()
if c.keycode == "KEY_1":
ser.write(str.encode('1'))
if c.keycode == "KEY_2":
ser.write(str.encode('2'))
if c.keycode == "KEY_3":
ser.write(str.encode('3'))
if c.keycode == "KEY_4":
ser.write(str.encode('4'))
if c.keycode == "KEY_5":
ser.write(str.encode('5'))
if c.keycode == "KEY_6":
ser.write(str.encode('6'))
if c.keycode == "KEY_7":
ser.write(str.encode('7'))
if c.keycode == "KEY_8":
ser.write(str.encode('8'))
if c.keycode == "KEY_9":
ser.write(str.encode('9'))
if c.keycode == "KEY_0":
ser.write(str.encode('0'))
if c.keycode == "KEY_S":
ser.write(str.encode('s'))
if c.keycode == "KEY_G":
ser.write(str.encode('g'))
if (event_det == 1):
for device in keyboardEvent, mouseEvent:
asyncio.ensure_future(print_events(device))
if (event_det == 2):
for device in keyboardEvent:
asyncio.ensure_future(print_events(device))
loop = asyncio.get_event_loop()
loop.run_forever()
basically my issue is in this section
if (event_det == 1):
for device in keyboardEvent, mouseEvent:
asyncio.ensure_future(print_events(device))
if (event_det == 2):
for device in keyboardEvent:
asyncio.ensure_future(print_events(device))
when i try to run this code assuming event_det is equal to 2 i get this error
Traceback (most recent call last):
File "etho.py", line 569, in <module>
trackBoth()
File "etho.py", line 559, in trackBoth
for device in keyboardEvent:
TypeError: 'InputDevice' object is not iterable
assuming i had both a keyboard and mouse plugged in it would run fine (event_det = 1). Why is it giving me the error when it only attempts to run one event (keyboardEvent) how should i change my code here.

Low frame rate, possibly because un-accelerated graphics

So I began creating a game a couple months ago using pygame, it has a top view similar to Starcraft, Age of empires etc... I have written around 1320 lines of code to create the basis of my game; however, I am experiencing issues with frame rate when blitting images and believe this is because I cannot use accelerated graphics with pygame. The way I am currently blitting images is by blitting all images ahead of time on a surface which I then subsurface to create a blit image of my entire screen. Is there a more effective way that i should be utilizing?
So my assumption is that would be huge mess to look through and I do not want to waste your guy's time. Essentially any time I blit a surface the size of my screen my framerate drops by ~20 frames, is there a way I can avoid this in pygame?
##PYGAME INITATE##
import pygame, os
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
_W,_H = pygame.display.Info().current_w, pygame.display.Info().current_h
flags = pygame.DOUBLEBUF | pygame.HWSURFACE
gameDisplay = pygame.display.set_mode((_W,_H),pygame.FULLSCREEN ) ## CREATES SCREEN YOU DISPLAY ON ##
gameDisplay.fill((0,0,0)) ## FILL COLOR OF SCREEN ##
pygame.display.set_caption("Dope Game") ## SETS NAME ##
gameClock = pygame.time.Clock() ## CLOCK OF THE GAME ##
import math
import os
import random
import copy
SQRT = math.sqrt
PI = math.pi
cos = math.cos
sin = math.sin
## REPEATABLE FUNCTIONS ##
def loadScale(file,command,sizeX,sizeY):
temp = pygame.image.load(file)
tempInfo = temp.get_rect()
tempInfo1,tempInfo2,tempInfo3,tempInfo4 = temp.get_rect()
tempInfo3 = int(tempInfo3)
tempInfo4 = int(tempInfo4)
if (command == "ratio"):
tempInfo3 = tempInfo3*sizeX
tempInfo4 = tempInfo4*sizeY
temp = pygame.transform.scale(temp,(int(tempInfo3),int(tempInfo4) ) )
elif (command == "size"):
temp = pygame.transform.scale(temp, (sizeX,sizeY) )
return(temp)
## NON GAME RELATED CLASSES ##
class EnterFrame():
def __init__(self,frameReset,function,parse,reset):
self.frameReset = frameReset
self.currentFrame = frameReset
self.function = function
self.parse = parse
self.reset = reset
if (self.reset != "onComplete"):
self.reset = (reset-1)
enterFrameTable.append(self)
def step(self,enterFrameTable):
if (self.currentFrame == 0):
self.function(self.parse)
if (self.reset != "onComplete"):
if (self.reset > 0):
self.currentFrame = self.frameReset
self.reset = self.reset-1
else:
enterFrameTable.remove(self)
del self
else:
self.currentFrame = self.frameReset
else:
self.currentFrame = self.currentFrame-1
class PlayerCreation():
def __init__(self):
self.x = _W
self.y = _H
self.view = [1600,1600]
self.viewShift = []
self.viewChangeSpeed = 25
def moveView(self,key):
add = EnterFrame(0,self.moveViewAction,key,"onComplete")
self.viewShift.append([add,key])
def moveViewAction(self,key):
if (key == "up"):
self.view[1] = self.view[1]-self.viewChangeSpeed
Map.recenterView()
if (self.view[1] < 0):
self.view[1] = 0
elif (key == "right"):
self.view[0] = self.view[0]+self.viewChangeSpeed
Map.recenterView()
if (self.view[0] > Map.tileSize*4):
self.view[0] = Map.tileSize*4
elif (key == "down"):
self.view[1] = self.view[1]+self.viewChangeSpeed
Map.recenterView()
if (self.view[1] > Map.tileSize*4):
self.view[1] = Map.tileSize*4
elif (key == "left"):
self.view[0] = self.view[0]-self.viewChangeSpeed
Map.recenterView()
if (self.view[0] < 0):
self.view[0] = 0
def endMoveView(self,key):
for i in range(len(self.viewShift)-1,-1,-1 ):
if (self.viewShift[i][1] == key):
enterFrameTable.remove(self.viewShift[i][0])
del self.viewShift[i]
class ImageCreation():
def __init__(self,name,image,type,hitBox):
self.name = name
self.image = image
self.type = type
self.hitBox = hitBox
self.rect = self.image.get_rect()
if (self.hitBox != "none"):
self.shiftX = hitBox[0][0]
self.shiftY = hitBox[0][1]
for i in range(1,len(hitBox) ):
if (hitBox[i][0] < self.shiftX):
self.shiftX = hitBox[i][0]
if (hitBox[i][1] < self.shiftY):
self.shiftY = hitBox[i][1]
else:
self.shiftX = self.rect[2]/2
self.shiftY = self.rect[3]/2
imageTable.append(self)
def draw(self,x,y):
image = self.image
self.blit = gameDisplay.blit(image,(x,y) )
class MapCreation():
def __init__(self):
self.tileSize = 800
self.size = self.tileSize*10
self.tiles = []
self.loadedTiles = []
self.surface = pygame.Surface([self.tileSize*5,self.tileSize*5], pygame.SRCALPHA, 32)
self.centerTile = [5,5]
self.drawPoint = [(_W-self.tileSize)/2,(_H-self.tileSize)/2]
self.amount = round(self.size/self.tileSize)
backGround = loadScale("Grass.png","size",self.tileSize,self.tileSize)
for i in range(0,self.amount):
for u in range(0,self.amount):
image = copy.copy(backGround)
newTile = Tile(image,[u*self.tileSize,i*self.tileSize])
self.tiles.append(newTile)
info = imageFind("House.png")
tile = self.tiles[55]
imageObject(info,tile,[240,50])
self.loadTiles("center")
def recenterView(self):
if (Player.view[0] > 3*self.tileSize):
self.centerTile[0] = self.centerTile[0]+1
Player.view[0] = Player.view[0]-self.tileSize
self.loadTiles("right")
print("right")
elif (Player.view[0] < 1*self.tileSize):
self.centerTile[0] = self.centerTile[0]-1
Player.view[0] = Player.view[0]+self.tileSize
self.loadTiles("center")
print("center")
if (Player.view[1] > 3*self.tileSize):
self.centerTile[1] = self.centerTile[1]+1
Player.view[1] = Player.view[1]-self.tileSize
self.loadTiles("center")
print("center")
elif (Player.view[1] < 1*self.tileSize):
self.centerTile[1] = self.centerTile[1]-1
Player.view[1] = Player.view[1]+self.tileSize
self.loadTiles("center")
print("center")
def loadTiles(self,load):
tileIndex = self.centerTile[0]+self.centerTile[1]*self.amount
if (load == "center"):
self.loadedTiles = []
for i in range(-2,3):
for u in range(-2,3):
loadTile = tileIndex + i*self.amount + u
self.loadedTiles.append(self.tiles[loadTile])
self.tiles[loadTile].loaded = [u+2,i+2]
self.surface = pygame.Surface([self.tileSize*5,self.tileSize*5], pygame.SRCALPHA, 32)
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*self.tileSize
sy = self.loadedTiles[i].loaded[1]*self.tileSize
self.surface.blit(self.loadedTiles[i].buttomLayer,(sx,sy) )
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*(self.tileSize+2)
sy = self.loadedTiles[i].loaded[1]*(self.tileSize+2)
self.surface.blit(self.loadedTiles[i].middleLayer,(sx,sy) )
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*(self.tileSize+2)
sy = self.loadedTiles[i].loaded[1]*(self.tileSize+2)
self.surface.blit(self.loadedTiles[i].topLayer,(sx,sy) )
elif (load == "right"):
self.loadedTiles = []
for i in range(-2,3):
for u in range(-2,3):
loadTile = tileIndex + i*self.amount + u
self.loadedTiles.append(self.tiles[loadTile])
self.tiles[loadTile].loaded = [u+2,i+2]
## OLD METHOD THAT WASNT WORKING ##
##subSurf = self.surface.subsurface(self.tileSize,0,self.tileSize*1,self.tileSize*5)
##self.surface = pygame.Surface([self.tileSize*5,self.tileSize*5], pygame.SRCALPHA, 32)
##self.surface.blit(subSurf,(0,0) )
## NEW METHOD ##
self.surface.scroll(dx=-self.tileSize*1,dy=0)
def draw(self):
global Player
image = self.surface.subsurface(Player.view[0],Player.view[1],self.tileSize,self.tileSize)
image = pygame.transform.scale(image,(self.tileSize,self.tileSize) )
gameDisplay.blit(image,((_W-self.tileSize)/2,(_H-self.tileSize)/2) )
image = pygame.transform.scale(self.surface,(300,300) )
gameDisplay.blit(image,(0,0 ) )
class Tile():
def __init__(self,image,coords):
transparentSurface = pygame.Surface([1000,1000], pygame.SRCALPHA, 32)
self.x = coords[0]
self.y = coords[1]
self.loaded = False
self.buttomLayer = image
self.middleLayer = copy.copy(transparentSurface)
self.topLayer = transparentSurface
class imageObject():
def __init__(self,info,tile,coords):
self.info = info
self.image = info.image
self.rect = self.image.get_rect()
self.x = coords[0]
self.y = coords[1]
self.hitBox = []
if (self.info.hitBox != "none"):
for i in range(0,len(self.info.hitBox) ):
self.hitBox.append([self.info.hitBox[i][0]+self.x,self.info.hitBox[i][1]+self.y])
#self.object = createObject(self.hitBox,True,"none")
if (info.type == "background"):
tile.buttomLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object"):
tile.middleLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object alphas"):
tile.topLayer.blit(self.image,(self.x,self.y) )
def imageFind(name):
for i in range(0,len(imageTable) ):
if (name == imageTable[i].name):
return(imageTable[i])
return("none")
def imageLoad(types):
if (types == "basic"):
image = loadScale("House.png","ratio",1,1)
basicHouse = ImageCreation("House.png",image,"object",[[190, 375], [350, 375], [350, 235], [190, 235]])
image = loadScale("Grass.png","ratio",1,1)
grass = ImageCreation("Grass.png",image,"background","none")
def enterFrameHandle(enterFrameTable):
for i in range(len(enterFrameTable)-1,-1,-1 ):
enterFrameTable[i].step(enterFrameTable)
def EventHandle(event):
global Player
if (event.type == pygame.QUIT):
endGame()
elif(event.type == pygame.KEYDOWN):
key = (pygame.key.name(event.key) )
if (key == "escape"):
endGame()
elif (key == "up" or key == "right" or key == "down" or key == "left"):
Player.moveView(key)
elif(event.type == pygame.KEYUP):
key = (pygame.key.name(event.key) )
if (key == "up" or key == "right" or key == "down" or key == "left"):
Player.endMoveView(key)
def endGame():
global QuitGame
QuitGame = True
def mainLoop():
## GLOBALS ##
global QuitGame
QuitGame = False
global Player
Player = PlayerCreation()
## MAIN TABLES ##
global enterFrameTable
enterFrameTable = []
global basicObjectTable
basicObjectTable = []
## TEMP TABLES ##
global imageTable
imageTable = []
## START UP LOOPS ##
imageLoad("basic")
global Map
Map = MapCreation()
## Temporary ##
while (QuitGame == False):
enterFrameHandle(enterFrameTable)
for event in pygame.event.get():
EventHandle(event)
Map.draw()
pygame.display.update() ## updates the screen ##
gameDisplay.fill([0,0,0]) ## Clears screen for next frame ##
gameClock.tick(64) ## The FPS ##
fps = gameClock.get_fps()
if (fps < 64 and fps != 0):
fps = gameClock.get_fps()
print("FPS HAS DROPPED TOO LOW DOWN TO",fps)
runGameNow = True
mainLoop()
pygame.quit()
quit()
Issue with path finder is due to picking up on itteration lines when finding nearest point.
The problem is quite straight forward really.
Your view the amount of lines as disease and the cause must be Pygame. Usually, or at least in almost any case - if you're not a god at using the language and library, the problem is probably not the language or the library. Because odds are you are no wear near to pushing the limits of what the two can deliver for you.
One dead give-away of this fact is, if you look at what your eyes see on screen, you'll quickly notice that whenever your background is about to loop around - that's when the glitch/frame drop occur.
Obviously, this COULD be a cause of the library doing something suspicious.. if it was the library that did the actual loop-around.
But in your code, this is a implementation you've done yourself.
So the best bet is to start looking there.
Just to be extremely confident in where the delay occurs - we can have a look at the profiler that comes with Python (cProfiler).
python -m cProfile -o sample_data.pyprof awesome_game.py
pyprof2calltree -i sample_data.pyprof -k
The result would show something along the lines of:
From this we can see that a lot of processing time goes to pygame.Surface and enterFrameHandle. And I bet that pygame.Surface is called within enterFrameHandle somewhere down the line.
So the best bet is to start at the end of the eventFrameHandle chain.
And in this breakdown, that's loadTiles.
And straight off the bat, a lot of warning signs in here.
There's at least four loops in here.. and loops are bad, because those take processing time.
I added debug information by simply doing:
def loadTiles
if load == "center":
print('Loading center')
elif load == "right":
print('Loading right')
Apparently center gets triggered whenever the glitch occurs.
So that narrows it down a little further. So i added timers around each for loop.
Loop one: takes 0.0 seconds
Loop two: takes 0.29 seconds
Loop three: takes 0.5 seconds
Loop four: takes 0.6 seconds
All in all, these are EXTREMELY bad for you, since they directly impact the render sequence. So why is this? Lets break it down even further.
We'll start off with the first loop that takes up 0.3 seconds:
for i in range(0,len(self.loadedTiles) ):
sx = self.loadedTiles[i].loaded[0]*self.tileSize
sy = self.loadedTiles[i].loaded[1]*self.tileSize
self.surface.blit(self.loadedTiles[i].buttomLayer,(sx,sy) )
So len(self.loadedTiles) will be 25.
That means this loop has to iterate 25 times per render cycle where loadTiles("center") is called.
Each cycle takes almost exactly 0.01 seconds which amounts up to 0.25 seconds per full loop. It's not terrible, well it is.. If you want 60 FPS out of your game, no loop or function call can take more than 0.0166 seconds in total.
So we've already overrun our desired FPS target. So we gotta knock a few milliseconds off this if we wanna get anywhere.
The loop in itself is not that bad, i mean 25 iterations, a modern PC can do that in less time than time() can measure. So, it all points to self.surface.blit().. Which from experience, it sure is.
This also correlate with the fact that our graph above spends 30% of the total CPU time in pygame.surface.blit.. So here's our first thief.
Looking at the rest of the loops, they are essentially the same, except with bigger numbers in the math which takes slight longer to calculate, hence probably the time difference in the loops.
So, what can we do to shrink down .blit times?
Well we can stop calling blit in every loop iteration, and just move the sprite objects positions and then blit them one by one.
But sensei, that's almost the same thing?
Well yes my soon to be ninja turtle, it is.. That's why, we'll move the sprites in to batches/groups, and render the group. Thus, we can change the positions (fast operation), and because we moved the rendering outside of the loops, we can also convert them into a group object and render the group.
First, we'll convert your objects into pygame.sprite.Sprite, these are clever little objects that contain both collision detection, moving arounds etc.
self.grass = pygame.sprite.Sprite()
self.grass.image = pygame.image.load("Grass.png")
self.grass.rect = pygame.Rect(0, 0, 60, 60)
And you add this into a rendering group (a batch):
self.main_group = pygame.sprite.Group(self.grass)
Bam, and instead of updating 25 sprites, now you can do:
self.main_group.draw(gameDisplay)
And bam, SPEED!
Now, your code is NOT designed for this.
So this will take a while to fix and correct this design flaw.
Because this would take hours for me in order to keep your original code as close as possible, i ignored this and reworked your entire MapCreation and modified your PlayerCreation and how you move the map around (I changed from trying to recenter each time, it's a nice idea but far to complex to implement 1PM rewriting someone else's code).. So.. Here's the new approach to your problem:
import pygame, os
from time import time
from collections import OrderedDict
os.environ['SDL_VIDEO_CENTERED'] = '1'
pygame.init()
_W,_H = pygame.display.Info().current_w, pygame.display.Info().current_h
flags = pygame.DOUBLEBUF | pygame.HWSURFACE | pygame.RESIZABLE
gameDisplay = pygame.display.set_mode((500, 500))
gameDisplay.fill((0,0,0)) ## FILL COLOR OF SCREEN ##
pygame.display.set_caption("Dope Game") ## SETS NAME ##
gameClock = pygame.time.Clock() ## CLOCK OF THE GAME ##
import math
import os
import random
import copy
SQRT = math.sqrt
PI = math.pi
cos = math.cos
sin = math.sin
## REPEATABLE FUNCTIONS ##
def loadScale(file,command,sizeX,sizeY):
temp = pygame.image.load(file)
tempInfo = temp.get_rect()
tempInfo1,tempInfo2,tempInfo3,tempInfo4 = temp.get_rect()
tempInfo3 = int(tempInfo3)
tempInfo4 = int(tempInfo4)
if (command == "ratio"):
tempInfo3 = tempInfo3*sizeX
tempInfo4 = tempInfo4*sizeY
temp = pygame.transform.scale(temp,(int(tempInfo3),int(tempInfo4) ) )
elif (command == "size"):
temp = pygame.transform.scale(temp, (sizeX,sizeY) )
return(temp)
## NON GAME RELATED CLASSES ##
class EnterFrame():
def __init__(self,frameReset,function,parse,reset):
self.frameReset = frameReset
self.currentFrame = frameReset
self.function = function
self.parse = parse
self.reset = reset
if (self.reset != "onComplete"):
self.reset = (reset-1)
enterFrameTable.append(self)
def step(self,enterFrameTable):
if (self.currentFrame == 0):
self.function(self.parse)
if (self.reset != "onComplete"):
if (self.reset > 0):
self.currentFrame = self.frameReset
self.reset = self.reset-1
else:
enterFrameTable.remove(self)
del self
else:
self.currentFrame = self.frameReset
else:
self.currentFrame = self.currentFrame-1
class PlayerCreation():
def __init__(self):
self.x = _W
self.y = _H
self.view = [1600,1600]
self.viewShift = []
self.viewChangeSpeed = 25
def moveView(self,key):
add = EnterFrame(0,self.moveViewAction,key,"onComplete")
self.viewShift.append([add,key])
def moveViewAction(self,key):
if (key == "up"):
self.view[1] = self.view[1]-self.viewChangeSpeed
Map.move_tile(0, 1) # Player moves up, so the tiles should move down -> (0, 1) == (x, y)
if (self.view[1] < 0):
self.view[1] = 0
elif (key == "right"):
self.view[0] = self.view[0]+self.viewChangeSpeed
Map.move_tile(-1, 0)
if (self.view[0] > Map.tileSize*4):
self.view[0] = Map.tileSize*4
elif (key == "down"):
self.view[1] = self.view[1]+self.viewChangeSpeed
Map.move_tile(0, -1)
if (self.view[1] > Map.tileSize*4):
self.view[1] = Map.tileSize*4
elif (key == "left"):
self.view[0] = self.view[0]-self.viewChangeSpeed
Map.move_tile(1, 0)
if (self.view[0] < 0):
self.view[0] = 0
def endMoveView(self,key):
for i in range(len(self.viewShift)-1,-1,-1 ):
if (self.viewShift[i][1] == key):
enterFrameTable.remove(self.viewShift[i][0])
del self.viewShift[i]
class ImageCreation():
def __init__(self,name,image,type,hitBox):
self.name = name
self.image = image
self.type = type
self.hitBox = hitBox
self.rect = self.image.get_rect()
if (self.hitBox != "none"):
self.shiftX = hitBox[0][0]
self.shiftY = hitBox[0][1]
for i in range(1,len(hitBox) ):
if (hitBox[i][0] < self.shiftX):
self.shiftX = hitBox[i][0]
if (hitBox[i][1] < self.shiftY):
self.shiftY = hitBox[i][1]
else:
self.shiftX = self.rect[2]/2
self.shiftY = self.rect[3]/2
imageTable.append(self)
def draw(self,x,y):
image = self.image
self.blit = gameDisplay.blit(image,(x,y) )
class MapCreation():
def __init__(self):
self.tileSize = 800
self.size = self.tileSize*10
self.tiles = []
self.centerTile = [5,5]
self.amount = round(self.size/self.tileSize)
self.sprites = OrderedDict()
self.grass_image = pygame.image.load("Grass.png")
self.grass_group = pygame.sprite.Group()
for x in range(0, self.amount*60, 60): ## 10*60, but we step 60 pixels, so in the end, this will be 10 steps.
for y in range(0, self.amount*60, 60): ## Which is the same as `for x in range(self.amount)` but we scale it up
## to give us pixels instead of just the ammount.
index = len(self.sprites) # -- Generate a index for this sprite. Used for access later (to update pos for instace)
## == Create the sprite, add a image to it and define a position and size.
self.sprites[index] = pygame.sprite.Sprite()
self.sprites[index].image = self.grass_image
self.sprites[index].rect = pygame.Rect(x, y, 60, 60)
## == Then add the sprite to the grass group.
self.grass_group.add(self.sprites[index])
def move_tile(self, dx, dy):
for index in self.sprites:
x, y, width, height = self.sprites[index].rect
## == this is how you move the sprites:d
self.sprites[index].rect = pygame.Rect(x+dx, y+dy, 60, 60)
def draw(self):
self.grass_group.update()
self.grass_group.draw(gameDisplay)
class Tile():
def __init__(self,image,coords):
transparentSurface = pygame.Surface([1000,1000], pygame.SRCALPHA, 32)
self.x = coords[0]
self.y = coords[1]
self.loaded = False
self.buttomLayer = image
self.middleLayer = copy.copy(transparentSurface)
self.topLayer = transparentSurface
class imageObject():
def __init__(self,info,tile,coords):
self.info = info
self.image = info.image
self.rect = self.image.get_rect()
self.x = coords[0]
self.y = coords[1]
self.hitBox = []
if (self.info.hitBox != "none"):
for i in range(0,len(self.info.hitBox) ):
self.hitBox.append([self.info.hitBox[i][0]+self.x,self.info.hitBox[i][1]+self.y])
#self.object = createObject(self.hitBox,True,"none")
if (info.type == "background"):
tile.buttomLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object"):
tile.middleLayer.blit(self.image,(self.x,self.y) )
if (info.type == "object alphas"):
tile.topLayer.blit(self.image,(self.x,self.y) )
def imageFind(name):
for i in range(0,len(imageTable) ):
if (name == imageTable[i].name):
return(imageTable[i])
return("none")
def imageLoad(types):
if (types == "basic"):
image = loadScale("House.png","ratio",1,1)
basicHouse = ImageCreation("House.png",image,"object",[[190, 375], [350, 375], [350, 235], [190, 235]])
image = loadScale("Grass.png","ratio",1,1)
grass = ImageCreation("Grass.png",image,"background","none")
def enterFrameHandle(enterFrameTable):
for i in range(len(enterFrameTable)-1,-1,-1 ):
enterFrameTable[i].step(enterFrameTable)
def EventHandle(event):
global Player
if (event.type == pygame.QUIT):
endGame()
elif(event.type == pygame.KEYDOWN):
key = (pygame.key.name(event.key) )
if (key == "escape"):
endGame()
elif (key == "up" or key == "right" or key == "down" or key == "left"):
Player.moveView(key)
elif(event.type == pygame.KEYUP):
key = (pygame.key.name(event.key) )
if (key == "up" or key == "right" or key == "down" or key == "left"):
Player.endMoveView(key)
def endGame():
global QuitGame
QuitGame = True
def mainLoop():
## GLOBALS ##
global QuitGame
QuitGame = False
global Player
Player = PlayerCreation()
## MAIN TABLES ##
global enterFrameTable
enterFrameTable = []
global basicObjectTable
basicObjectTable = []
## TEMP TABLES ##
global imageTable
imageTable = []
## START UP LOOPS ##
imageLoad("basic")
global Map
Map = MapCreation()
## Temporary ##
while (QuitGame == False):
enterFrameHandle(enterFrameTable)
for event in pygame.event.get():
EventHandle(event)
pygame.display.update() ## updates the screen ##
gameDisplay.fill([0,0,0]) ## Clears screen for next frame ##
#Map.draw()
Map.grass_group.update()
Map.grass_group.draw(gameDisplay)
gameClock.tick(64) ## The FPS ##
fps = gameClock.get_fps()
if (fps < 64 and fps != 0):
fps = gameClock.get_fps()
print("FPS HAS DROPPED TOO LOW DOWN TO",fps)
pygame.display.flip()
runGameNow = True
mainLoop()
pygame.quit()
quit()
This code rarely drop down in FPS.
And when it does, it drops down to ~63 FPS because I moved the window around (that's a taxing thing to do because all the OpenGL references need to compensate for the new window position and events that triggers within Pygame (resize events, move events etc are taxing, but occurs once and not every render loop.. so it's acceptable).
Conclusion
It's rarely the library's fault for slow performance.
Stop thinking as a developer for a second, and watch the screen and see if you can find clues to as where the fault may lie. In this case a obvious glitch occurred every time the tiles rotated around the screen edge.. That's a clue! Not a single programming skill needed to visually see this bug.
Use a profiler and debug where your code spends most of the time.
Try to see how others have solved game designs.. You're on the right track by putting everything in classes, but that mostly helps your readability of the code - try to see what's in other peoples classes and how they use Pygame calls.
All in all, it's quite a fun and nice code you got going.
It's just not made for performance and it's hard to code in afterwards.. so maybe it's not such a bad idea to start all over? But keep your old code base for reference and avoid the obvious traps:
loops in the render sequence
rendering objects one by one, use batches instead
Only do loops to update positions/stats, not graphics!
Last note on the topic: Graphic cards are huge power houses. They can calculate a few million operations a second if not more. And each time you blit something to the screen, the graphics card needs to interrupt it's calculations and memory allocation to flip around buffers and update what you see on the screen.. And that's slow work because it's not calculations, it's operational tasks (which usually includes waiting for queues and signals).. So instead of throwing hundreds of blit's a second to the graphics card, throw math operations at it by sending it textures (math data describing how things look).. and then call blit (draw, in this case) once and let the graphics card take all the math you threw at it - do one big logical operation - and presto.. x100 the speed already.

python keypress simple game

I would like to see on screen a sign, e.x might be (hash) '#'. Sign will have some starting position, let's say (0, 0). I would like to see sign moving right if I press right arrow, left if I press left arrow, etc.
So far my code looks like this, and it works for reading pos, but I want to add some kind of "animation" so I can see sign is moving on the screen:
!Update: Just to give u a clue, I created "icon" and now when u press right or left, icon moves in desired direction.
from msvcrt import getch
icon = chr(254)
pos = [0, 0]
t = []
def fright():
global pos
pos[0] += 1
print ' ' * pos[0],
print(icon)
def fleft():
global pos
pos[0] -= 1
print ' ' * pos[0],
print(icon)
def fup():
global pos
pos[1] += 1
def fdown():
global pos
pos[1] -= 1
def appendTab():
global pos, t
t.append(pos)
while True:
print'Distance from zero: ', pos
key = ord(getch())
if key == 27: #ESC
break
elif key == 13: #Enter
print('selected')
appendTab()
elif key == 32: #Space, just a small test - skip this line
print('jump')
print(t)
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key == 80: #Down arrow
print('down')
fdown()
elif key == 72: #Up arrow
print('up')
fup()
elif key == 75: #Left arrow
print('left')
fleft()
elif key == 77: #Right arrow
print('right')
fright()
You could create a list of lists that serves as the map and set the cell of the player to '#'. Then just print the map and if the player moves, clear the command-line/terminal with os.system('cls' if os.name == 'nt' else 'clear') and print the updated map.
import os
from msvcrt import getch
pos = [0, 0]
# The map is a 2D list filled with '-'.
gamemap = [['-'] * 5 for _ in range(7)]
# Insert the player.
gamemap[pos[1]][pos[0]] = '#'
while True:
print('Distance from zero: ', pos )
key = ord(getch())
if key == 27: #ESC
break
elif key == 224: #Special keys (arrows, f keys, ins, del, etc.)
key = ord(getch())
if key in (80, 72, 75, 77):
# Clear previous tile if player moves.
gamemap[pos[1]][pos[0]] = '-'
if key == 80: #Down arrow
pos[1] += 1
elif key == 72: #Up arrow
pos[1] -= 1
elif key == 75: #Left arrow
pos[0] -= 1
elif key == 77: #Right arrow
pos[0] += 1
print('clear')
# Clear the command-line/terminal.
os.system('cls' if os.name == 'nt' else 'clear')
# Set the player to the new pos.
gamemap[pos[1]][pos[0]] = '#'
# Print the map.
for row in gamemap:
for tile in row:
print(tile, end='')
print()

'break' outside loop in snake game

I'm currently making a snake game in python (IDLE 3.6.0) and it keeps coming up with an error called 'break' outside loop. What does that mean? What am I doing wrong. This is the first time I've ever come accross this error.
Here's my code:
# SNAKES GAME
# Use ARROW KEYS to play, SPACE BAR for pausing/resuming and Esc Key for exiting
import curses
from curses import KEY_RIGHT, KEY_LEFT, KEY_UP, KEY_DOWN
from random import randint
curses.initscr()
win = curses.newwin(20, 60, 0, 0)
win.keypad(1)
curses.noecho()
curses.cur_set(0)
win.border(0)
win.nodelay(1)
key = KEY_RIGHT # Initalizing Values
score = 0
snake = [[4,10], [4,9], [4,8]] # Initial snake co-ordinates
food = [10,20] # First food co-ordinates
win.addc(food[0], food[1], '*') # Prints the food
while key != 27:
win.border(0)
win.addstr(0, 2, 'Score :' + str(score) + ' ') # Printing 'Score' and
win.addstr(0, 27, ' SNAKE ') # 'SNAKE' strings
win.timeout(150 - (len(snake)/5 + len(snake)/10)%120)
prevKey = key # Previous key pressed
event = win.getch
key = key if event == -1 else event
if key == ord(' '): # If SPACE BAR is pressed, wait for another
key = -1 # one (Pause/Resume)
while key != ord(' '):
key = win.getch()
key = prevKey
continue
if key not in [KEY_LEFT, KEY_RIGHT, KEY_UP, KEY_DOWN, 27]: # If an invalid key is pressed
key = prevKey
# Calculates the new coordinates of the head of the snake. NOTE: len(snake) increases.
# This is taken care of later at [1].
snake.insert(0, [snake[0][0] + (key == KEY_DOWN and 1) + (key == KEY_UP and -1), snake[0][1] + (key == KEY_LEFT and -1) + (key == KEY_RIGHT and 1)])
# If snake crosses the boundaries, make it enter from the other side
if snake[0][0] == 0: snake[0][0] = 18
if snake[0][1] == 0: snake[0][1] = 58
if snake[0][0] == 19: snake[0][0] = 1
if snake[0][1] == 59: snake[0][1] = 1
# Exit if snake crosses the boundaries (Uncomment to enable)
# if snake[0][0] == 0 or snake[0][0] == 19 or snake[0][1] == 0 or snake[0][1] == 59: break
# If snake runs over itself
if snake[0]in snake[1:]: break
if snake[0] == food:
food = []
score += 1
while food == []:
food = [randint(1, 18), randint(1, 58)]
if food in snake: food = []
win.addch(food[0], food[1], '*')
else:
last = snake.pop()
win.addch(last[0], last[1], ' ')
win.addch(snake[0][0], snake[0][1], '#')
curses.erdwin()
print("\nScore - " + str(score))
print("http://bitemelater.in\n")
I'd be glad if you could help! Thanks!
The line that says if snake[0]in snake[1:]: break is where your error is stemming from. You probably want to use some kind of game ending function to display points and high scores and such:
def end_game():
#do your game ending things here
Then you would call that function here:
if snake[0]in snake[1:]: end_game()
EDIT:
I found this in relation to your curses error: Error no module named curses. It seems as if curses doesn't support Windows machines. Try installing UniCurses or this binary.

simplegui indentation error: SyntaxError: bad input (' ')

I'm doing a mini-project on Coursera and I can run most parts of my code. However there's an error in the critical part about the game's match or not checking.
# implementation of card game - Memory
import simplegui
import random
# helper function to initialize globals
def new_game():
global turns, state, pairs, cards
turns = 0
state = 0
pairs = []
cards = range(9) * 2
random.shuffle(cards)
# define event handlers
def mouseclick(pos):
# add game state logic here
global turns, state, pairs
pointed = pos[0] // 50
if pointed in pairs:
pass
else:
if state == 0:
state = 1
pairs.append(pointed)
elif state == 1:
state = 2
turns += 1
label.set_text('Turns =' + str(turns))
pairs.append(pointed)
# if cards[pairs[-2]] == cards[[pairs[-1]]:
# flag = True
# else:
# flag = False
else:
state = 1
if flag == False:
del pairs[-2:]
pairs.append(pointed)
# cards are logically 50x100 pixels in size
def draw(canvas):
for n in range(1, 16):
canvas.draw_line((n * 50, 0), (n * 50, 100), 1, 'Green')
for n in pairs:
canvas.draw_line((n * 50 + 25, 0), (n * 50 + 25, 100), 50, 'White')
for n in pairs:
canvas.draw_text(str(cards[n]), (n * 50 + 15, 65), 50, 'Black')
# create frame and add a button and labels
frame = simplegui.create_frame("Memory", 800, 100)
frame.set_canvas_background('Red')
frame.add_button("Reset", new_game)
label = frame.add_label("Turns = 0")
# register event handlers
frame.set_mouseclick_handler(mouseclick)
frame.set_draw_handler(draw)
# get things rolling
new_game()
frame.start()
# Always remember to review the grading rubric
I commented out Line 31 to 34 and that's the part where I have a problem. The console keeps telling me Line 31: SyntaxError: bad input (' ') but I think the indentation is correctly made.
Please help me figure out why it's a 'bad input', thanks a lot!
Update:
Thanks to Russell's help, this function works now.
# define event handlers
def mouseclick(pos):
# add game state logic here
global turns, state, pairs, flag
pointed = pos[0] // 50
if pointed in pairs:
pass
else:
if state == 0:
state = 1
pairs.append(pointed)
elif state == 1:
state = 2
turns += 1
label.set_text('Turns =' + str(turns))
pairs.append(pointed)
if cards[pairs[-2]] == cards[pairs[-1]]:
flag = True
else:
flag = False
else:
state = 1
if flag == False:
del pairs[-2:]
pairs.append(pointed)
Your if statement is indented too far.
elif state == 1:
state = 2
turns += 1
label.set_text('Turns =' + str(turns))
pairs.append(pointed)
if cards[pairs[-2]] == cards[pairs[-1]]:
flag = True
else:
flag = False
else:
state = 1
if flag == False:
del pairs[-2:]
pairs.append(pointed)

Categories

Resources