Python Forum

Full Version: Circle Game: My first game using a graphical library
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,
Over the past week I have been making a game in Python using the graphical library Pyxel (https://github.com/kitao/pyxel) and made a little game that started out as a single python file and then grew into several different files as I implemented more features with each new day. This is the first version I released:

CG-20191118.py
import pyxel
import random

pyxel.init(255,160,caption="Circle Game 20191118")

x = random.randrange(10,244)
y = random.randrange(10,149)
col = 7
debug = False

def update():
	global runtime,frame
	global x,y,col,debug
	if pyxel.btn(pyxel.KEY_LEFT) or pyxel.btn(pyxel.KEY_A):
		x-=1
	if pyxel.btn(pyxel.KEY_RIGHT) or pyxel.btn(pyxel.KEY_D):
		x+=1
	if pyxel.btn(pyxel.KEY_UP) or pyxel.btn(pyxel.KEY_W):
		y-=1
	if pyxel.btn(pyxel.KEY_DOWN) or pyxel.btn(pyxel.KEY_S):
		y+=1
	
	if pyxel.btn(pyxel.KEY_Q):
		col = col - 1
		if col < 1:
			col = 15
	if pyxel.btn(pyxel.KEY_E):
		col = col + 1
		if col > 15:
			col = 1

	if pyxel.btn(pyxel.KEY_R):
		x = random.randrange(0,244)
		y = random.randrange(0,149)

	if pyxel.btnp(pyxel.KEY_F):
		if debug == True:
			debug = False
		elif debug == False:
			debug = True
		
def draw():
	global runtime,frame
	global x,y,col,debug
	pyxel.cls(0)
	pyxel.circ(x,y,10,col)
	pyxel.text(0,0,"CG-20191118", 7)
	if debug == True:
		pyxel.text(0,6,"X: " + str(x),7)
		pyxel.text(0,12,"Y: " + str(y),7)
		pyxel.text(0,18,"COL: " + str(col),7)

pyxel.run(update,draw)
And then over the course of 4 days, I turned it into a functioning game with walls, a proper respawning system, and hazardous objects which will kill you when you touch them. I think it is pretty good considering that it was 4 days of development, I am 15 and have only a decent amount of experience with Python (I'm not very good with classes), plus most of that time was spent doing theatre stuff. If you want to look at the source code and maybe even propose changes to it, or if you want to try the game yourself, go to https://github.com/Flux3on/circle-game. Thank you to everyone who reads this post.
Have a read here and then refactor your code accordingly.

Why are global variables evil?
(Nov-24-2019, 10:18 AM)ThomasL Wrote: [ -> ]Have a read here and then refactor your code accordingly.

Why are global variables evil?

Do variables from another file count as global variables? Because that's what the latest version uses.
(Nov-24-2019, 06:19 PM)Flux3on Wrote: [ -> ]Do variables from another file count as global variables?
If you have to use the global keyword because you're trying to update a global variable, you're using global variables in a way people will be worried about. If your global "variables" don't vary, then they're constants and (1) you don't need the keyword, which is only important for mutation and (2) global constants are fine.

Object-oriented programming is the usual solution to global variables. Sometimes the fix is small and easy, and sometimes it's very complicated. The more you use globals, the more likely you are to get to a point where you're unable to update / fix the code, and people like me won't help beyond suggesting re-writing without globals :)