• Log InLog In
  • Register
Liquid`
Team Liquid Liquipedia
EDT 16:31
CEST 22:31
KST 05:31
  • 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
[ASL21] Ro24 Preview Pt2: News Flash8[ASL21] Ro24 Preview Pt1: New Chaos0Team Liquid Map Contest #22 - Presented by Monster Energy16ByuL: The Forgotten Master of ZvT30Behind the Blue - Team Liquid History Book20
Community News
Weekly Cups (March 23-29): herO takes triple6Aligulac acquired by REPLAYMAN.com/Stego Research8Weekly Cups (March 16-22): herO doubles, Cure surprises3Blizzard Classic Cup @ BlizzCon 2026 - $100k prize pool49Weekly Cups (March 9-15): herO, Clem, ByuN win4
StarCraft 2
General
What mix of new & old maps do you want in the next ladder pool? (SC2) Team Liquid Map Contest #22 - Presented by Monster Energy Aligulac acquired by REPLAYMAN.com/Stego Research Weekly Cups (March 23-29): herO takes triple herO wins SC2 All-Star Invitational
Tourneys
RSL Season 4 announced for March-April Sparkling Tuna Cup - Weekly Open Tournament StarCraft Evolution League (SC Evo Biweekly) WardiTV Mondays World University TeamLeague (500$+) | Signups Open
Strategy
Custom Maps
[M] (2) Frigid Storage Publishing has been re-enabled! [Feb 24th 2026]
External Content
Mutation # 519 Inner Power The PondCast: SC2 News & Results Mutation # 518 Radiation Zone Mutation # 517 Distant Threat
Brood War
General
ASL21 General Discussion Gypsy to Korea How Can I Add Timer & APM Count? A cwal.gg Extension - Easily keep track of anyone BGH Auto Balance -> http://bghmmr.eu/
Tourneys
[ASL21] Ro24 Group E Azhi's Colosseum - Foreign KCM Escore Tournament StarCraft Season 2 [Megathread] Daily Proleagues
Strategy
Fighting Spirit mining rates What's the deal with APM & what's its true value Simple Questions, Simple Answers
Other Games
General Games
Nintendo Switch Thread Stormgate/Frost Giant Megathread Starcraft Tabletop Miniature Game General RTS Discussion Thread Darkest Dungeon
Dota 2
The Story of Wings Gaming Official 'what is Dota anymore' discussion
League of Legends
G2 just beat GenG in First stand
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
Mafia Game Mode Feedback/Ideas TL Mafia Community Thread Five o'clock TL Mafia
Community
General
US Politics Mega-thread Things Aren’t Peaceful in Palestine Canadian Politics Mega-thread The Games Industry And ATVI European Politico-economics QA Mega-thread
Fan Clubs
The IdrA Fan Club
Media & Entertainment
[Manga] One Piece Movie Discussion! [Req][Books] Good Fantasy/SciFi books
Sports
2024 - 2026 Football Thread Formula 1 Discussion Cricket [SPORT] Tokyo Olympics 2021 Thread General nutrition recommendations
World Cup 2022
Tech Support
[G] How to Block Livestream Ads
TL Community
The Automated Ban List
Blogs
Funny Nicknames
LUCKY_NOOB
Money Laundering In Video Ga…
TrAiDoS
Iranian anarchists: organize…
XenOsky
FS++
Kraekkling
Shocked by a laser…
Spydermine0240
ASL S21 English Commentary…
namkraft
Customize Sidebar...

Website Feedback

Closed Threads



Active: 1432 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
Germany2594 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 29m
[ Submit Event ]
Live Streams
Refresh
StarCraft 2
UpATreeSC 189
ProTech136
Hui .88
StarCraft: Brood War
Calm 4046
Mini 937
Horang2 588
EffOrt 443
firebathero 269
BeSt 195
Soulkey 176
actioN 148
Dewaltoss 103
hero 46
[ Show more ]
Backho 33
910 21
IntoTheRainbow 7
Dota 2
monkeys_forever168
capcasts58
Counter-Strike
fl0m1430
byalli691
minikerr6
Heroes of the Storm
Liquid`Hasu478
Other Games
summit1g9122
gofns8679
Grubby2978
FrodaN2049
Beastyqt706
RotterdaM335
Fuzer 159
ArmadaUGS145
C9.Mang0132
ToD71
Trikslyr43
KnowMe34
ZombieGrub29
Organizations
StarCraft 2
angryscii 39
Blizzard YouTube
StarCraft: Brood War
BSLTrovo
sctven
[ Show 16 non-featured ]
StarCraft 2
• AfreecaTV YouTube
• intothetv
• Kozan
• IndyKCrew
• LaughNgamezSOOP
• Migwel
• sooper7s
StarCraft: Brood War
• blackmanpl 54
• sM.Zik 1
• BSLYoutube
• STPLYoutube
• ZZZeroYoutube
Dota 2
• WagamamaTV753
• lizZardDota274
Other Games
• imaqtpie1083
• Shiphtur164
Upcoming Events
Replay Cast
3h 29m
RSL Revival
13h 29m
Maru vs MaxPax
BSL
22h 29m
RSL Revival
1d 10h
Cure vs Rogue
uThermal 2v2 Circuit
1d 17h
BSL
1d 22h
Afreeca Starleague
2 days
Wardi Open
2 days
Replay Cast
3 days
Sparkling Tuna Cup
3 days
[ Show More ]
Kung Fu Cup
4 days
The PondCast
5 days
Replay Cast
6 days
Liquipedia Results

Completed

CSL Season 20: Qualifier 1
WardiTV Winter 2026
NationLESS Cup

Ongoing

BSL Season 22
CSL Elite League 2026
ASL Season 21
CSL Season 20: Qualifier 2
StarCraft2 Community Team League 2026 Spring
RSL Revival: Season 4
Nations Cup 2026
Stake Ranked Episode 1
BLAST Open Spring 2026
ESL Pro League S23 Finals
ESL Pro League S23 Stage 1&2
PGL Cluj-Napoca 2026
IEM Kraków 2026
BLAST Bounty Winter 2026

Upcoming

CSL 2026 SPRING (S20)
Acropolis #4
IPSL Spring 2026
BSL 22 Non-Korean Championship
CSLAN 4
Kung Fu Cup 2026 Grand Finals
HSC XXIX
uThermal 2v2 2026 Main Event
IEM Cologne Major 2026
Stake Ranked Episode 2
CS Asia Championships 2026
Asian Champions League 2026
IEM Atlanta 2026
PGL Astana 2026
BLAST Rivals Spring 2026
CCT Season 3 Global Finals
IEM Rio 2026
PGL Bucharest 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.