Python Forum
Circle Game: My first game using a graphical library
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Circle Game: My first game using a graphical library
#1
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.
Reply
#2
Have a read here and then refactor your code accordingly.

Why are global variables evil?
Reply
#3
(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.
Reply
#4
(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 :)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  CODEBREAKER game rob101 0 818 Nov-07-2023, 07:08 AM
Last Post: rob101
  Word game revisited menator01 1 1,849 Sep-12-2022, 02:33 AM
Last Post: XavierPlatinum
  Open source RTS game Forward Only IlyaFaer 11 6,287 Aug-29-2022, 06:38 AM
Last Post: IlyaFaer
  Tkinter number guessing game menator01 2 3,440 Mar-24-2022, 07:13 PM
Last Post: menator01
  Arc_AntsHunt: Game Developed In Python Arcade adt 2 4,405 Apr-24-2021, 06:05 PM
Last Post: adt
  Mazecraft - A fast paced, maze solving game(with maze editor) Atekka 0 2,341 Oct-16-2020, 01:36 AM
Last Post: Atekka
  Car game for Reinforcement Learning (CaRL) MadS123 0 2,034 Sep-19-2020, 03:45 PM
Last Post: MadS123
  Pygame clicker game CrazyMakes 2 13,082 May-26-2020, 07:41 PM
Last Post: CrazyMakes
  Lunar Lander Text Game hendry1 1 3,127 May-17-2020, 12:21 AM
Last Post: TomToad
  SKUNK Dice Game ProntName 9 6,480 Apr-19-2020, 12:15 PM
Last Post: eXcalibur432

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020