• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EST 17:32
CET 23:32
KST 07:32
  • Home
  • Forum
  • Calendar
  • Streams
  • Liquipedia
  • Features
  • Store
  • EPT
  • TL+
  • StarCraft 2
  • Brood War
  • Smash
  • Heroes
  • Counter-Strike
  • Overwatch
  • Liquibet
  • Fantasy StarCraft
  • TLPD
  • StarCraft 2
  • Brood War
  • Blogs
Forum Sidebar
Events/Features
News
Featured News
SC2 All-Star Invitational: Tournament Preview2RSL Revival - 2025 Season Finals Preview8RSL Season 3 - Playoffs Preview0RSL Season 3 - RO16 Groups C & D Preview0RSL Season 3 - RO16 Groups A & B Preview2
Community News
Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets4$21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)15Weekly Cups (Dec 29-Jan 4): Protoss rolls, 2v2 returns7[BSL21] Non-Korean Championship - Starts Jan 103SC2 All-Star Invitational: Jan 17-1834
StarCraft 2
General
Stellar Fest "01" Jersey Charity Auction SC2 All-Star Invitational: Tournament Preview Weekly Cups (Jan 5-11): Clem wins big offline, Trigger upsets When will we find out if there are more tournament SC2 Spotted on the EWC 2026 list?
Tourneys
SC2 All-Star Invitational: Jan 17-18 OSC Season 13 World Championship SC2 AI Tournament 2026 Sparkling Tuna Cup - Weekly Open Tournament $21,000 Rongyi Cup Season 3 announced (Jan 22-Feb 7)
Strategy
Simple Questions Simple Answers
Custom Maps
Map Editor closed ?
External Content
Mutation # 508 Violent Night Mutation # 507 Well Trained Mutation # 506 Warp Zone Mutation # 505 Rise From Ashes
Brood War
General
BGH Auto Balance -> http://bghmmr.eu/ [ASL21] Potential Map Candidates How Rain Became ProGamer in Just 3 Months BW General Discussion A cwal.gg Extension - Easily keep track of anyone
Tourneys
[Megathread] Daily Proleagues Small VOD Thread 2.0 [BSL21] Grand Finals - Sunday 21:00 CET [BSL21] Non-Korean Championship - Starts Jan 10
Strategy
Soma's 9 hatch build from ASL Game 2 Simple Questions, Simple Answers Game Theory for Starcraft Current Meta
Other Games
General Games
Beyond All Reason Awesome Games Done Quick 2026! Nintendo Switch Thread Mechabellum Stormgate/Frost Giant Megathread
Dota 2
Official 'what is Dota anymore' discussion
League of Legends
Heroes of the Storm
Simple Questions, Simple Answers Heroes of the Storm 2.0
Hearthstone
Deck construction bug Heroes of StarCraft mini-set
TL Mafia
Vanilla Mini Mafia Mafia Game Mode Feedback/Ideas
Community
General
Russo-Ukrainian War Thread US Politics Mega-thread European Politico-economics QA Mega-thread Things Aren’t Peaceful in Palestine Trading/Investing Thread
Fan Clubs
Innova Crysta on Hire
Media & Entertainment
[Manga] One Piece Anime Discussion Thread
Sports
2024 - 2026 Football Thread
World Cup 2022
Tech Support
Computer Build, Upgrade & Buying Resource Thread
TL Community
The Automated Ban List
Blogs
My 2025 Magic: The Gathering…
DARKING
Physical Exercise (HIIT) Bef…
TrAiDoS
Life Update and thoughts.
FuDDx
How do archons sleep?
8882
James Bond movies ranking - pa…
Topin
Customize Sidebar...

Website Feedback

Closed Threads



Active: 2032 users

The Big Programming Thread - Page 209

Forum Index > General Forum
Post a Reply
Prev 1 207 208 209 210 211 1032 Next
Thread Rules
1. This is not a "do my homework for me" thread. If you have specific questions, ask, but don't post an assignment or homework problem and expect an exact solution.
2. No recruiting for your cockamamie projects (you won't replace facebook with 3 dudes you found on the internet and $20)
3. If you can't articulate why a language is bad, don't start slinging shit about it. Just remember that nothing is worse than making CSS IE6 compatible.
4. Use [code] tags to format code blocks.
mustache
Profile Joined April 2010
Switzerland309 Posts
December 05 2012 08:45 GMT
#4161
I'm programming a small snake game in C that runs in the console window. right now it clears and redraws the entire screen every time the snake moves 1 increment. this make the bottom of the screen start flickering because the redraw at the bottom takes longer than the top.

my question is if there is a way to go to a specific line in the console window, delete that line, and redraw it. that way i could check if something changed and update only that line.

using windows and visual basic
Sub40APM
Profile Joined August 2010
6336 Posts
December 05 2012 09:25 GMT
#4162
On December 04 2012 06:48 Blisse wrote:

For example, if you call lower() on a string, then you check if the string has any uppercase letters. It even sounds redundant, no? as how can a lower cased string have uppercase letters anyways?

I agree with most of your post but must come to his defense. If you look back to his code, while he converts the input string into lower and does stuff to it, his check on Uppercase applies to the original input string, whatever it may be.
Arnstein
Profile Blog Joined May 2010
Norway3381 Posts
December 05 2012 10:33 GMT
#4163
Anyone know where I can find good exercises for double lists and matrices in Python?
rsol in response to the dragoon voice being heard in SCII: dragoon ai reaches new lows: wanders into wrong game
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
Last Edited: 2012-12-05 10:53:57
December 05 2012 10:47 GMT
#4164
I'm heavy into C++ and .NET but I need to learn some PHP+mySQL. Are there any good advanced tutorial sites out there? As in, they don't have to explain all the programming basics, just the language itself and the environment to work in. I have never developed in a web language so I'm new in that specific area.

Also what good free IDEs are there for php?
England will fight to the last American
AmericanUmlaut
Profile Blog Joined November 2010
Germany2589 Posts
December 05 2012 11:28 GMT
#4165
On December 05 2012 19:47 KaiserJohan wrote:
Also what good free IDEs are there for php?

I use NetBeans for PHP and it works very well.
The frumious Bandersnatch
KaiserJohan
Profile Joined May 2010
Sweden1808 Posts
December 05 2012 11:36 GMT
#4166
What about the eclipse PHP plugins?
England will fight to the last American
Kambing
Profile Joined May 2010
United States1176 Posts
Last Edited: 2012-12-05 16:05:15
December 05 2012 14:17 GMT
#4167
On December 05 2012 17:41 ghrur wrote:
Hi guys,

So next semester, I will be learning Python as part of a Comp Sci class I'll be taking. At the end of the class, we'll have to make a project primarily in Python. I've heard about this from my friends who're taking it now, so I've started contemplating the project. One of the thoughts I had was making an AI of sorts to play BW. It'd probably work through BWAPI. The problem is, a lot of my friends also say Python is too slow to be able to make the calculations for a game AI.

Anyway, I'd just like some opinions on if it'd be possible to build a BW AI-bot in python.

Thanks!


It's perfectly reasonable; you aren't going to be building anything crazy performance-demanding.

The only catch is that the BWAPI library is written in C++ so you would need to understand how to interop with C++ code from Python. Luckily, someone's already written Python bindings against BWAPI, so you don't even need to worry about that:

http://code.google.com/p/bwapi/
http://code.google.com/p/pybw/
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
December 05 2012 14:28 GMT
#4168
On December 05 2012 17:45 mustache wrote:
I'm programming a small snake game in C that runs in the console window. right now it clears and redraws the entire screen every time the snake moves 1 increment. this make the bottom of the screen start flickering because the redraw at the bottom takes longer than the top.

my question is if there is a way to go to a specific line in the console window, delete that line, and redraw it. that way i could check if something changed and update only that line.

using windows and visual basic


I came across the same problem before. I knew this ability existed, because if you've used a lot of terminal installers, they often have a static download bar. However, I never found out how to do this.

I would appreciate an answer to this problem as well, even though I've moved on.



On December 05 2012 18:25 Sub40APM wrote:
Show nested quote +
On December 04 2012 06:48 Blisse wrote:

For example, if you call lower() on a string, then you check if the string has any uppercase letters. It even sounds redundant, no? as how can a lower cased string have uppercase letters anyways?

I agree with most of your post but must come to his defense. If you look back to his code, while he converts the input string into lower and does stuff to it, his check on Uppercase applies to the original input string, whatever it may be.


Ooops. XD Strange that I missed that, though that would be another sample of redundancy, for this case.
Actually, now I'm deciding if it was wrong to get the Pig Latin of the lowercased word, though I'm not sure how abbreviations and proper nouns should be handled, because the words that would be created wouldn't be real words (though it's never a real word anyways).
There is no one like you in the universe.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2012-12-05 16:47:29
December 05 2012 16:46 GMT
#4169
On December 05 2012 17:41 ghrur wrote:
Hi guys,

So next semester, I will be learning Python as part of a Comp Sci class I'll be taking. At the end of the class, we'll have to make a project primarily in Python. I've heard about this from my friends who're taking it now, so I've started contemplating the project. One of the thoughts I had was making an AI of sorts to play BW. It'd probably work through BWAPI. The problem is, a lot of my friends also say Python is too slow to be able to make the calculations for a game AI.

Anyway, I'd just like some opinions on if it'd be possible to build a BW AI-bot in python.

Thanks!

A lot of your friends say Python is too slow for AI calculations? Sigh... A lot of your friends like to make assumptions without testing them. Python is perfectly fine for scripting AI. Your friends don't know what they're talking about.

On December 05 2012 17:45 mustache wrote:
I'm programming a small snake game in C that runs in the console window. right now it clears and redraws the entire screen every time the snake moves 1 increment. this make the bottom of the screen start flickering because the redraw at the bottom takes longer than the top.

my question is if there is a way to go to a specific line in the console window, delete that line, and redraw it. that way i could check if something changed and update only that line.

using windows and visual basic

That's weird, are you multi-threading at all? That sounds like screen tearing where you're calling WriteConsoleOutput too quickly. Are you at least double buffering? Here's a couple links, which should be way more than enough information for you to fix this:
http://cecilsunkure.blogspot.com/2011/11/windows-console-game-setting-up-window.html
http://cecilsunkure.blogspot.com/2012/07/windows-console-game-ascii-engine.html
mustache
Profile Joined April 2010
Switzerland309 Posts
Last Edited: 2012-12-05 18:26:41
December 05 2012 18:24 GMT
#4170
+ Show Spoiler +

void Print_Field()
{
int i;
int h;
system("cls"); // clears old screen
printf("points: %d\n", points); // displays points in first console line
for(i=0, h=1; i<AREA; i++) // prints gamefield, until the array with size AREA is completely printed
{
if(i == h*WIDTH) // array is 1 dimensional ; program skips to a new line every time WIDTH has been reached
{
printf("\n");
h=h+1;
}
printf("%c", field[i].sprite); // chooses what character is printed

}
}


so im just cycling through my array and printing the values that are in that. but because the redrawing isnt fast enough the screen flickers(i assume)

I've just started out programming in general so im not sure what multi threading or double buffering is.(so im probably not using it)

ill check out the links in detail, but i'd like to program the game from start to finish by my self, to get more familiar with how things work.

EDIT: i noticed in my first post i wrote im using visual basic. I meant visual studio c++, and im coding in C. ouch
Bigpet
Profile Joined July 2010
Germany533 Posts
December 05 2012 18:30 GMT
#4171
On December 06 2012 03:24 mustache wrote:
+ Show Spoiler +

void Print_Field()
{
int i;
int h;
system("cls"); // clears old screen
printf("points: %d\n", points); // displays points in first console line
for(i=0, h=1; i<AREA; i++) // prints gamefield, until the array with size AREA is completely printed
{
if(i == h*WIDTH) // array is 1 dimensional ; program skips to a new line every time WIDTH has been reached
{
printf("\n");
h=h+1;
}
printf("%c", field[i].sprite); // chooses what character is printed

}
}


so im just cycling through my array and printing the values that are in that. but because the redrawing isnt fast enough the screen flickers(i assume)

I've just started out programming in general so im not sure what multi threading or double buffering is.(so im probably not using it)

ill check out the links in detail, but i'd like to program the game from start to finish by my self, to get more familiar with how things work.

EDIT: i noticed in my first post i wrote im using visual basic. I meant visual studio c++, and im coding in C. ouch


I don't know about the performance characteristics of system("cls") but you shouldn't printf() every single charater. You flush way too often that way. I'd advise you sprintf() a whole frame and just call printf() once you constructed the whole frame.
I'm NOT the caster with a similar nick
Deleted User 101379
Profile Blog Joined August 2010
4849 Posts
December 05 2012 18:34 GMT
#4172
On December 06 2012 03:24 mustache wrote:
+ Show Spoiler +

void Print_Field()
{
int i;
int h;
system("cls"); // clears old screen
printf("points: %d\n", points); // displays points in first console line
for(i=0, h=1; i<AREA; i++) // prints gamefield, until the array with size AREA is completely printed
{
if(i == h*WIDTH) // array is 1 dimensional ; program skips to a new line every time WIDTH has been reached
{
printf("\n");
h=h+1;
}
printf("%c", field[i].sprite); // chooses what character is printed

}
}


so im just cycling through my array and printing the values that are in that. but because the redrawing isnt fast enough the screen flickers(i assume)

I've just started out programming in general so im not sure what multi threading or double buffering is.(so im probably not using it)

ill check out the links in detail, but i'd like to program the game from start to finish by my self, to get more familiar with how things work.

EDIT: i noticed in my first post i wrote im using visual basic. I meant visual studio c++, and im coding in C. ouch


It's not possible in default C as far as i remember, though there are libraries for that.
From a quick google, http://www.projectpluto.com/win32a.htm should work. I haven't tried it though since most of my C development was under linux where we had ncurses (which is not available for windows).
Blisse
Profile Blog Joined July 2010
Canada3710 Posts
December 05 2012 18:37 GMT
#4173
On December 06 2012 03:24 mustache wrote:
+ Show Spoiler +

void Print_Field()
{
int i;
int h;
system("cls"); // clears old screen
printf("points: %d\n", points); // displays points in first console line
for(i=0, h=1; i<AREA; i++) // prints gamefield, until the array with size AREA is completely printed
{
if(i == h*WIDTH) // array is 1 dimensional ; program skips to a new line every time WIDTH has been reached
{
printf("\n");
h=h+1;
}
printf("%c", field[i].sprite); // chooses what character is printed

}
}


so im just cycling through my array and printing the values that are in that. but because the redrawing isnt fast enough the screen flickers(i assume)

I've just started out programming in general so im not sure what multi threading or double buffering is.(so im probably not using it)

ill check out the links in detail, but i'd like to program the game from start to finish by my self, to get more familiar with how things work.

EDIT: i noticed in my first post i wrote im using visual basic. I meant visual studio c++, and im coding in C. ouch


Could you try building the array as a giant string, and then calling printf once on that entire string? That should solve it.


@Cecil, I think he's writing a literal basic Console program, not a WIN32 application that interfaces with the Console. XD (even though it's kinda a subset of the other)
There is no one like you in the universe.
RoyGBiv_13
Profile Blog Joined August 2010
United States1275 Posts
Last Edited: 2012-12-05 18:51:27
December 05 2012 18:50 GMT
#4174
On December 06 2012 03:37 Blisse wrote:
Show nested quote +
On December 06 2012 03:24 mustache wrote:
+ Show Spoiler +

void Print_Field()
{
int i;
int h;
system("cls"); // clears old screen
printf("points: %d\n", points); // displays points in first console line
for(i=0, h=1; i<AREA; i++) // prints gamefield, until the array with size AREA is completely printed
{
if(i == h*WIDTH) // array is 1 dimensional ; program skips to a new line every time WIDTH has been reached
{
printf("\n");
h=h+1;
}
printf("%c", field[i].sprite); // chooses what character is printed

}
}


so im just cycling through my array and printing the values that are in that. but because the redrawing isnt fast enough the screen flickers(i assume)

I've just started out programming in general so im not sure what multi threading or double buffering is.(so im probably not using it)

ill check out the links in detail, but i'd like to program the game from start to finish by my self, to get more familiar with how things work.

EDIT: i noticed in my first post i wrote im using visual basic. I meant visual studio c++, and im coding in C. ouch


Could you try building the array as a giant string, and then calling printf once on that entire string? That should solve it.


@Cecil, I think he's writing a literal basic Console program, not a WIN32 application that interfaces with the Console. XD (even though it's kinda a subset of the other)


Yep, turns out this is one of those things Windows sucks at. This program would run as intended on a Unix box, because of how POSIX streams work. In any case, probably best practice to build your string first, then print it. It will make the code a bit more readable.

Also, sorry that you have to deal with windows command line :/


Any sufficiently advanced technology is indistinguishable from magic
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
December 05 2012 18:51 GMT
#4175
On December 06 2012 03:37 Blisse wrote:
Show nested quote +
On December 06 2012 03:24 mustache wrote:
+ Show Spoiler +

void Print_Field()
{
int i;
int h;
system("cls"); // clears old screen
printf("points: %d\n", points); // displays points in first console line
for(i=0, h=1; i<AREA; i++) // prints gamefield, until the array with size AREA is completely printed
{
if(i == h*WIDTH) // array is 1 dimensional ; program skips to a new line every time WIDTH has been reached
{
printf("\n");
h=h+1;
}
printf("%c", field[i].sprite); // chooses what character is printed

}
}


so im just cycling through my array and printing the values that are in that. but because the redrawing isnt fast enough the screen flickers(i assume)

I've just started out programming in general so im not sure what multi threading or double buffering is.(so im probably not using it)

ill check out the links in detail, but i'd like to program the game from start to finish by my self, to get more familiar with how things work.

EDIT: i noticed in my first post i wrote im using visual basic. I meant visual studio c++, and im coding in C. ouch


Could you try building the array as a giant string, and then calling printf once on that entire string? That should solve it.


@Cecil, I think he's writing a literal basic Console program, not a WIN32 application that interfaces with the Console. XD (even though it's kinda a subset of the other)


Oh I see, haha. Yeah just use a single printf call. The screen might "flicker" a little when the console scrolls. You can change the height in characters of the console so that it cannot scroll, if you wanted to.
Recognizable
Profile Blog Joined December 2011
Netherlands1552 Posts
Last Edited: 2012-12-05 18:53:05
December 05 2012 18:52 GMT
#4176
Could somebody explain to me whilst keeping terminology to a minimum what Indendation(Python) actually does. I keep coding and doing stuff but this is something I keep wondering about. My guess is that it separates things.
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
Last Edited: 2012-12-05 18:55:33
December 05 2012 18:55 GMT
#4177
On December 06 2012 03:52 Recognizable wrote:
Could somebody explain to me whilst keeping terminology to a minimum what Indendation(Python) actually does. I keep coding and doing stuff but this is something I keep wondering about. My guess is that it separates things.

It creates a new scope every time you indent. The scope closes when the indent is de-indented.
mustache
Profile Joined April 2010
Switzerland309 Posts
December 05 2012 18:59 GMT
#4178
Ok thanks, ill try using a single string, though on my first try the game start crashing :D ill have to give it another go


btw here the code if anyone's interested, and wants to see the flickering.
+ Show Spoiler +
#include <stdio.h>
#include <conio.h>
#include <Windows.h>
#include <time.h>

/**************************************************************************************************************/
#define HEIGHT (24)
#define WIDTH (50)
#define AREA (HEIGHT*WIDTH)
#define MAX_LENGTH (40)
//#define LENGTH (30)
#define RIGHT ('d')
#define LEFT ('a')
#define UP ('w')
#define DOWN ('s')


/**************************************************************************************************************/
struct position // structure prototype for positions in array
{
int x;
int y;
char sprite;
};

struct position field[AREA]; // gamefield array
struct position snake[MAX_LENGTH]; // snake body array
struct position snake_temp[MAX_LENGTH]; // temp snake array copy
struct position fruit;
char input = RIGHT;
int SPEED = 5;
int points = 0;
int LENGTH = 1;
char buffer[AREA+1];
/**************************************************************************************************************/
int Game_Menu();
int Sub_Menu();
void Field_Set();
void Print_Field();
int Snake_Set();
void Snake_Input();
void Snake_Init();
void Menu();
void Fruit_Gen();
void Fruit_Set();
/**************************************************************************************************************/

int main()
{
int game_loop = 1;
int main_loop = 1;
srand(time(NULL));

while(main_loop ==1)
{
Game_Menu();

game_loop = 1;
while (game_loop == 1)
{
Snake_Input();
Field_Set();
Fruit_Set();
game_loop = Snake_Set();
Print_Field();
}
}
return 0;
}

int Game_Menu()
{
int menu = 1;
int game_loop;
char menu_input = 'o';


while( menu == 1)
{
Menu();
menu_input = getch();
menu = Sub_Menu(menu_input);
}

system("cls");
return 0;
}
void Menu()
{
system("cls");
printf("*******************************************************************************\n");
printf("* ***** * * ***** * * **** *\n");
printf("* * ** * * * * * * *\n");
printf("* ***** * * * ***** * ** *\n");
printf("* * * ** * * * * * *\n");
printf("* ***** * * * * * * **** *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* SET SPEED OPTIONS[s] SET OBSTACLE OPTIONS[o] *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* START GAME[y] *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* exit[x] *\n");
printf("*******************************************************************************\n");
}
int Sub_Menu(char menu_input)
{
int output = 1;
char set_char = 'z';

switch(menu_input)
{
case 's':
system("cls");
printf("*******************************************************************************\n");
printf("* ***** * * ***** * * **** *\n");
printf("* * ** * * * * * * *\n");
printf("* ***** * * * ***** * ** *\n");
printf("* * * ** * * * * * *\n");
printf("* ***** * * * * * * **** *\n");
printf("* DIFFICULTY SETTINGS *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* HARD[h] EASY[e] *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* MIDDLE[m] *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* *\n");
printf("* exit[x] *\n");
printf("*******************************************************************************\n");

while(set_char != 'm' && set_char != 'e' && set_char != 'h')
{
set_char = getch();
}
switch(set_char)
{
case 'e':
SPEED = 100;
break;
case 'm':
SPEED = 5;
break;
case 'h':
SPEED = 0;
}
break;
case 'o':
printf("you are in obstacle menu");
break;
case 'y':
output = 0;
Snake_Init();
break;
case 'x':
printf("you exited the game");
break;
}

return output;
}

void Field_Set()
{
int i,h;// counter variable




for(i = 0, h=0; i< AREA;i++)// writes x and y coordinates into gamefield array
{

field[i].x = i%WIDTH;
field[i].y = i/WIDTH;
}


for(i = 0; i< AREA; i++)// writes boundarys into gamefield
{
if(field[i].x == 0 || field[i].x == (WIDTH-1) || field[i].y == 0 || field[i].y == (HEIGHT-1))
{
field[i].sprite = '*'; // boundary
}
else
{
field[i].sprite = ' '; // empty space
}
}
}

void Print_Field()
{
int i;
int h;
system("cls");
printf("points: %d\n", points);
for(i=0, h=1; i<AREA; i++) // prints gamefield
{
if(i == h*WIDTH)
{
printf("\n");

h=h+1;
}
printf("%c", field[i].sprite);

}


}

int Snake_Set()
{
int game_loop = 1;
int i;

for(i=0;i<LENGTH;i++)
{
if(field[snake[i].x + snake[i].y*WIDTH].sprite == '*' ||field[snake[i].x + snake[i].y*WIDTH].sprite == 'X')
{
Beep(500,500);
game_loop = 0;
}
else if(field[snake[i].x + snake[i].y*WIDTH].sprite == 'O')
{
points = points + 10;
if(LENGTH < MAX_LENGTH-1)
{
LENGTH = LENGTH +1;
}
Fruit_Gen();
Beep(500,30);
Beep(400,100);
}
else
{
field[snake[i].x+ snake[i].y*WIDTH].sprite = '*';
}
}
return game_loop;
}

void Snake_Input() // DONT FORGET TO SET THE DIRECTION TO AN INITIAL VALUE
{
int i;
int temp_input;


for(i=0; i<1; i++)
{
if(kbhit() == 1)
{
temp_input = getch();

if(temp_input == RIGHT || temp_input == LEFT || temp_input == UP ||temp_input == DOWN)
{
input = temp_input;
}
}
else
{
Sleep(SPEED);
}
}
for(i=0; i<LENGTH; i++) // copies snake into a temp snake array, and increase the positions
{
snake_temp[i].x = snake[i].x;
snake_temp[i].y = snake[i].y;
}

for(i=0;i<LENGTH;i++)
{
snake[i+1].x = snake_temp[i].x;
snake[i+1].y = snake_temp[i].y;
}


switch(input)
{
case RIGHT:
snake[0].x = snake[0].x + 1;
break;
case LEFT:
snake[0].x = snake[0].x - 1;
break;
case UP:
snake[0].y = snake[0].y - 1;
break;
case DOWN:
snake[0].y = snake[0].y + 1;
break;
}
}

void Snake_Init()
{
int i;
Fruit_Gen();
points = 0;
LENGTH = 1;
for(i = 0; i<MAX_LENGTH;i++)
{
snake[i].x = WIDTH/2-i;
snake[i].y = HEIGHT/2;
}
}

void Fruit_Gen()
{
int random_x;
int random_y;
int fruit_loop = 1;

while(fruit_loop == 1)
{
random_x = rand()%(WIDTH/2+1);
random_y = rand()%(HEIGHT/2+1);
if(random_x != 0 && random_x != WIDTH && random_y != 0 && random_y != HEIGHT)
{
fruit_loop = 0;
fruit.x = random_x;
fruit.y = random_y;
}
}
}

void Fruit_Set()
{
field[fruit.x+ fruit.y*WIDTH].sprite = 'O';
}
Recognizable
Profile Blog Joined December 2011
Netherlands1552 Posts
Last Edited: 2012-12-05 19:06:29
December 05 2012 19:06 GMT
#4179
I had to search for what scope is but I guess I understand it a bit better now. Why is this important? Why doesn't the computer understand what you are doing when you aren't indenting?

def f(a,b):
c = a + b
c = g(c) + X
d = c * c + a
return d * b


Am I right in saying that this works because return is in the same scope as C,D and so it can do something with these variables. If return wasn't indented it wouldn't be able to do that because it wasn't in the same scope and therefore can't do anything with those variables?
CecilSunkure
Profile Blog Joined May 2010
United States2829 Posts
December 05 2012 19:10 GMT
#4180
On December 06 2012 04:06 Recognizable wrote:
I had to search for what scope is but I guess I understand it a bit better now. Why is this important? Why doesn't the computer understand what you are doing when you aren't indenting?

def f(a,b):
c = a + b
c = g(c) + X
d = c * c + a
return d * b


Am I right in saying that this works because return is in the same scope as C,D and so it can do something with these variables. If return wasn't indented it wouldn't be able to do that because it wasn't in the same scope and therefore can't do anything with those variables?

Yeah that's right. When you make something within a scope, things outside of that scope cannot access the variable. It's not about the computer "knowing what you are doing", the Python interpreter is just following rules the creator laid out. Scoping is a good thing.

If there was no scoping (or another way to say this is if there were only the global scope) then you could only have one name to one identifier. This gets annoying when you want to reuse an identifier.


for i in range( 0, 10 ):
print( i )

for x in range( 0, 10 ):
print( x )


You can see here I've already used two different identifiers, x and i. Luckily those two variables are within the scope of the for loops, otherwise I'd have to use a different variable name for each loop.
Prev 1 207 208 209 210 211 1032 Next
Please log in or register to reply.
Live Events Refresh
Next event in 3h 43m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
mouzHeroMarine 529
JuggernautJason135
StarCraft: Brood War
Britney 18343
Shuttle 167
firebathero 111
Dota 2
syndereN404
Pyrionflax213
Counter-Strike
FalleN 4682
fl0m1648
Foxcn131
Other Games
tarik_tv10230
gofns5961
FrodaN4713
summit1g3687
Grubby3130
Liquid`RaSZi2535
B2W.Neo870
Beastyqt573
shahzam436
Liquid`Hasu364
Mlord312
Harstem282
KnowMe226
C9.Mang0222
ToD219
mouzStarbuck215
XaKoH 163
ZombieGrub40
Ketroc13
Organizations
Other Games
gamesdonequick2499
BasetradeTV79
StarCraft 2
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 18 non-featured ]
StarCraft 2
• musti20045 33
• RyuSc2 3
• Kozan
• LaughNgamezSOOP
• sooper7s
• AfreecaTV YouTube
• intothetv
• Migwel
• IndyKCrew
StarCraft: Brood War
• HerbMon 34
• STPLYoutube
• ZZZeroYoutube
• BSLYoutube
Dota 2
• masondota2736
League of Legends
• TFBlade1765
• Doublelift1724
Other Games
• imaqtpie2668
• Shiphtur143
Upcoming Events
All-Star Invitational
3h 43m
INnoVation vs soO
Serral vs herO
Cure vs Solar
sOs vs Scarlett
Classic vs Clem
Reynor vs Maru
uThermal 2v2 Circuit
13h 28m
AI Arena Tournament
21h 28m
All-Star Invitational
1d 3h
MMA vs DongRaeGu
Rogue vs Oliveira
Sparkling Tuna Cup
1d 11h
OSC
1d 13h
Replay Cast
2 days
Wardi Open
2 days
Monday Night Weeklies
2 days
The PondCast
4 days
[ Show More ]
Replay Cast
6 days
Big Brain Bouts
6 days
Serral vs TBD
Liquipedia Results

Completed

Proleague 2026-01-14
Big Gabe Cup #3
NA Kuram Kup

Ongoing

C-Race Season 1
IPSL Winter 2025-26
BSL 21 Non-Korean Championship
CSL 2025 WINTER (S19)
KCM Race Survival 2026 Season 1
OSC Championship Season 13
Underdog Cup #3
BLAST Bounty Winter Qual
eXTREMESLAND 2025
SL Budapest Major 2025
ESL Impact League Season 8
BLAST Rivals Fall 2025
IEM Chengdu 2025

Upcoming

Escore Tournament S1: W5
Acropolis #4
IPSL Spring 2026
Bellum Gens Elite Stara Zagora 2026
HSC XXVIII
Rongyi Cup S3
SC2 All-Star Inv. 2025
Nations Cup 2026
BLAST Open Spring 2026
ESL Pro League Season 23
ESL Pro League Season 23
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026
TLPD

1. ByuN
2. TY
3. Dark
4. Solar
5. Stats
6. Nerchio
7. sOs
8. soO
9. INnoVation
10. Elazer
1. Rain
2. Flash
3. EffOrt
4. Last
5. Bisu
6. Soulkey
7. Mini
8. Sharp
Sidebar Settings...

Advertising | Privacy Policy | Terms Of Use | Contact Us

Original banner artwork: Jim Warren
The contents of this webpage are copyright © 2026 TLnet. All Rights Reserved.