Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎯 Unreal

First game, endless criticizm, hopefully 1 compliment :)

Started by avgprogramingjoe Aug 7, 2007 at 7:46 PM 9 replies 2.2k views
Original Post
avgprogramingjoe
avgprogramingjoe
Hey guys I started programming a month ago, but i have only had about a good week, maybe two to learn due to vacations and getting grounded. I have finally made my first game. I am quite glad as I feel I have come a long way since I have only had a computer for a few months. It's a simple pong game, the pong is square as I couldn't figure out how to make a circle that moved. The ai and gameplay is pretty good from my prospective. The code though is really sloppy, the graphics(colors), collision detection all pretty much suck, but I'm just glad to have finally gotten a full game together. Any comments or suggestions are welcome. heres the code
#Import and initialize
import pygame, random
from pygame.locals import *

global wins, comp, main, deaths, KeepGoing, box_x, box_y

pygame.init()

KeepGoing = True
deaths = 0
wins = 0
main = 1
comp = 0

def check_key():
    
    global box_y, main
    
    keystate = pygame.key.get_pressed()
    if box_y > 0:
        
        if keystate[pygame.locals.K_UP]:
            box_y -= 5
            
    if box_y < 430:
        
        if keystate[pygame.locals.K_DOWN]:
            box_y += 5
            
    if keystate[pygame.locals.K_ESCAPE]:
        main = 1
        
def main_seq():
    global main, KeepGoing, winning, deaths, wins
    
    pygame.init()
    
    deaths = 0
    wins = 0
    color = (10, 10, 10)
    display = True
    
    screen = pygame.display.set_mode((640,480))
    pygame.display.set_caption("Hello, world!")
    
    intro = "Welcome to pong, please click here to start."
    myFont = pygame.font.SysFont("Comic Sans MS", 15)
    label = myFont.render(intro, 1, (255,25,0))
    
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((100,60,25))
    
    while display:
        
        keystate = pygame.key.get_pressed()
        
        if keystate[pygame.locals.K_ESCAPE]:
            not_playing = False
            KeepGoing = False
            
        for event in pygame.event.get():
            
            if event.type == pygame.QUIT:
                    KeepGoing = False
                    display = False
                    winning = False
                    
            mouse = pygame.mouse.get_pos()
            
            if mouse[0] >= 220 and mouse[0] <= 545 and mouse[1] >= 200 and mouse[1] <= 250:
                
                if event.type == pygame.MOUSEBUTTONDOWN:
                    color = (70, 70, 70)
                else:
                    color = (50, 50, 50)
                    
                if event.type == MOUSEBUTTONUP:
                    display = False
                    main = 0
                    
            else:
                color = (10, 10, 10)
                
        bar = pygame.Surface((325, 50))
        bar = bar.convert()
        bar.fill(color)
        
        screen.blit(background, (0,0))
        screen.blit(bar, (220, 200))
        screen.blit(label, (240, 220))
        pygame.display.flip()
        
def winning_game():

    global winning, comp, main, deaths, box_y, KeepGoing

    #Display configuration
    screen = pygame.display.set_mode((640,480))
    pygame.display.set_caption("Hello, world!")
    
    #Entities (just background for now)
    background = pygame.Surface(screen.get_size())
    background = background.convert()
    background.fill((0,0,0))
    
    box = pygame.Surface((10, 50))
    box = box.convert()
    box.fill((200,200,150))
    
    paddle = pygame.Surface((10, 50))
    paddle = paddle.convert()
    paddle.fill((200,200,150))
    
    pong_blit = pygame.image.load("pong.bmp")
    pong_blit = pong_blit.convert()
    
    box_x = 625
    box_y = 300
    
    pong_x = random.randrange(60, 300)
    pong_y = random.randrange(60, 400)
    
    paddle_x = 20
    paddle_y = 0
    
    x = 5
    y = 5
    
    bounce = True
    
    text = "Computer: "+ str(deaths) + " User: " + str(wins)
    myFont = pygame.font.SysFont("Comic Sans MS", 12)
    label = myFont.render(text, 1, (255,255,0))
    
    #Assign values to key variables
    clock = pygame.time.Clock()
    winning = True
    dir = 0
    counter = 11
    pong_dir = pong_y
    
    while winning:
        
        clock.tick(30)
        
        box_x_right = box_x + 10
        box_y_bottom = box_y + 50
        pong_x_right = pong_x + 25
        pong_y_bottom = pong_y + 25
        
        check_key()
        
        if main == 1:
            winning = False
        if box_x <= pong_x_right and box_x + 10 >= pong_x_right and box_y <= pong_y_bottom and box_y + 50 >= pong_y_bottom:
            x = -x
            
            if x < 0:
                x -= 0.15
                y -= 0.1
            else:
                x += 0.15
                y += 0.1
                
        if box_x <= pong_x and box_x + 10 >= pong_x and box_y <= pong_y and box_y + 50 >= pong_y:
            x = -x
            
            if x < 0:
                x -= 1
                y -= 0.75
            else:
                x += 1
                y += 0.75
                
        if paddle_x <= pong_x_right and paddle_x + 10 >= pong_x_right and paddle_y <= pong_y_bottom and paddle_y + 50 >= pong_y_bottom:
            x = -x
            faster = ( 1.00 * random.randrange(1, 80))/ 100
            faster_1 = ( 1.10 * random.randrange(1, 120))/ 100
            
            if x < 0:
                x -= faster
                y -= faster_1
            else:
                x += faster
                y += faster_1
                
        if paddle_x <= pong_x and paddle_x + 10 >= pong_x and paddle_y <= pong_y and paddle_y + 50 >= pong_y:
            x = -x
            
            if x < 0:
                x -= 1
                y -= 0.75
            else:
                x += 1
                y += 0.75
        
        dir = 0
        
        if pong_y >= 455:
            y = -y
            
        if pong_y <= 0:
            y = -y
            
        speed = [6, -6]
        
        if pong_x > 350 and pong_y <= random.randrange(1, 480):
            rand = random.randrange(1, 5)
            
            if rand == 3:
                counter = -5
                
        if pong_x < 300:
            counter = 11
            
        if counter > 10:
            
            if paddle_y > pong_y:
                
                if paddle_y > 0 :
                    dir = speed[1]
                elif paddle_y < 0:
                    dir = 0
                    
            if paddle_y < pong_y:
                
                if paddle_y < 430:
                    dir = speed[0]
                elif paddle_y > 430:
                    dir = 0
                    
            elif pong_dir > pong_y and pong_x < 350 and pong_x > 100 and pong_y > 100 and pong_y < 500:
                dir = speed[1]
                
            elif pong_dir < pong_y and pong_x < 350 and pong_x > 100 and pong_y > 100 and pong_y < 500:
                dir = speed[0]
        counter += 1   
        paddle_y += dir
        
        if pong_x >= 615:
            winning = False
            main = 0
            comp = 0
        elif pong_x <= 0 :
            winning = False
            main = 0
            comp = 1
        
        pong_x += x
        pong_y += y
        pong_dir = pong_y
        
        for event in pygame.event.get():
            
            if event.type == pygame.QUIT:
                KeepGoing = False
                winning = False
        
            
        screen.blit(background, (0,0))
        screen.blit(label, (400,20))
        screen.blit(box, (box_x, box_y))
        screen.blit(paddle, (paddle_x, paddle_y))
        screen.blit(pong_blit, (pong_x, pong_y))
        pygame.display.flip()


def game():

    global deaths, wins, comp, main, KeepGoing
    
    main = 1
    
    while KeepGoing:

        if main == 0:
            
            if comp == 0:
                deaths += 1
            elif comp == 1:
                wins += 1
                
        elif main == 1:
            main_seq()
        winning_game()

if __name__ == "__main__":
    
    game()    
        


A suggestions on how to improve the collision detection and make a circle are most wanted. Thanks, Joe [Edited by - avgprogramingjoe on August 8, 2007 1:31:17 PM]
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
herocks
herocks
Just use a circle instead of a rect. Pretty good considering who long you've been programming.
I'm a loser, thanks for letting me know.N00b game programmer.
avgprogramingjoe
avgprogramingjoe
I don't think that your a loser, never said so. But I don't understand how to make a circle. I can draw one, but I can't delete one, and make it move. Could someone explain how.


Thanks,


Joe
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
herocks
herocks
...That was from my signature...anyway, you could try to create a image and make "nothing" a certain color and the circle another color (put a circle in a image, everything else is one color). Then you can remove the other parts with a color key..however, I don't know how to do that in Pygame, however....

Once you have an image loaded, just have it move around by changing where you draw it.
I'm a loser, thanks for letting me know.N00b game programmer.
swiftcoder
swiftcoder
You might want to try putting your source code in [ source ] [ / source ] tags. Not only will it provide basic indentation and syntax highlighting, it will also place it in a nice scrolling div-tag, so that it doesn't make your post a mile long :)
Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]
avgprogramingjoe
avgprogramingjoe
sorry I fixed the tags, thanks for the tips. And herocks, my bad, didn't know that was your sig, looks like part of your post.


Joe
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
JBourrie
JBourrie
@herocks

You ought to make your sig a different font or something... I was also trying to figure out why you thought he was calling you a loser [lol]
Zyndrof
Zyndrof
One thing you should think about is using much more whitespace to make your code more readable.

A very short example:
class main():    def Main():       while True:           print "Hello"class somethingElse():    def haha():        while True:           print "Haha"class ...

...is less readable than:
class main():    def Main():       while True:           print "Hello"class somethingElse():    def haha():        while True:           print "Haha"class ...

Ofcourse I don't make a point in my example. But try it in your code and see a difference :)
avgprogramingjoe
avgprogramingjoe
I cleaned up the code. The biggest glitch I have found is the collision detection. It doesn't work in special cases (probably because it sucks). I still can't figure out how to make it detect collision better nor can I make the pong a circle. Any suggestions.

Joe

[Edited by - avgprogramingjoe on August 8, 2007 12:24:43 PM]
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site
Chad Smith
Chad Smith
Quote:
Original post by avgprogramingjoe
I cleaned up the code. The biggest glitch I have found is the collision detection. It doesn't work in special cases (probably because it sucks). I still can't figure out how to make it detect collision better nor can I make the pong a circle. Any suggestions.

Joe


I do not know python so I really can't just give you code. But I say that you were using PyGame so I went ahead to try to answer your circle question.

Anyway, basically you just need to load an image of a circle. Lets say the whole image is 64x64. Then you want only half (32x32) of that to actually be a circle. So in paint or some other image editing program you create a w/e size square/rectangle and fill it with a color. Then you select a different color and draw a circle in the square/rectangle.

Save it to your programs directory and get to coding.

You use the pygame.image.load(filename here as a string) function to load an image. Then, I think you will use the set_colorkey function which from the looks of it seems to be apart of pygames Surface struct/class.

Then you basically just update it's x and y like you have been and just draw the ball every frame using the x and y values and bam...it's moving.

I hope that helped at all. I don't know if pygame can just draw a circle in code, so that is why I took you the image loading route.

Chad


avgprogramingjoe
avgprogramingjoe
Thanks I got the ball so that it is now circular rather than square. I have updated the code above to consolidate for this. Now does anybody have any suggestions for the collision detection or ai? Also are there any attributes from the original pong that I am missing?

Thanks,

Joe
Visit my general programming site to get help, tutorials, as well as get experience by joining projects or making your own - My Site

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.