[Tutor] Random ai win glitch

2019-05-01 Thread Cravan
Hi all, 

    I’m a beginner using python3, and recently learnt oop in 
pygame. What I’m trying to create is a tug of war game, but somehow my console 
prints Ai wins even before the player crosses the middle line.Here is my code:

 

newsettings.py:

# Basic colours

BLACK    = (   0,   0,   0)

WHITE    = ( 255, 255, 255)

GREEN    = (   0, 255,   0)

RED  = ( 255,   0,   0)

BLUE = (   0,   0, 255)

YELLOW   = ( 255, 255, 0)

ORANGE   = ( 255, 165, 0)

PURPLE   = ( 100, 0, 100)

 

#settings

WIDTH = 1024

HEIGHT = 768

FPS = 60

TITLE = "War of Tugs"

BGCOLOR = GREEN

 

spritesdata.py:

import pygame as pg

from os import path

import os

from newsettings import *

size = (WIDTH, HEIGHT)

screen = pg.display.set_mode(size)

pg.display.init()

game_folder = os.path.dirname(__file__)

resources_folder = os.path.join(game_folder, 'resources')

stickmanplayer_img = pg.image.load(os.path.join(resources_folder, 
'stickmanplayer.png')).convert()

stickmanai_img = pg.image.load(os.path.join(resources_folder, 
'stickmanai.png')).convert()

#https://www.shutterstock.com/image-vector/stick-figure-pulling-rope-coiled-1140317804?src=c7Vbr97B4rIRsJ9OYUVcLw-1-0

string = pg.sprite.Group()

all_sprites = pg.sprite.Group()

stickman_ai = pg.sprite.Group()

stickman_player = pg.sprite.Group()

AI_wins = 0

Player_wins = 0

class Player(pg.sprite.Sprite):

    def __init__(self, game, x, y):

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.game = game

    self.image = stickmanplayer_img

    self.image.set_colorkey(BLACK)

    self.rect = self.image.get_rect()

    self.rect.x = x

    self.rect.y = y

    self.dx = 1

    def move_left(self,lol):

    if self.rect.left > 10 and lol.rect.x > 310:

    self.rect.x -= 30

    lol.rect.x -= 30

    else:

    self.rect.left = 10

 

    def update(self):

    if self.rect.right < 770:

    self.rect.x += self.dx

class AI(pg.sprite.Sprite):

    def __init__(self, game, x, y):

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.game = game

    self.image = stickmanai_img

    self.image.set_colorkey(BLACK)

    self.rect = self.image.get_rect()

    self.rect.x = x

    self.rect.y = y

    self.dx = 1

    def update(self):

    if self.rect.right < 1000:

    self.rect.x += self.dx

 

class String(pg.sprite.Sprite):

    def __init__(self, game, x, y, height = 10, width = 320):

    self.game = game

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.height = height

    self.width = width

    self.surface = pg.Surface((2 * self.width, 2 * self.height))

    self.surface.fill(YELLOW)

    self.surface.set_colorkey(YELLOW)

    pg.draw.line(self.surface, BLACK, [0,0], [self.width,0], self.height)

    self.image = self.surface

    self.rect = self.image.get_rect()

    self.rect.x = x

    self.rect.y = y

    self.dx = 1

    def move_to_player(self):

    if self.rect.left > 100:

    self.rect.x -= 30

    else:

    self.rect.left = 100

    def update(self):

    if self.rect.right < 1300:

    self.rect.x += self.dx

    def check_win(self,lol):

    global Player_wins

    global AI_wins

    if self.rect.right < lol.rect.x:

    Player_wins += 1

    print("player wins")

    if self.rect.left > lol.rect.x:

    AI_wins += 1

    print("ai wins")

class Middle_Line(pg.sprite.Sprite):

    def __init__(self, game, x):

    self.game = game

    self.groups = game.all_sprites

    pg.sprite.Sprite.__init__(self, self.groups)

    self.width = WIDTH + 100

    self.height = HEIGHT

    self.surface = pg.Surface((2 * self.width, 2 * self.height))

    self.surface.fill(WHITE)

    self.surface.set_colorkey(WHITE)

    pg.draw.line(self.surface, RED, [200, 0], [200, self.height], 5)

    self.image = self.surface

    self.rect = self.image.get_rect()

    self.rect.x = x

main.py:

import pygame as pg

import sys

import random

import math

import time

from os import path

from newsettings import *

from spritesdata import *

clock = pg.time.Clock()

Player_wins = 0

AI_wins = 0

class Game:

    def __init__(self):

    pg.init()

    Player_wins = 0

    AI_wins = 0

    self.screen = pg.display.set_mode((WIDTH, HEIGHT))

    pg.display.set_caption(TITLE)

    self.clock = pg.time.Clock()

    self.time = pg.time.get_ticks()

    pg.key.set_repeat(500, 100)

    self.all_sprites = pg.sprite.Group()

    self.player = Player(self, 249, 384)

    self.ai = AI(self, 550, 430)

    self.middle_line = Middle_Line(self, 300)

    self.string = String(s

[Tutor] execute_from_command_line(sys.argv)

2019-05-01 Thread Miguel Angel Ramos Herrera via Tutor
Hi, good afternoon…


This line of code gives me an error

Can not find sys.argv

Appreciate if anybody can assist me in this item…

Miguel angel
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] execute_from_command_line(sys.argv)

2019-05-01 Thread Alan Gauld via Tutor
On 01/05/2019 20:08, Miguel Angel Ramos Herrera via Tutor wrote:

> This line of code gives me an error

I assume you mean this line:

execute_from_command_line(sys.argv)

But that's not a line of standard Python so I can
only assume that this is a function that you have
defined yourself.

But without seeing what the function does it's
hard to debug it.

However, the solution could simply be that you
have not imported sys.

You would need to call it with

import sys
execute_from_command_line(sys.argv)

If that is not the solution then please post again
with more detail (see below)


> Can not find sys.argv

That's not a standard python error message. Please
always send the full error trace not a summary or
partial message. There is a lot of important
detail in the text.

Please always try to show us both the actual code
that produces an error as well as the full error
text. And, if significant, the input and output data too.

It may also help to tell us the Python version and OS.

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor